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:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS