Implementation of ItemChooser in Java

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 interface we are trying to make a simple ItemChoosher GUI application. 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
  1. import java.util.ArrayList;  
  2. import java.util.Iterator;  
  3. import java.awt.Container;  
  4. import java.awt.BorderLayout;  
  5. import java.awt.event.ItemEvent;  
  6. import java.awt.event.ActionEvent;  
  7. import java.awt.event.ActionListener;  
  8. import java.awt.event.ItemListener;  
  9. import java.awt.event.WindowAdapter;  
  10. import java.awt.event.WindowEvent;  
  11. import javax.swing.JFrame;  
  12. import javax.swing.JLabel;  
  13. import javax.swing.JList;  
  14. import javax.swing.JPanel;  
  15. import javax.swing.BoxLayout;  
  16. import javax.swing.ButtonGroup;  
  17. import javax.swing.JButton;  
  18. import javax.swing.JComboBox;  
  19. import javax.swing.JScrollPane;  
  20. import javax.swing.JOptionPane;  
  21. import javax.swing.JRadioButton;  
  22. import javax.swing.border.EtchedBorder;  
  23. import javax.swing.border.TitledBorder;  
  24. import javax.swing.event.ChangeEvent;  
  25. import javax.swing.event.ChangeListener;  
  26. import javax.swing.event.ListSelectionEvent;  
  27. import javax.swing.event.ListSelectionListener;  
  28.   
  29. public class ItemChooserDemo extends JPanel  
  30. {  
  31.  String name;  
  32.  String[] labels;  
  33.  Object[] values;  
  34.  int selection;  
  35.  int presentation;  
  36.  public static final int LIST = 1;  
  37.  public static final int COMBOBOX = 2;  
  38.  public static final int RADIOBUTTONS = 3;  
  39.  JList list;  
  40.  JComboBox combobox;  
  41.  JRadioButton[] radiobuttons;  
  42.  ArrayList listeners = new ArrayList();  
  43.  public ItemChooser(String name, String[] labels, Object[] values,  
  44.   int defaultSelection, int presentation) {  
  45.   this.name = name;  
  46.   this.labels = labels;  
  47.   this.values = values;  
  48.   this.selection = defaultSelection;  
  49.   this.presentation = presentation;  
  50.   if (values == null)  
  51.    this.values = labels;  
  52.   switch (presentation) {  
  53.    case LIST:  
  54.     initList();  
  55.     break;  
  56.    case COMBOBOX:  
  57.     initComboBox();  
  58.     break;  
  59.    case RADIOBUTTONS:  
  60.     initRadioButtons();  
  61.     break;  
  62.   }  
  63.  }  
  64.  void initList() {  
  65.   list = new JList(labels);  
  66.   list.setSelectedIndex(selection);  
  67.   list.addListSelectionListener(new ListSelectionListener() {  
  68.    public void valueChanged(ListSelectionEvent e) {  
  69.     ItemChooser.this.select(list.getSelectedIndex());  
  70.    }  
  71.   });  
  72.   this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));  
  73.   this.add(new JLabel(name));  
  74.   this.add(new JScrollPane(list));  
  75.  }  
  76.  void initComboBox() {  
  77.   combobox = new JComboBox(labels);  
  78.   combobox.addItemListener(new ItemListener() {  
  79.    public void itemStateChanged(ItemEvent e) {  
  80.     ItemChooser.this.select(combobox.getSelectedIndex());  
  81.    }  
  82.   });  
  83.   this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));  
  84.   this.add(new JLabel(name));  
  85.   this.add(combobox);  
  86.  }  
  87.  void initRadioButtons() {  
  88.   radiobuttons = new JRadioButton[labels.length];  
  89.   ButtonGroup radioButtonGroup = new ButtonGroup();  
  90.   ChangeListener listener = new ChangeListener() {  
  91.    public void stateChanged(ChangeEvent e) {  
  92.     JRadioButton b = (JRadioButton) e.getSource();  
  93.     if (b.isSelected()) {  
  94.      for (int i = 0; i < radiobuttons.length; i++) {  
  95.       if (radiobuttons[i] == b) {  
  96.        ItemChooser.this.select(i);  
  97.        return;  
  98.       }  
  99.      }  
  100.     }  
  101.    }  
  102.   };  
  103.   this.setBorder(new TitledBorder(new EtchedBorder(), name));  
  104.   this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));  
  105.   for (int i = 0; i < labels.length; i++) {  
  106.    radiobuttons[i] = new JRadioButton(labels[i]);  
  107.    if (i == selection)  
  108.     radiobuttons[i].setSelected(true);  
  109.    radiobuttons[i].addChangeListener(listener);  
  110.    radioButtonGroup.add(radiobuttons[i]);  
  111.    this.add(radiobuttons[i]);  
  112.   }  
  113.  }  
  114.  public String getName() {  
  115.   return name;  
  116.  }  
  117.  public int getPresentation() {  
  118.   return presentation;  
  119.  }  
  120.  public String[] getLabels() {  
  121.   return labels;  
  122.  }  
  123.  public Object[] getValues() {  
  124.   return values;  
  125.  }  
  126.  public Object getSelectedValue() {  
  127.   return values[selection];  
  128.  }  
  129.  public void setSelectedIndex(int selection) {  
  130.   switch (presentation) {  
  131.    case LIST;  
  132.     list.setSelectedIndex(selection);  
  133.     break;  
  134.    case COMBOBOX:  
  135.     combobox.setSelectedIndex(selection);  
  136.     break;  
  137.    case RADIOBUTTONS:  
  138.     radiobuttons[selection].setSelected(true);  
  139.     break;  
  140.   }  
  141.   this.selection = selection;  
  142.  }  
  143.  protected void select(int selection) {  
  144.   this.selection = selection;  
  145.   if (!listeners.isEmpty()) {  
  146.    ItemChooser.Event e = new ItemChooser.Event(this, selection, values[selection]);  
  147.    for (Iterator i = listeners.iterator(); i.hasNext();) {  
  148.     ItemChooser.Listener l = (ItemChooser.Listener) i.next();  
  149.     l.itemChosen(e);  
  150.    }  
  151.   }  
  152.  }  
  153.  public void addItemChooserListener(ItemChooser.Listener l) {  
  154.   listeners.add(l);  
  155.  }  
  156.  public void removeItemChooserListener(ItemChooser.Listener l) {  
  157.   listeners.remove(l);  
  158.  }  
  159.  public static class Event extends java.util.EventObject {  
  160.   int selectedIndex;  
  161.   Object selectedValue;  
  162.   public Event(ItemChooser source, int selectedIndex, Object selectedValue) {  
  163.    super(source);  
  164.    this.selectedIndex = selectedIndex;  
  165.    this.selectedValue = selectedValue;  
  166.   }  
  167.   public ItemChooser getItemChooser() {  
  168.    return (ItemChooser) getSource();  
  169.   }  
  170.   public int getSelectedIndex() {  
  171.    return selectedIndex;  
  172.   }  
  173.   public Object getSelectedValue() {  
  174.   
  175.    return selectedValue;  
  176.   
  177.   }  
  178.   
  179.  }  
  180.  public interface Listener extends java.util.EventListener {  
  181.   public void itemChosen(ItemChooser.Event e);  
  182.  }  
  183.  public static void main(String[] args) {  
  184.   final JFrame frame = new JFrame("ItemChooser Demo");  
  185.   frame.addWindowListener(new WindowAdapter() {  
  186.    public void windowClosing(WindowEvent e) {  
  187.     System.exit(0);  
  188.    }  
  189.   });  
  190.   final JLabel msgline = new JLabel(" ");  
  191.   args = new String[] {  
  192.    "MCA",  
  193.    "B-tech",  
  194.    "MBBS"  
  195.   };  
  196.   JPanel chooserPanel = new JPanel();  
  197.   final ItemChooser c1 = new ItemChooser("1-You Select", args, null0, ItemChooser.LIST);  
  198.   final ItemChooser c2 = new ItemChooser("2-You Select", args, null0, ItemChooser.COMBOBOX);  
  199.   final ItemChooser c3 = new ItemChooser("3-You Select", args, null0, ItemChooser.RADIOBUTTONS);  
  200.   ItemChooser.Listener l = new ItemChooser.Listener() {  
  201.    public void itemChosen(ItemChooser.Event e) {  
  202.     msgline.setText(e.getItemChooser().getName() + ": " + e.getSelectedIndex() + ": " + e.getSelectedValue());  
  203.    }  
  204.   };  
  205.   c1.addItemChooserListener(l);  
  206.   c2.addItemChooserListener(l);  
  207.   c3.addItemChooserListener(l);  
  208.   JButton report = new JButton("Report");  
  209.   report.addActionListener(new ActionListener() {  
  210.    public void actionPerformed(ActionEvent e) {  
  211.     String msg = "<html><i>" + c1.getName() + ": " + c1.getSelectedValue() + "<br>" + c2.getName() + ": " + c2.getSelectedValue() + "<br>" + c3.getName() + ": " + c3.getSelectedValue() + "</i>";  
  212.     JOptionPane.showMessageDialog(frame, msg);  
  213.    }  
  214.   });  
  215.   chooserPanel.add(c1);  
  216.   chooserPanel.add(c2);  
  217.   chooserPanel.add(c3);  
  218.   chooserPanel.add(report);  
  219.   Container contentPane = frame.getContentPane();  
  220.   contentPane.add(chooserPanel, BorderLayout.CENTER);  
  221.   contentPane.add(msgline, BorderLayout.SOUTH);  
  222.   frame.pack();  
  223.   frame.show();  
  224.  }  
  225. }   
OUTPUT
 
cmd output
 
 image4.gif
 
Initial output
 
image1.gif
 
This output shows how you select an item.
 
image2.gif
 
When you click on the report. The following image will be displayed.
 
image-3.gif
 
Resources
 


Similar Articles