• Register
Welcome to Kodlogs, programming questions and answer website.
0 votes
What is Java Swing JList , can you illustrate with examples?
by (1.6k points)  
reshown by

7 Answers

0 votes

Java JList

Create a new empty java application project

Create a new JFrame named ProductList

Design GUI with JComboBox, JList, edit text and change variable name for all objects

Modify model property of JComboxBox to clear all default items

Modify model property of JList to clear all default items

Create action event for Add button

Use addItem(Object item) method of JComboBox to add item

JList class declaration

Run the project and add some item to JComboBox

But no method for JList to add directly an item into it

So we must create an object of DefaultListModel class to contain its data

And use addElement(Object element) method of model object to add an item for JList

Use setModel(Model model) method of JList object to import all data into JList

Run the project and add some item to both JComboBox and JList simultaneously

Now create action event for Delete button

Use removeItemAt(int index) method of JComboBox object to remove an item at some index

Use getSelectedIndex method of JComboBox object to get the index when end-user choose any item

But JList doesn't have remove method to remove an item like JComboBox

So we must use removeElementAt(int index) method of model object instead to remove an item at some index of JList

And use getSelectedIndex method of JList object the same as JComboBox to get the index when end-user choose any item

Run the project, add some items, and then remove some

But when no choice in JList, and click Delete button, it throws an exception

And when the JComboBox is empty, we can't remove any more item remaining and it throws more exception

So now we must modify, use if to check condition when JComboBox and JList model are empty or not

If not empty, perform deleting selected item in both JComboBox and JList model

Otherwise, a message appears that "product list should not be empty"

Run the project and now, when the list or combobox is empty, a message appear

But when no choice in JList, it still throws an exception

So use try-catch block to catch exception and handle it

When exception occurs, the message "You must choose an item to delete!" will appear instead

Run the project and add some items, delete any, and everything is OK now!

by (9.8k points)  
0 votes

Java JList Example

  1. import javax.swing.*;  
  2. public class ListExample  
  3. {  
  4.      ListExample(){  
  5.         JFrame f= new JFrame();  
  6.         DefaultListModel<String> l1 = new DefaultListModel<>();  
  7.           l1.addElement("Item1");  
  8.           l1.addElement("Item2");  
  9.           l1.addElement("Item3");  
  10.           l1.addElement("Item4");  
  11.           JList<String> list = new JList<>(l1);  
  12.           list.setBounds(100,10075,75);  
  13.           f.add(list);  
  14.           f.setSize(400,400);  
  15.           f.setLayout(null);  
  16.           f.setVisible(true);  
  17.      }  
  18. public static void main(String args[])  
  19.     {  
  20.    new ListExample();  
  21.     }}  
by (9.8k points)  
0 votes

Some of Constructor for JList are :

  1. JList()
  2. JList(E [ ] l) :
  3. JList(ListModel d)
  4. JList(Vector l) : 
by (9.8k points)  
0 votes

Methods in JList are



setSelectedIndex(int i)

setSelectionBackground(Color c)

setSelectionForeground(Color c)

setListData(E [ ] l)

setVisibleRowCount(int v)

setSelectedValue(Object a, boolean s)

setSelectedIndices(int[] i)

setListData(Vector l)

setLayoutOrientation(int l)

setFixedCellWidth(int w)

setFixedCellHeight(int h)

isSelectedIndex(int i)

indexToLocation(int i)

getToolTipText(MouseEvent e)








addListSelectionListener(ListSelectionListener l)

by (9.8k points)  
0 votes
by (9.8k points)  
0 votes

Some commonly used methods of JList are:

public void addListSelectionListener(ListSelectionListener listener)

public void clearSelection()

public E getSelectedValue())

by (9.8k points)  
0 votes

Example of JList:

We are going to look at how to sort JList.

When user clicks the sort button,we sort our data which is an array of strings.

Our Main Class here extends javax,swing.Jframe.

The sortList method takes a javax,swing.Jlist object

We are going to javax.swing.ListModel object from our JList instance passed us.

We instantiate a string array,the size being the size of our DefaultListModel instance.

We then fill it with Random strings.

by (9.8k points)