Dat602 database application development
This presentation is the property of its rightful owner.
Sponsored Links
1 / 30

DAT602 Database Application Development PowerPoint PPT Presentation


  • 49 Views
  • Uploaded on
  • Presentation posted in: General

DAT602 Database Application Development . Lecture 6 JAVA Swing. Database Application Development - Lecture 6. Library of JAVA. In JAVA’s library, different packages contain classes with different functions. java.lang java.io java.util javax.swing …

Download Presentation

DAT602 Database Application Development

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Dat602 database application development

DAT602 Database Application Development

Lecture 6 JAVA Swing


Database application development lecture 6

Database Application Development - Lecture 6

  • Library of JAVA. In JAVA’s library, different packages contain classes with different functions.

    java.langjava.iojava.utiljavax.swing…

  • Developers must import corresponding package in the resource code before using it.


Database application development lecture 61

Database Application Development - Lecture 6

  • JFC and Swing

    JFC is short for Java Foundation Classes, which encompass a group of features for building graphical user interfaces (GUIs) and adding rich graphics functionality and interactivity to Java applications.

  • This lecture’s topic is javax.swing.


Database application development lecture 62

Database Application Development - Lecture 6

  • What is in javax.swing package ? What can swing provide to us ?


Database application development lecture 63

Database Application Development - Lecture 6

  • What is container in JAVA swing ?

    Container is a set of Java classes, a container could be JFrame, JApplet or JDialog. These containers can be used for containing other Java swing components or containers.


Database application development lecture 64

Database Application Development - Lecture 6

  • To appear onscreen, every GUI component must be part of a containment hierarchy. A containment hierarchy is a tree of components that has a top-level container as its root.


Database application development lecture 65

Database Application Development - Lecture 6

  • containment hierarchy for this example's GUI


Database application development lecture 66

Database Application Development - Lecture 6

  • Top-Level Containers and Containment Hierarchies

    Each program that uses Swing components has at least one top-level container. This top-level container is the root of a containment hierarchy — the hierarchy that contains all of the Swing components that appear inside the top-level container.


Database application development lecture 67

Database Application Development - Lecture 6

  • The Root Pane

    Each top-level container relies on a reclusive intermediate container called the root pane.


Database application development lecture 68

Database Application Development - Lecture 6

  • How to add a component to pane ?

    Here's the simple code that add a label to a container.

    container.add(yellowLabel, BorderLayout.CENTER);

    yellowLabel is a Label component.

    BorderLayout.CENTER indicate which kind of layout this component applied, and its position.


Database application development lecture 69

Database Application Development - Lecture 6

  • In next slides, you will see some useful swing components.

    Button, Label, List, Panel, Password Field, Scroll Pane, Text Field.


Database application development lecture 610

Database Application Development - Lecture 6

  • First, let’s see the JButton. In this part, we’ll use ButtonDemo for illustration.


Database application development lecture 611

Database Application Development - Lecture 6

  • Button. If you want using swing’s button component, you have to import following package in the beginning of your code.

    package: javax.swing.JButton

    import javax.swing.JButton;


Database application development lecture 612

Database Application Development - Lecture 6

  • How to create a JButton ?

    JButton b1 = new JButton("Disable middle button", leftButtonIcon);

  • A Swing button can display both text and an image.


Database application development lecture 613

Database Application Development - Lecture 6

  • After a JButton being created, some properties should be set.

    b1 = new JButton("Disable middle button", leftButtonIcon);

    b1.setVerticalTextPosition(AbstractButton.CENTER);

    b1.setHorizontalTextPosition(AbstractButton.LEADING); //aka LEFT, for left-to-right locales

    b1.setMnemonic(KeyEvent.VK_D);

    b1.setActionCommand("disable");


Database application development lecture 614

Database Application Development - Lecture 6

  • After b1 being created, we also need to add a action listener to this new button.

    b1.addActionListener(this);

  • What is action listener ?

    Action listener is a action capturer, it is used for notifying every time the user clicks the button.


Database application development lecture 615

Database Application Development - Lecture 6

  • After all properties of button being set, and the action listener being bound with this button, then we can add this button to its higher level container.

    for example, add button b1 to a pane:

    pand.add(b1);


Database application development lecture 616

Database Application Development - Lecture 6

  • For response the command from buttons, we should create a function to process different commands.

    public void actionPerformed(ActionEvent e) {

    if ("disable".equals(e.getActionCommand())) {

    b2.setEnabled(false);

    b1.setEnabled(false);

    b3.setEnabled(true);

    }

    }


Database application development lecture 617

Database Application Development - Lecture 6

  • JList

    A JList presents the user with a group of items, displayed in one or more columns, to choose from. Lists can have many items, so they are often put in scroll panes.


Database application development lecture 618

Database Application Development - Lecture 6

  • Create and initializing a List

    JList list = new JList(listModel); //create a instance of Jlist

    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // set a selection model

    list.setSelectedIndex(0);// the default selected item

    list.addListSelectionListener(this); // add a listener

    list.setVisibleRowCount(5); // number of visible records


Database application development lecture 619

Database Application Development - Lecture 6

  • Choose a list model

    If you initialize a list with an array or vector, the constructor implicitly creates a default list model. The default list model is immutable — you cannot add, remove, or replace items in the list.

    ArrayList data = new ArrayList();

    list = new JList(data); //data has type Object[]


Database application development lecture 620

Database Application Development - Lecture 6

  • Choose a list model

    To create a list whose items can be changed individually, set the list‘s model to an instance of a mutable list model class, such as an instance of DefaultListModel.

    You can set a list's model when you create the list or by calling the setModel method. 

    list.setModel(ListModel model);


Database application development lecture 621

Database Application Development - Lecture 6

  • List selection mode

    you can use the method setSelectionMode to set the selecting model.

    SINGLE_SELECTIONSINGLE_INTERVAL_SELECTIONMULTIPLE_INTERVAL_SELECTIO


Database application development lecture 622

Database Application Development - Lecture 6

  • Adding Items to and Removing Items from a List

  • Before introduce add and remove operation.

    Here we creates a mutable list model object, puts the initial items in it, and uses the list model to create a list. We create a list for our example first.

    DefaultListModellistModel = new DefaultListModel(); listModel.addElement("Debbie Scott"); listModel.addElement("Scott Hommel"); listModel.addElement("Alan Sommerer");

    JList list = new JList(listModel);


Database application development lecture 623

Database Application Development - Lecture 6

  • Add a element to list.

    example:

    listModel.insertElementAt(employeeName.getText(), index);

    employeeName is a text field which is used for input new employee’s name.

    index is used for indicating where this new element be inserted.

    NOTE: add new element to the object list contains.


Database application development lecture 624

Database Application Development - Lecture 6

  • Remove an element from list

    listModel.remove(index);

    index is used for indicating which element should be removed. You can indicate index manually, or you can use JList’s method getSelectedIndex to get the selected element’s index.


Database application development lecture 625

Database Application Development - Lecture 6

  • Scroll Panes

    A JScrollPane provides a scrollable view of a component. When screen real estate is limited, use a scroll pane to display a component that is large or one whose size can change dynamically.


Database application development lecture 626

Database Application Development - Lecture 6


Database application development lecture 627

Database Application Development - Lecture 6

  • Hot to create and initialize scroll pane ?

    JList list = new JList(array);

    JScrollPanescrollPane = new JScrollPane(list);

    You should also add this scroll pane to a higher level container.

    example:

    rootPane.add(scrollPane, BorderLayout.CENTER);


Database application development lecture 628

Database Application Development - Lecture 6

  • Reference

    http://java.sun.com/docs/books/tutorial/uiswing/components/index.html


  • Login