JaVa
   

The JList

The JList class allows us to create a GUI object to contain a list of strings from which the user can select. Let's create an example program that uses the JList. Here is the complete code for the example.

Code Listing 14: Using the JList
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class JListExample extends JFrame implements ActionListener
{
 public static void main(String[] argv)
 {
 JListExample mainApp = new JListExample();
 }
 public JListExample()
 {
 super("JList Example");
 setBounds(0, 0, 450, 350);
 getContentPane().setLayout(null);
 setDefaultCloseOperation(EXIT_ON_CLOSE);
 // Create the list object...
 list = new JList();
 list.setBounds(10, 10, 420, 200);
 listData = new Vector();
 // Create a JTextField...
 textfield = new JTextField(15);
 textfield.setLocation(10, 220);
 textfield.setSize(textfield.getPreferredSize());
 // Create the two buttons...
 addButton = new JButton("Add Text to List");
 addButton.setLocation(200, 220);
 addButton.setSize(addButton.getPreferredSize());
 removeButton = new JButton("Remove Selected from List");
 removeButton.setLocation(200, 250);
 removeButton.setSize(removeButton.getPreferredSize());
 // Add the action listeners...
 addButton.addActionListener(this);
 removeButton.addActionListener(this);
 // Add the objects to the content pane...
 getContentPane().add(list);
 getContentPane().add(textfield);
 getContentPane().add(addButton);
 getContentPane().add(removeButton);
 setVisible(true);
 }
 public void actionPerformed(ActionEvent e)
 {
 if(e.getSource() == addButton)
 {
 // Check there is text in the 'textfield'
 if(textfield.getText().compareTo("") != 0)
 {
 // Then add it to the JList object
 listData.add(textfield.getText());
 list.setListData(listData);
 // Clear the textfield...
 textfield.setText("");
 }
 }
 else if(e.getSource() == removeButton)
 {
 // Check there is a list item selected
 if(list.getSelectedValue() != null)
 {
 // Then add it to the JList object
 listData.remove(list.getSelectedValue());
 list.setListData(listData);
 }
 }
 }
 JList list;
 Vector listData;
 JTextField textfield;
 JButton addButton;
 JButton removeButton;
}


Java End example

When we execute the JList example, we can see that the following is visible:

Java Click To expand
Screenshot: The JList example

In the example, we can enter text into the JTextField and click the Add Text to List button to append the data entered into the JTextField to the list. This can be repeated as many times as you wish. Also note that items in the list can be selected by clicking on them. Once an item is selected in the JList, it can then be removed by clicking the Remove Selected from List button. Let's take a look at the code that we used to add and manipulate the JList. First we create an object called list using the following code segement:

list = new JList();
list.setBounds(10, 10, 420, 200);


Then, once our object is created, we create a vector to hold the data that we wish to contain in our list object. This is accomplished with the following line of code:

listData = new Vector();


Now, let's look at the code that we use to add items into our list object:

if(textfield.getText().compareTo("") != 0)
{
 // Then add it to the JList object
 listData.add(textfield.getText());
 list.setListData(listData);
 // Clear the textfield...
 textfield.setText("");
}


First we check that the textfield object actually contains some text. Then, if so, we add the text to our listData vector, using the add method. Once we have added the text to the vector, we update the list by calling the setListData method of our list object using the listData object as the parameter. Finally, we clear the text in the text field. Let's look at how we handle removing items from the list.

if(list.getSelectedValue() != null)
{
 // Then add it to the JList object
 listData.remove(list.getSelectedValue());
 list.setListData(listData);
}


First, we ensure that the list has an item selected by comparing the getSelectedValue method to null. If the list has an item selected, we remove the item from the listData vector by invoking the remove method using the getSelectedValue method as a parameter. Then, finally, we update the list data by calling the setListData method of our list object.

Implementing a Scrollable JList

One problem with this example is that we add more items than can be displayed in the list; there is no way that we can scroll the list to see all the items. Let's make an addition to our example so that we have a vertical scroll bar to allow the scrolling of the JList. Let's look at the complete code now after we have implemented the scrolling code.

Code Listing 15: Implementing a scrollable JList
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class JListExample extends JFrame implements ActionListener
{
 public static void main(String[] argv)
 {
 JListExample mainApp = new JListExample();
 }
 public JListExample()
 {
 super("JList Example");
 setBounds(0, 0, 450, 350);
 getContentPane().setLayout(null);
 setDefaultCloseOperation(EXIT_ON_CLOSE);
 // Create the list object...
 list = new JList();
 list.setBounds(10, 10, 420, 200);
 listData = new Vector();
 // NEW ->
 // Create a scrollpane
 scrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_
 ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
 scrollPane.setBounds(10, 10, 420, 200);
 scrollPane.add(list);
 scrollPane.setViewportView(list);
 // <- NEW
 // Create a JTextField...
 textfield = new JTextField(15);
 textfield.setLocation(10, 220);
 textfield.setSize(textfield.getPreferredSize());
 // Create the two buttons...
 addButton = new JButton("Add Text to List");
 addButton.setLocation(200, 220);
 addButton.setSize(addButton.getPreferredSize());
 removeButton = new JButton("Remove Selected from List");
 removeButton.setLocation(200, 250);
 removeButton.setSize(removeButton.getPreferredSize());
 // Add the action listeners...
 addButton.addActionListener(this);
 removeButton.addActionListener(this);
 // Add the objects to the content pane...
 getContentPane().add(scrollPane); // MODIFIED
 getContentPane().add(textfield);
 getContentPane().add(addButton);
 getContentPane().add(removeButton);
 setVisible(true);
 }
 public void actionPerformed(ActionEvent e)
 {
 if(e.getSource() == addButton)
 {
 // Check there is text in the 'textfield'
 if(textfield.getText().compareTo("") != 0)
 {
 // Then add it to the JList object
 listData.add(textfield.getText());
 list.setListData(listData);
 // Clear the textfield...
 textfield.setText("");
 }
 }
 else if(e.getSource() == removeButton)
 {
 // Check there is a list item selected
 if(list.getSelectedValue() != null)
 {
 // Then add it to the JList object
 listData.remove(list.getSelectedValue());
 list.setListData(listData);
 }
 }
 }
 JList list;
 Vector listData;
 JTextField textfield;
 JButton addButton;
 JButton removeButton;
 JScrollPane scrollPane; // NEW
}


Java End example

When we now add a lot of items into the JList, we can see from the following screen shot that a scroll bar is visible and allows us to scroll the JList.

Java Click To expand
Screenshot: Implementing a scrollable JList

Let's look at the code that we have added/modified in our example. First we created a JScrollPane with the following code segment:

scrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setBounds(10, 10, 420, 200);


Then, once created, we add our list object to our scrollPane object and set the viewport to be our list object. This is accomplished with the following two lines of code:

scrollPane.add(list);
scrollPane.setViewportView(list);


Finally we have modified the code that adds our list to the content pane to add our scrollPane object instead, as our list object has already been added to our scrollPane object. This modification can be seen in the following line of code:

getContentPane().add(scrollPane);


JaVa
   
Comments