Is there a way to add dynamically items of type JCheckBox
in Java as in JComboBox
we use addItem
method?
Asked
Active
Viewed 137 times
0

Andrew Thompson
- 168,117
- 40
- 217
- 433

alessandrob
- 1,605
- 4
- 20
- 23
-
why do you want to add "item" to JCheckbox? – Maxim Shoustin Sep 19 '13 at 08:54
-
Do you perhaps mean adding buttons to a button group? – kiheru Sep 19 '13 at 08:55
-
What i want to do is, after selected something in my program, generate a number of other choices depending on that selection, so i've thought to jCheckList, because i want to choose more than one option. – alessandrob Sep 19 '13 at 08:57
-
2There are some tricks to "adding components at run-time". The [nested layout example](http://stackoverflow.com/a/5630271/418556) shows how to adds labels dynamically. – Andrew Thompson Sep 19 '13 at 09:13
-
1*"..because i want to choose more than one option."* Use a `JList`. – Andrew Thompson Sep 19 '13 at 09:15
-
`JList` is ok but i don't know if there's the possibility to add ticks, instead of selecting more items by pressing CTRL button on keyboard – alessandrob Sep 19 '13 at 09:32
-
1*"`JList` is ok but i don't know if there's the possibility to add ticks"* There is. It can be done in the rendering component. – Andrew Thompson Sep 19 '13 at 09:41
-
An accepted answer is all the thanks I need. ;) – Andrew Thompson Sep 19 '13 at 10:03
-
What, are you ignoring me now? – Andrew Thompson Sep 19 '13 at 11:28
-
No, but i can't also add new elements to JList :) – alessandrob Sep 19 '13 at 11:46
2 Answers
1
Something like this may be effective if you want to add multiple items to another Component:
List<Component> myList = new Arraylist<Component>() //List for storage
Item myItem = new Item(); //New component
myList.add(myItem); //Store all the components to add in the list
for(int i = 0; i < myList.size; i++){
myjCheckBox.add(myList[i]); //Add all items from list to jCheckBox
}
The above example uses this method inherited in jCheckBox and should be able to provide what you need
Hope it helps!

Levenal
- 3,796
- 3
- 24
- 29
-
Is there also some way to take account of items selected in the JCheckbox then? – alessandrob Sep 19 '13 at 09:11
-
Do you mean something like storing the items selected from your JCheckBox into a list also? – Levenal Sep 19 '13 at 09:31
-
Yes, i want to store items selected in my JCheckBox and reuse on my program – alessandrob Sep 19 '13 at 09:36
-
Your going to need to add a listener, I suggest you start here http://www.java2s.com/Tutorial/Java/0240__Swing/CheckifaJCheckBoxisselectedinitsitemchangelistener.htm :) – Levenal Sep 19 '13 at 10:10
1
Note you might use actual check-boxes for the rendering component, but this was a few lines shorter.
import java.awt.*;
import javax.imageio.ImageIO;
import javax.swing.*;
class JCheckList {
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
JPanel gui = new JPanel(new BorderLayout());
JLabel l = new JLabel("Ctrl/shift click to select multiple");
gui.add(l, BorderLayout.PAGE_START);
JList<String> list = new JList<String>(
ImageIO.getReaderFileSuffixes());
list.setCellRenderer(new CheckListCellRenderer());
gui.add(list, BorderLayout.CENTER);
JOptionPane.showMessageDialog(null, gui);
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency
SwingUtilities.invokeLater(r);
}
}
class CheckListCellRenderer extends DefaultListCellRenderer {
String checked = new String(Character.toChars(9745));
String unchecked = new String(Character.toChars(9746));
@Override
public Component getListCellRendererComponent(
JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
Component c = super.getListCellRendererComponent(
list,value,index,isSelected,cellHasFocus);
if (c instanceof JLabel) {
JLabel l = (JLabel)c;
String s = (isSelected ? checked : unchecked) + (String)value;
l.setText(s);
}
return c;
}
}

Andrew Thompson
- 168,117
- 40
- 217
- 433