Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By jim829

Thread: Create JCheckBoxes dynamically

  1. #1
    Ravendark is offline Member
    Join Date
    Mar 2014
    Posts
    9
    Rep Power
    0

    Default Create JCheckBoxes dynamically

    Hello,

    I am trying to write a program for a hairsalon, and I have a JDialog popup, where the user inputs the appropriate data for an appointment...
    This includes a scrollpane with all the available services the hairsalon has to offer. (Pic in Greek sorry)



    I create these checkboxes dynamically (using a for loop) after storing them in a Vector first

    Java Code:
    Vector<JCheckBox> checkboxVector = new Vector<JCheckBox>();
    for (int i = 0; i < services.femaleServices.length; i++) // femaleServices is a string array holding the services for ladies
    			checkboxVector.addElement(new JCheckBox(services.femaleServices[i]));
    		
    		for (int i = 0; i < checkboxVector.size(); i++)
    			servicesPanel.add(checkboxVector.elementAt(i));
    They are dynamically created for 2 reasons:
    1) Many different services
    2) User may add new ones over time...

    The problem is I cannot reference them.
    For example, if a lady wants to have a haircut and change her hair colour, 2 checkboxes will be checked...
    How can I know which ones are checked?

    Any ideas?

    Thank you in advance.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,786
    Rep Power
    5

    Default Re: Create JCheckBoxes dynamically

    Well, first off, I don't consider these dynamically created. You have just created some checkboxes in a loop (which is fine) and stored them in a vector. However, regarding your problem, I assume you have an ItemListener for your check boxes. You can use the source in combination with the name from the Item event to match the check to the appropriate box. For more info, check out http://docs.oracle.com/javase/tutori...ts/button.html

    Regards,
    Jim
    Last edited by jim829; 03-18-2014 at 04:25 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Ravendark is offline Member
    Join Date
    Mar 2014
    Posts
    9
    Rep Power
    0

    Default Re: Create JCheckBoxes dynamically

    Hi Jim and thank you for your reply.

    I don't quite follow the trick with the ItemListener.
    As I read, implementing an ItemListener, will catch changes and will throw them in the method ItemStateChanged();

    What troubles me though is that using this way for creating the checkboxes
    Java Code:
    checkboxVector.addElement(new JCheckBox(services.femaleServices[i]));
    I don't have a name for each checkbox.

    For example, creating a checkbox like this:

    Java Code:
    JCheckBox c = new JCheckBox()
    I can use an if clause to check if the "c" checkbox is selected or not.
    How can I have names for each checkbox?

    Regards,

    George

  4. #4
    Ravendark is offline Member
    Join Date
    Mar 2014
    Posts
    9
    Rep Power
    0

    Default Re: Create JCheckBoxes dynamically

    To make it clearer, I have a String array
    Java Code:
    public String[] femaleServices = {"Κούρεμα Κοντό", "Κούρεμα Μακρύ", "Χτένισμα Κοντό", "Χτένισμα Μακρύ", "Βαφή Κοντό", "Βαφή Μακρύ", "Ανταύγιες Κοντό", "Ανταύγιες Μακρύ", "Λούσιμο"};
    What I am trying to do, is to make JCheckBoxes out of this array...

    My approach seems to be not so good.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,786
    Rep Power
    5

    Default Re: Create JCheckBoxes dynamically

    There are lots of ways to do it. For example:

    Java Code:
          String [] service = {"perm", "dye", "haircut"};
          for (String s : service) {
             JCheckBox c = new JCheckBox(s);
             c.addItemListener(MyItemListener);
             panel.add(c);
          }
          // and later
          public void itemStateChanged(ItemEvent ie) {
             if (ie.getSource() instanceof JCheckBox) {
                JCheckBox cb = (JCheckBox)ie.getSource();
                switch (cb.getName()) {
                   case "perm": // give a perm
                      break;
                      //etc/
                }
             }
          }
    Note that you don't really need to save the check boxes in a vector. They are still around when you add them to the panel. You could also have a separate ItemListener for each checkbox. Then you don't need the switch statement. But you still need to know what item listener is listening for what service.

    Caveat: This was not compiled so there may be some errors but it gives the general idea.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Ravendark is offline Member
    Join Date
    Mar 2014
    Posts
    9
    Rep Power
    0

    Default Re: Create JCheckBoxes dynamically

    ok I am getting somewhere:

    I am adding the ItemListener for each checkbox.

    Java Code:
    for (int i = 0; i < checkboxVector.size(); i++) {
    			servicesPanel.add(checkboxVector.elementAt(i));
    			checkboxVector.elementAt(i).addItemListener(this); // this adds the ItemListener
    		}
    Now, how can I know which checkbox is selected?

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default Re: Create JCheckBoxes dynamically

    Quote Originally Posted by Ravendark View Post
    Now, how can I know which checkbox is selected?
    As Jim already showed: get the source of the event (with the getSource() method; it returns a JCheckBox) and get its text. Given the text (a simple String), figure out what to do ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Ravendark is offline Member
    Join Date
    Mar 2014
    Posts
    9
    Rep Power
    0

    Default Re: Create JCheckBoxes dynamically

    OK I figured it out. Thank you for your replies

    Getting the source of the event with getSource(), returns null.
    I can use the setName() method to give each JCheckBox a name:

    Java Code:
    for (int i = 0; i < checkboxVector.size(); i++) {
    			servicesPanel.add(checkboxVector.elementAt(i));
    			checkboxVector.elementAt(i).addItemListener(this);
    			checkboxVector.elementAt(i).setName(services.femaleServices[i]);
    		}
    and this returns me the selected names of the checkboxes:

    Java Code:
    public void itemStateChanged(ItemEvent e) {
    		if (e.getStateChange() == ItemEvent.SELECTED) {
    			JCheckBox temp = (JCheckBox)e.getSource();
    			String name = temp.getName();
    			System.out.println(name);
    		}
    	}

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,786
    Rep Power
    5

    Default Re: Create JCheckBoxes dynamically

    Like I said, I didn't compile it. It should be cb.getText() and not cb.getName(). But your way is fine too.

    Regards,
    Jim
    Ravendark likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Possible to dynamically create strings?
    By ajrobson in forum New To Java
    Replies: 2
    Last Post: 12-10-2012, 06:59 AM
  2. Dynamically create objects, set value and call
    By buntyindia in forum New To Java
    Replies: 5
    Last Post: 05-25-2011, 06:59 PM
  3. Dynamically create a button, but what happened?
    By love2java in forum AWT / Swing
    Replies: 1
    Last Post: 02-17-2008, 01:01 AM
  4. How to create widgets dynamically
    By sarbuland in forum Advanced Java
    Replies: 0
    Last Post: 02-06-2008, 09:08 PM

Posting Permissions

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