The following example illustrates using JList in Swing. This example covers all core methods of javax.swing.JList class.
import javax.swing.*;
import java.awt.*;
import java.util.*;
class JListExample extends JFrame
{
JList<String> l1,l2,l3,l4;
JScrollPane s1,s2,s3,s4;
final String data[]={"Apple","Apple","Mango","Orange","Banana"};
DefaultListModel<String> model=new DefaultListModel<>();
Vector<String> v=new Vector<>();
Dimension d=new Dimension(100,75);
public JListExample()
{
createAndShowGUI();
}
private void createAndShowGUI()
{
setTitle("JList Example in Swing");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
// A model contains data, it is a Vector implementation
// Add items to model
model.addElement("Vanilla");
model.addElement("Strawberry");
model.addElement("Chocolate");
model.addElement("Butterscotch");
// Add items to vector
v.add("Red");
v.add("Green");
v.add("Blue");
v.add("White");
v.add("Black");
// Create an empty list
l1=new JList<>();
// Create a list of String[] data
// An array of 'any' type is supported
l2=new JList<>(data);
// Create a list with given model
l3=new JList<>(model);
// Create a list containing items in
// the given vector
l4=new JList<>(v);
// Add some data to l1
// You can also send 'v' (overloaded version)
l1.setListData(data);
// Set some preferred size
l1.setPreferredSize(d);
l2.setPreferredSize(d);
l3.setPreferredSize(d);
l4.setPreferredSize(d);
// Set horizontal wrap
l1.setLayoutOrientation(JList.HORIZONTAL_WRAP);
// Set selection mode
// default is MULTIPLE_INTERVAL_SELECTION
// Multi select means the user can select multiple items
// on ctrl+click
l1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//l2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// Get selection mode
System.out.println(l1.getSelectionMode());
System.out.println(l3.getSelectionMode());
// Set selected index
l2.setSelectedIndex(1);
// Get selected index
System.out.println("Selected index l2 is "+l2.getSelectedIndex());
// Set selected item (case-sensitive)
// Scroll to the value, to make it visible
// here true and false is same, because of few items
// first Apple will be selected
l1.setSelectedValue("Apple",true);
// Get selected item
System.out.println("Selected item in l1 is "+l1.getSelectedValue());
// Select multiple indices
l3.setSelectedIndices(new int[]{1,3});
l2.setSelectedIndices(new int[]{3,1});
// Set selection interval
// select from 3rd index to 1st index
// so that 1st index will be lead index
l4.setSelectionInterval(2,1);
// Add a selection interval
// now lead index is updated from 1 to 4
l4.addSelectionInterval(3,4);
// Lead selection index is index of currently focused item
System.out.println("Lead selection index for l2 "+l2.getLeadSelectionIndex());
System.out.println("Lead selection index for l4 "+l4.getLeadSelectionIndex());
// Now remove the selection interval
// now 3 will become lead selection index
l4.removeSelectionInterval(2,3);
System.out.println("Lead selection index for l4 "+l4.getLeadSelectionIndex());
// Now clear selection
l2.clearSelection();
System.out.println("Is l2 selection empty? "+l2.isSelectionEmpty());
System.out.println("Lead selection index for l2 "+l2.getLeadSelectionIndex());
// Get minimum,maximum selected index
System.out.println("Minimum selected index for l3 is "+l3.getMinSelectionIndex());
System.out.println("Maximum selected index for l3 is "+l3.getMaxSelectionIndex());
// Returns -1 because selection is empty
System.out.println("Minimum selected index for l2 is "+l2.getMinSelectionIndex());
// Get first and last visible index
System.out.println("First visible index is "+l4.getFirstVisibleIndex());
System.out.println("Last visible index is "+l4.getLastVisibleIndex());
// Get selected indices
int indices[]=l3.getSelectedIndices();
for(int k:indices) System.out.print(k+" ");
System.out.println();
// Get selected values
// method getSelectedValues() is deprecated and replaced
// by this method
java.util.List<String> sval=l3.getSelectedValuesList();
for(int i=0;i<sval.size();i++) System.out.print(sval.get(i)+" ");
// Get item at given index
System.out.println("\nItem at index 2 in l1 is "+l1.getModel().getElementAt(2));
// Set a selection background,foreground
l3.setSelectionBackground(Color.GRAY);
l3.setSelectionForeground(Color.WHITE);
// Create scroll panes for every JList
s1=new JScrollPane(l1);
s2=new JScrollPane(l2);
s3=new JScrollPane(l3);
s4=new JScrollPane(l4);
// Add all items
add(s1);
add(s2);
add(s3);
add(s4);
setSize(400,400);
setVisible(true);
}
public static void main(String args[])
{
new JListExample();
}
}
Also see adding and removing elements dynamically in JList
No comments:
Post a Comment