Introduction
In this article we are going to describe the implementation of an ItemChooser in the Java language. In Java with the help of JList, JRadioButton and JComboBox, class and ItemListener, SelectItemListener inertface we are trying to make a simple ItemChoosher GUI applictaion. Mostly ItemChooser is seen in a window form application and another in a registration form in a website. In the following example we are using a Swing component that presents a choice to the user. It allows the choice to be presented in a JList, in a JComboBox, or with a bordered group of JRadioButton components. Additionally, it displays the name of the choice with a JLabel. It allows an arbitrary value to be associated with each possible choice. Note that this component only allows one item to be selected at a time. Multiple selections are not supported.
Note : ItemChooser is not a class in Java. In this example we use JList as described in a previous article so there is no need to describe that here.
ListSelectionListener
This is the interface for handling the select List item and it is found in the Subpackage event within the swing package. It has only one method, valueChanged(ListSelectionEvent e) called whenever the value of the selection changes.
ItemListener
This is the interface for handling the select List item. When an item-selection event occurs, the listener object's itemStateChanged
method is invoked. And your class which produced item event must be implemented by this interface.
ActionListener
The Action listener is used for a mouse generated event. When the action event occurs, that object's actionPerformed(ActionEvent e) Is invoked when an action occurs. Your class in which you want to handle the action event of that class must be implemented by this interface.
Code
import java.util.ArrayList;
import java.util.Iterator;
import java.awt.Container;
import java.awt.BorderLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class ItemChooserDemo extends JPanel
{
String name;
String[] labels;
Object[] values;
int selection;
int presentation;
public static final int LIST = 1;
public static final int COMBOBOX = 2;
public static final int RADIOBUTTONS = 3;
JList list;
JComboBox combobox;
JRadioButton[] radiobuttons;
ArrayList listeners = new ArrayList();
public ItemChooser(String name, String[] labels, Object[] values,
int defaultSelection, int presentation) {
this.name = name;
this.labels = labels;
this.values = values;
this.selection = defaultSelection;
this.presentation = presentation;
if (values == null)
this.values = labels;
switch(presentation) {
case LIST:
initList();
break;
case COMBOBOX:
initComboBox();
break;
case RADIOBUTTONS:
initRadioButtons();
break;
}
}
void initList() {
list = new JList(labels);
list.setSelectedIndex(selection);
list.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
ItemChooser.this.select(list.getSelectedIndex());
}
}
);
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
this.add(new JLabel(name));
this.add(new JScrollPane(list));
}
void initComboBox() {
combobox = new JComboBox(labels);
combobox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
ItemChooser.this.select(combobox.getSelectedIndex());
}
});
this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
this.add(new JLabel(name));
this.add(combobox);
}
void initRadioButtons() {
radiobuttons = new JRadioButton[labels.length];
ButtonGroup radioButtonGroup = new ButtonGroup();
ChangeListener listener = new ChangeListener() {
public void stateChanged(ChangeEvent e) {
JRadioButton b = (JRadioButton) e.getSource();
if (b.isSelected()) {
for (int i = 0; i < radiobuttons.length; i++) {
if (radiobuttons[i] == b) {
ItemChooser.this.select(i);
return;
}
}
}
}
};
this.setBorder(new TitledBorder(new EtchedBorder(), name));
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
for (int i = 0; i < labels.length; i++) {
radiobuttons[i] = new JRadioButton(labels[i]);
if (i == selection)
radiobuttons[i].setSelected(true);
radiobuttons[i].addChangeListener(listener);
radioButtonGroup.add(radiobuttons[i]);
this.add(radiobuttons[i]);
}
}
public String getName() {
return name;
}
public int getPresentation() {
return presentation;
}
public String[] getLabels() {
return labels;
}
public Object[] getValues() {
return values;
}
public Object getSelectedValue() {
return values[selection];
}
public void setSelectedIndex(int selection) {
switch (presentation) {
case LIST:
list.setSelectedIndex(selection);
break;
case COMBOBOX:
combobox.setSelectedIndex(selection);
break;
case RADIOBUTTONS:
radiobuttons[selection].setSelected(true);
break;
}
this.selection = selection;
}
protected void select(int selection) {
this.selection = selection;
if (!listeners.isEmpty()) {
ItemChooser.Event e = new ItemChooser.Event(this, selection,values[selection]);
for (Iterator i = listeners.iterator(); i.hasNext();) {
ItemChooser.Listener l = (ItemChooser.Listener) i.next();
l.itemChosen(e);
}
}
}
public void addItemChooserListener(ItemChooser.Listener l) {
listeners.add(l);
}
public void removeItemChooserListener(ItemChooser.Listener l) {
listeners.remove(l);
}
public static class Event extends java.util.EventObject {
int selectedIndex;
Object selectedValue;
public Event(ItemChooser source, int selectedIndex, Object selectedValue) {
super(source);
this.selectedIndex = selectedIndex;
this.selectedValue = selectedValue;
}
public ItemChooser getItemChooser() {
return (ItemChooser) getSource();
}
public int getSelectedIndex() {
return selectedIndex;
}
public Object getSelectedValue() {
return selectedValue;
}
}
public interface Listener extends java.util.EventListener {
public void itemChosen(ItemChooser.Event e);
}
public static void main(String[] args) {
final JFrame frame = new JFrame("ItemChooser Demo");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
final JLabel msgline = new JLabel(" ");
args = new String[]{"MCA","B-tech","MBBS"};
JPanel chooserPanel = new JPanel();
final ItemChooser c1 = new ItemChooser("1-You Select", args, null, 0,ItemChooser.LIST);
final ItemChooser c2 = new ItemChooser("2-You Select", args, null, 0,ItemChooser.COMBOBOX);
final ItemChooser c3 = new ItemChooser("3-You Select", args, null, 0,ItemChooser.RADIOBUTTONS);
ItemChooser.Listener l = new ItemChooser.Listener() {
public void itemChosen(ItemChooser.Event e) {
msgline.setText(e.getItemChooser().getName() + ": "+ e.getSelectedIndex() + ": " + e.getSelectedValue());
}
};
c1.addItemChooserListener(l);
c2.addItemChooserListener(l);
c3.addItemChooserListener(l);
JButton report = new JButton("Report");
report.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String msg = "<html><i>" + c1.getName() + ": " + c1.getSelectedValue() + "<br>" + c2.getName() + ": " + c2.getSelectedValue() + "<br>" + c3.getName() + ": " + c3.getSelectedValue() + "</i>";
JOptionPane.showMessageDialog(frame, msg);
}
});
chooserPanel.add(c1);
chooserPanel.add(c2);
chooserPanel.add(c3);
chooserPanel.add(report);
Container contentPane = frame.getContentPane();
contentPane.add(chooserPanel, BorderLayout.CENTER);
contentPane.add(msgline, BorderLayout.SOUTH);
frame.pack();
frame.show();
}
}
OUTPUT
cmd output
Initial output
This output shows how you select an item.
When you click on report. The following image will be displayed.
Resources