Swing

Using a List

A list is a swing component that displays lists of objects within a box. We have total control over how the items in the list are displayed. To create a list and specify its items, we pass an array to the JList constructor. Then we call the setVisibleRowCount method to set the number of rows we want to be visible, thus adding the list to the scroll pane, then add the scroll pane to a panel so that we can later add to the frame.

Here we create an array of Strings

String[] months = {"Jan.", "Feb.", "March", "April", "May", "June",
                             "July", "Aug.", "Sept.", "Oct.", "Nov.", "Dec"};

Now, adding the Strings to the list

list = new JList(months);

Then set 5 rows to be visible on the list

list.setVisibleRowCount(5)

Hence, add the list to the scroll pane

JScrollPane scroll = new JScrollPane(list);

Finally, add the scroll pane to a panel

JPanle panel = new JPanel();

Now, Lets see a more detail and complete example with outputs

 

ClickHere to download List.java

The above program only allows us to scroll down and up to see the list. JList also able us to select from the list then display what we've selected to the screen. Lets see a more expanded version of months, and this time we are allow to select which ever months we want from the list.


ClickHere to download SelectList.java

 

The above example, the only things different from the previous example is the button and we added a new method from JList, which is setSlectionMode(int mode) ( We can find the methods in the link below). Inside the button listener we added String select = (String) list.getSelectedValue() basically, it gets a single value from the list then assign it to select. The getSelectedValue method is in the JList class, where we can find it in the link below "Swing Method & Constructor Table." What if we want to select more than one from the list, luckily we have another method in JList that allows us to select more than one from the list. I will use the same example above just so we can easily tell the difference between just a JList, a single selectable JList, and a more than one selectable JList.
 

 

ClickHere to download SelectLists.java
 

The only difference from this example is the selectionMode has changed to MULTIPLE_INTERVAL_SELECTION, and we used a different getSelected method, in addition to that we used a loop to loop out the array. It is not much a different from the previous 3 examples besides the methods are different.

JComboBox

A combo box is a combination of a text field and a drop-down list where the user can choose a value. It's very similar to a JList.

Creating a JComboBox

JComboBox jcombo = new JComboBox();

Adding items to the combo Box

jcombo.addItem("Jan.");
jcombo.addItem("Feb.");
jcombo.addItem("March");

Alternative way of adding to the combo box using array

String[] month = {"Jan.", "Feb.", "March"};

JComboBox jcombo = new JComboBox(month);

Note: For all the methods and constructors look at the link below " Swing Method & Constructor Table."

 

ClickHere to download JCombo_Box.java

 

Click here to continue

  <Previous><Java 24><Home><Swing Method & Constructor Table>