3

I am designing a user interface in which I want to give users the option to select one or more Trainers for a particular Course. Is there such a thing as a Checkboxlist in Java? I am using Netbeans.What do you recommend as a best UI solution.

Lion
  • 18,729
  • 22
  • 80
  • 110
unleashed
  • 331
  • 1
  • 5
  • 17
  • You can find the answer here: http://stackoverflow.com/questions/19766/how-do-i-make-a-list-with-checkboxes-in-java-swing – Mubin Jun 13 '13 at 21:49
  • I suppose your UI is in Swing ? – Toilal Jun 13 '13 at 21:49
  • I don't believe it is enough.I want something like a JComboBox with checkboxes beside the list items. – unleashed Jun 13 '13 at 21:51
  • Yes Toilal my UI is in Swing – unleashed Jun 13 '13 at 21:52
  • So what you want is merely a visual organization of the checkboxes? If thats the case then what you should do is just play around with the layout managers by adding the checkboxes to some "auto grouping" component such as a VBox in JavaFX (sorry I don't know the equivalent for Swing, never really used it for complex layouts) – 0x6C38 Jun 13 '13 at 21:56
  • ![enter image description here](http://i.stack.imgur.com/h46LO.jpg) If you want something like this, take a look [here](http://stackoverflow.com/questions/8848239/nimbus-l-f-setting-different-background-colors-of-a-check-boxes-in-a-checkbox). – aran Jun 13 '13 at 21:51
  • No there is no pre-defined component of such name, but you can implement listCellRender interface, for more description Here's [a link](http://www.devx.com/tips/Tip/5342)! – Saubar Jun 13 '13 at 21:56

2 Answers2

2

Found this: http://chianti.ucsd.edu/svn/csplugins/trunk/soc/jgao/IDMapping/src/csplugins/id/mapping/ui/CheckComboBox.java

It is an extension of JComboBox.

enter image description here

Here is a quick example I whipped up, but you get the picture:

import java.util.HashSet;
import java.util.Set;
import javax.swing.JFrame;

@SuppressWarnings("serial")
public class Test extends JFrame {

    public Test() {
        Set<Object> options = new HashSet<>();
        options.add(new Option<Integer>("One", 1));
        options.add(new Option<Integer>("Two", 2));
        options.add(new Option<Integer>("Three", 3));
        options.add(new Option<Integer>("Four", 4));
        CheckComboBox c = new CheckComboBox(options);
        this.add(c);
        this.setVisible(true);
        this.setLocationRelativeTo(null);
        this.pack();
    }

    public static void main(String[] args) {
        new Test();
    }


    private class Option<T> implements Comparable<T> {
        private String label;
        private T value;

        public Option(String label, T value) {
            this.label = label;
            this.value = value;
        }

        @Override
        public String toString() {
            return this.label;
        }

        @Override
        public int compareTo(T o) {
            // TODO Auto-generated method stub
            return 0;
        }
    }
}
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
0

Thank you for the quick responses as usual. I will be using a JTable with boolean fields instead. JTable seems to be a cleaner solution to me http://docs.oracle.com/javase/tutorial/uiswing/components/table.html

unleashed
  • 331
  • 1
  • 5
  • 17