• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JCheckBox

 
Martin Vietor
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to create 13 Checkboxes that set one of the booleans in a boolean[] true or false. This is not working at all. The first 3 lines in the for-loop are in their own method (createCheckBox), I just included them here for easy reading. I think the mistake is more basic than the question in which method I am doing what. Any ideas?



Thanks a bunch, M

EDIT:
The problem is not, that I get an Error in return, the problem is that ALL the booleans are set to true rather than just the one I want, the one belonging to my JCheckBox. Also I didn't put in all the parentheses, but like I said, it's running, that's not the issue.
[ December 01, 2008: Message edited by: Martin Vietor ]
 
Campbell Ritchie
Sheriff
Posts: 51349
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many boxes do you want? You appear to be creating 12 of them.

The action command and addActionListener(this) technique is hardly object-oriented programming. You ought to have a class which implements ActionListener and sets the members of the array from its actionPerformed method. With each box, you would pass the array and its index to the ActionListener constructor.

Sounds like something which would sit better on the Swing forum-moving.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you have code that doesn't work, so you type in code here that you think you have
generally doesn't help doing it that way - post the real code.

but if we're happy playing 20 guesses, mine is you have an extra ; after your if statement
if e.getActioncommand.equals(""+i);//<--here (in the real code)
 
Martin Vietor
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, no extra ;, all is well there. But you're right, I'll post the real code. Sitting at the wrong computer, don't have it here. It's a lot of code in several classes though, I'll try to make it comprehensible. Tomorrow. It's 10:30 pm in my world.

Thank you so far, M
 
Brian Cole
Author
Ranch Hand
Posts: 912
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Martin Vietor:
the problem is that ALL the booleans are set to true rather than just the one I want, the one belonging to my JCheckBox.


I'd have to concur with Mr. Dunn in that we need to see the actual code.

The code you have presented here is riddled with errors, but should essentially work.

Some here will argue that you shouldn't use the actionCommand property in this way, but I personally have no problem with it. There are little things I could quibble with, though.

  • When setting the actionCommand, I would generally prefer Integer.toString(i) to ""+i.
  • In the listener, I would prefer getting the index via Integer.parseInt(e.getActionCommand()) rather than looping through each possibility.
  • You are setting elements of your array to true when the user checks a box, but you are not setting them to false when the user unchecks a box. You may want to consider ((AbstractButton)e.getSource()).isSelected().

  •  
    Martin Vietor
    Ranch Hand
    Posts: 40
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you, Brian, I will try those. It seems they are just a more elegnat way of doing the same thing. While elegance in programming is important, and I am glad to have learned that code (had no idea about them), it doesn't solve my problem. So here's the update:

    The reason I am looping is because the enum and the array exist anyway. So I figured I'll use them that way. They were not created for the purpose of the checkboxes.

    I think I pretty much have everything set up. I put the CheckBoxCreation into a class and everything works just fine - until I try to male my Boxes actually DO something. Here's my code for the CheckBoxCreation:



    I can't hand over c and s to the entire class (maybe I'm tried it wrong) and if I hand it over just to CheckBoxCreator.create (like now) actionPerformed of course doesn't know what "c" is. I was hoping by handing over a boolean to the CheckBoxCreator.create method, the checkbox itself would remember it. Can I hand over a variable to actionPerformed?

    Is my question starting to make more sense?
     
    Michael Dunn
    Ranch Hand
    Posts: 4632
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    pass the array to CheckBoxCreator

    something like this

     
    Brian Cole
    Author
    Ranch Hand
    Posts: 912
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Michael Dunn:
    pass the array to CheckBoxCreator

    something like this



    In this case CheckBoxCreator's methods may refer to bools[] directly
    if desired, since CheckBoxCreator is a non-static nested class.

    Also, since each checkbox gets its own listener instance (instead
    of sharing a single listener as in the original post) there is no longer
    any need to encode the index into the actionCommand or name.
    For example:

    I prefer not to use the c = !c idiom in this kind of context when
    I can help it. GUIs sometimes do drop events, and c = !c is not
    self-correcting when it happens.
    [ December 04, 2008: Message edited by: Brian Cole ]
     
    Martin Vietor
    Ranch Hand
    Posts: 40
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you, first of all for you time and effort. I actually had my code set up that way at some point. Didn't compile it though, because I didn't think it would work. The part about cc=c then change c - wouldn't cc then stay unchanged? I'll have to give it a try, see how it ends up working. Thanks a lot.

    M
     
    Martin Vietor
    Ranch Hand
    Posts: 40
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you, figured it out. I needed a constructor to actually CHANGE the boolean[]:



    Thank you A LOT, it's working now.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic