Displaying a List of Checkboxes

Displaying a List of Checkboxes

All of the aggregate components in Swing–that is, components made up other components, such as JTable, JTree, or JComboBox–can be highly customized. For example, a JTable component normally displays a grid of JLabel components, but it can also display JButtons, JTextFields, or even other JTables. Getting these aggregate components to display non-default objects is the easy part, however. Making them respond properly to keyboard and mouse events is a much harder task, due to Swing’s separation of components into “renderers” and “editors.” This separation was (in my opinion) a poor design choice and only serves to complicate matters when trying to extend Swing components.

To see what I mean, try enhancing Swing’s JList component so that it displays checkboxes instead of labels. According to Swing philosophy, this task requires implementing two interfaces: ListCellRenderer (for drawing the checkboxes) and CellEditor (for handling keyboard and mouse events on the checkboxes). Implementing the ListCellRenderer interface is easy enough, but the CellEditor interface can be rather clumsy and hard to understand. In this particular case, I would suggest to forget CellEditor entirely and to handle input events directly, as shown in the following code.

 import javax.swing.*;import javax.swing.border.*;import java.awt.*;import java.awt.event.*;public class CheckBoxList extends JList{   protected static Border noFocusBorder =                                 new EmptyBorder(1, 1, 1, 1);   public CheckBoxList()   {      setCellRenderer(new CellRenderer());      addMouseListener(new MouseAdapter()         {            public void mousePressed(MouseEvent e)            {               int index = locationToIndex(e.getPoint());               if (index != -1) {                  JCheckBox checkbox = (JCheckBox)                              getModel().getElementAt(index);                  checkbox.setSelected(                                     !checkbox.isSelected());                  repaint();               }            }         }      );      setSelectionMode(ListSelectionModel.SINGLE_SELECTION);   }   protected class CellRenderer implements ListCellRenderer   {      public Component getListCellRendererComponent(                    JList list, Object value, int index,                    boolean isSelected, boolean cellHasFocus)      {         JCheckBox checkbox = (JCheckBox) value;         checkbox.setBackground(isSelected ?                 getSelectionBackground() : getBackground());         checkbox.setForeground(isSelected ?                 getSelectionForeground() : getForeground());         checkbox.setEnabled(isEnabled());         checkbox.setFont(getFont());         checkbox.setFocusPainted(false);         checkbox.setBorderPainted(true);         checkbox.setBorder(isSelected ?          UIManager.getBorder(           "List.focusCellHighlightBorder") : noFocusBorder);         return checkbox;      }   }}

Here, I intercept mouse clicks from the listbox and simulate a click on the appropriate checkbox. The result is a “CheckBoxList” component that is both simpler and smaller than an equivalent component using the CellEditor interface. To use the class, simply instantiate it, then pass it an array of JCheckBox objects (or subclasses of JCheckBox objects) by calling setListData. Note that the checkboxes in this component will not respond to keypresses (i.e. the spacebar), but you could always add your own key listener if needed.

Share the Post:
data observability

Data Observability Explained

Data is the lifeblood of any successful business, as it is the driving force behind critical decision-making, insight generation, and strategic development. However, due to its intricate nature, ensuring the

Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular