Results 1 to 10 of 10
  1. #1
    ki_ha1984 is offline Member
    Join Date
    Aug 2011
    Posts
    74
    Rep Power
    0

    Default 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.

    Controll of many checkboxes created dynamically.-players.png

    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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    ki_ha1984 is offline Member
    Join Date
    Aug 2011
    Posts
    74
    Rep Power
    0

    Default Re: Controll of many checkboxes created dynamically.

    When you say JTable.

    Do you have any example ????

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  5. #5
    ki_ha1984 is offline Member
    Join Date
    Aug 2011
    Posts
    74
    Rep Power
    0

    Default 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 ????

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  7. #7
    ki_ha1984 is offline Member
    Join Date
    Aug 2011
    Posts
    74
    Rep Power
    0

    Default 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]);
    
            }

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Controll of many checkboxes created dynamically.

    You've a problem here:

    Java Code:
    for (int i = 0; i <= button.length; i++) {
     //....
    }
    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.

  9. #9
    ki_ha1984 is offline Member
    Join Date
    Aug 2011
    Posts
    74
    Rep Power
    0

    Default Re: Controll of many checkboxes created dynamically.

    Thank you very much you are perfect

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Controll of many checkboxes created dynamically.

    Quote Originally Posted by ki_ha1984 View Post
    Thank you very much you are perfect
    So my mother tells me as well. You're welcome!

Similar Threads

  1. How to add ajax behavior to dynamically created component?
    By burferd in forum JavaServer Faces (JSF)
    Replies: 1
    Last Post: 05-31-2012, 07:02 AM
  2. Checkboxes and MS Access DB
    By m_patten2 in forum AWT / Swing
    Replies: 2
    Last Post: 11-26-2010, 01:56 AM
  3. Replies: 1
    Last Post: 11-03-2009, 07:07 AM
  4. Replies: 3
    Last Post: 10-27-2009, 02:19 PM
  5. [SOLVED] Can variable names be dynamically created?
    By CJSLMAN in forum New To Java
    Replies: 4
    Last Post: 01-03-2009, 01:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •