views:

633

answers:

2

Hi all,

Been a while since I posted on here, and now I am at a point where I find myself stuck yet again.

For my assignment at uni, we have been asked to create an address book application. It is going well, and it's nearly completed, however one part of the project, which is purely my preference, eludes me. I'm not asking for help on any part of the assignment which will gain me marks, just to be clear. I can upload the spec if anyone would like evidence of that. (/disclosure)

Part of the application is importing and exporting from a custom file type and the vcard format. This works fine. The custom file type holds multiple contacts, with only 4 lines per contact. Because it holds multiple contacts, a new window opens which asks the user to select the contacts they wish to be exported to the file. This contains a JList, where the user can select the contacts and export the file, no problem.

What I have been trying to do, is create a custom cell renderer which will allow the use of a check box in the JList. I nearly got this working by looking here (http://java.sun.com/docs/books/tutorial/uiswing/components/combobox.html#renderer) and here (http://www.jroller.com/santhosh/entry/jlist%5Fwith%5Fcheckboxes), however no luck. The first link explains how to create the custom cell renderer, but not check boxs, and the second explains how to do check boxs. I can get check boxs to display, however then that is ALL that displays.

In the latter example, the box's themselves must be clicked in order for the members of the list to be selected. What I would like to be able to do, is to allow the user to click on either the list items or the check box's to select the item.

In the version of just check boxs that I got working, the selection worked, however ctrl still had to be held down for multiple items to be selected. Is there any way to make it so ctrl is not needed for multiple selection of items? I know you can read modifiers on a click, but no idea if you can apply them to a click of a specified object.

Your thoughts, comments, suggestions, links and exact answers are all very welcome! :)

A: 

I would use a JTable and add the checkbox as a separate column.

You could then add a MouseListener to the table and whenever the other column is checked you toggle the value of the checkbox.

camickr
Yeh, seems so simple now you say it. No idea why I didnt think of that.Still, it says you should be able to create a custom cell renderer which contains a checkbox.I will try this idea tomorrw.Still want to hear what others have to say.
Relequestual
A: 

This does it, sort of; though it's essentially faked because the checkboxes don't really do anything, yet checking them does select the list item.

import java.awt.Component;
import java.awt.FlowLayout;

import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;


public class Relequestual extends JFrame {

   private final FunkyCellRendererComponent funkyCRC = new FunkyCellRendererComponent();

   public Relequestual() {
      getContentPane().setLayout(new FlowLayout());

      String[] items = { "Item 1", "Item 2", "Item 3", "Item 4" };
      JList theList = new JList(items);
      theList.setCellRenderer(
      new ListCellRenderer() {
         @Override
         public Component getListCellRendererComponent(JList list, Object value, int index,
               boolean isSelected, boolean cellHasFocus) {
            funkyCRC.setup(list, value, index, isSelected, cellHasFocus);
            return funkyCRC;
         }
      });
      getContentPane().add(theList);

   }

   /**
    * @param args
    */
   public static void main(String[] args) {
      Relequestual gui = new Relequestual();
      gui.pack();
      gui.setVisible(true);
   }

   class FunkyCellRendererComponent extends JPanel {

      private JCheckBox checkBox = new JCheckBox();
      private JLabel label = new JLabel();
      private JList list;
      private int index;

      public FunkyCellRendererComponent() {
         super(null);
         setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
         add(this.checkBox);
         add(this.label);
      }

      public void setup(JList list, Object value, int index,
               boolean isSelected, boolean cellHasFocus) {
         this.list = list;
         this.index = index;
         this.label.setText((String) list.getModel().getElementAt(index)); 
         if (isSelected) {
            setForeground(list.getSelectionForeground());
            setBackground(list.getSelectionBackground());
         } else {
            setForeground(list.getForeground());
            setBackground(list.getBackground());
         }
         this.checkBox.setSelected(isSelected);
      }

   }

}
Carl Smotricz
By your description, that's exactly what I want to do, as I already have a method for finding the selected items in the list. Will check it out in the morning.
Relequestual