Results 1 to 10 of 10
- 01-27-2013, 03:10 PM #1
Member
- Join Date
- Aug 2011
- Posts
- 56
- Rep Power
- 0
Controll of many checkboxes created dynamically.
Hello
I create the code below in a Jframe. Like you can see i create 10 rows with some objects inside of them. the results is like the next picture.

I need a solution for the checking of a checkbox. In example when i press one of the check box from the 2 column (Rename) to enable the corresponds ltextField (Name) to change the content of it and when i deselect it to disable the correspond jtextfield(Name).
Or i need some thing that can tell me "You have selected the xx checkbox ";
Java Code:public JPanel playerPanelAdd(Integer numPlayers){ JPanel jp = new JPanel(); GridLayout eLayout = new GridLayout(0,5); jp.setLayout(eLayout); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { AbstractButton abstractButton = (AbstractButton) actionEvent.getSource(); boolean selected = abstractButton.getModel().isSelected(); System.out.println(selected); } }; ItemListener itemlistener = new ItemListener() { public void itemStateChanged(ItemEvent e) { System.out.println("itemlis="+e.paramString()); } }; jp.add(new JLabel("Name")); jp.add(new JLabel("Rename")); jp.add(new JLabel("Group")); jp.add(new JLabel("E-Greedy")); jp.add(new JLabel("Use It")); for (int i = 1; i <= numPlayers; i++) { JTextField jtxtFname = new JTextField("P" +i); jtxtFname.setEditable(false); JCheckBox jcbxRename = new JCheckBox("",false); jcbxRename.addActionListener(actionListener); jcbxRename.addItemListener(itemlistener); JTextField jtxtfGroup = new JTextField("1"); JTextField jtxtf = new JTextField("Player" + i); jtxtf.setText("0.1"); JCheckBox jcbx = new JCheckBox(); jcbx.setSelected(true); jp.add(jtxtFname); jp.add(jcbxRename); jp.add(jtxtfGroup); jp.add(jtxtf); jp.add(jcbx); } return jp; }
Thank you in advanced
-
Re: Controll of many checkboxes created dynamically.
Myself, I'd create a JTable for this sort of display.
- 01-27-2013, 03:21 PM #3
Member
- Join Date
- Aug 2011
- Posts
- 56
- Rep Power
- 0
Re: Controll of many checkboxes created dynamically.
When you say JTable.
Do you have any example ????
-
Re: Controll of many checkboxes created dynamically.
There are many examples of using JTables to be found on this forum and on other sites as well as well as the Java Swing tutorials.
- 01-27-2013, 03:51 PM #5
Member
- Join Date
- Aug 2011
- Posts
- 56
- Rep Power
- 0
Re: Controll of many checkboxes created dynamically.
ok i found some tutorials and examples.
but by dont changing my code is there any other solution ????
-
Re: Controll of many checkboxes created dynamically.
Your problem is simply one of obtaining the right references. You could link JTextFields to JCheckBoxes with a Map<JCheckBox, JTextField> or if both are held by an array or ArrayList, you could iterate through the JCheckBox array to find the index of the check box clicked and then use its selection to change the state of the JTextField held by the same index in its corresponding array or list, although I recommend against use of parallel arrays. Or you could create a class to hold both check box and text field,... there are many ways to skin this cat.
- 01-27-2013, 04:24 PM #7
Member
- Join Date
- Aug 2011
- Posts
- 56
- Rep Power
- 0
Re: Controll of many checkboxes created dynamically.
Ok i will use the method of table variables and the List.
I try to convert it as like the next code and it give me an error of (Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 10)
Java Code:int numPlayers = 10 JCheckBox button[]=new JCheckBox[numPlayers]; for (int i = 0; i <= button.length; i++) { button[i]= new JCheckBox(Integer.toString(i+1)); jp.add(button[i]); }
-
Re: Controll of many checkboxes created dynamically.
You've a problem here:
Since you loop continues while i <= button.length, then it will in fact loop when i = button.length, but the button array and all Java arrays are 0 based and go from 0 to length - 1, so this will cause the AIOOBE that you're seeing. Solution, loop til i < button.length not i <= button.length.Java Code:for (int i = 0; i <= button.length; i++) { //.... }
- 01-27-2013, 05:55 PM #9
Member
- Join Date
- Aug 2011
- Posts
- 56
- Rep Power
- 0
Re: Controll of many checkboxes created dynamically.
Thank you very much you are perfect
-
Similar Threads
-
How to add ajax behavior to dynamically created component?
By burferd in forum JavaServer Faces (JSF)Replies: 1Last Post: 05-31-2012, 07:02 AM -
Checkboxes and MS Access DB
By m_patten2 in forum AWT / SwingReplies: 2Last Post: 11-26-2010, 01:56 AM -
How can i execute one java file(dynamically created) from the other
By Rak239 in forum New To JavaReplies: 1Last Post: 11-03-2009, 07:07 AM -
How to create ActionListners for dynamically created MenuItem?
By anilshelar in forum AWT / SwingReplies: 3Last Post: 10-27-2009, 02:19 PM -
[SOLVED] Can variable names be dynamically created?
By CJSLMAN in forum New To JavaReplies: 4Last Post: 01-03-2009, 01:06 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks