wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Using same checkbox multiple times in an applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Using same checkbox multiple times in an applet" Watch "Using same checkbox multiple times in an applet" New topic
Author

Using same checkbox multiple times in an applet

Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Hello.

I am making an applet where i am using 2 check boxes after every textfields. These check boxes converts weight into gram or kilogram, depending upon the selection by the user. I will be using a different class for that conversion as i need this functionality in many applets.

I am having about 3 textfields in an applet.





Check boxes :



now, the problem i am facing is that i can't use the same check box after every textfield, like :



What should i do?






kunal
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
What prevents you from instantiating two more JCheckBox objects?
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Is there any other way out?

If there isn't, i will have to use about 10check boxes in an applet. All with same functionality, i mean gram and kg
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Hard to say, as it's not clear from the limited details you provide what exactly the code is doing (or how a checkbox represents "gram" or "kg"). 10 checkboxes would be appropriate if there are 10 different boolean values that the user can select. I can't tell from your posts whether that is the case here.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18655
    
    8

The bottom line is that you can't add a component more than once to a container. And you're trying to add a component twice.

You haven't said whether the two copies of the component are supposed to represent a single choice, or two different choices. In the former case, you should redesign your GUI so the the single choice appears in a single place, and then just add the component once to put it in that place. In the latter case, you should create two different components to deal with the two separate choices.

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Is there any other way out?
> If there isn't, i will have to use about 10check boxes in an applet. All with same functionality, i mean gram and kg

perhaps a JTable, setting the renderer/editor claasses for the columns, then adding the number of rows required
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Thanks all of you for your replies.

Paul, can you please elaborate how to redesign my GUI so that the single choice appears in a single place, and then just add the component once to put it in that place

Tim, there will be 2 check boxes namely "gram" & "kg" after each text field. If user checks 'gram', the value entered represents the weight in gram. Similar for kg. Similarly for all textfields. After taking all textfields in consideration (with gram & kg), i will derive a formula (will 1st convert all into same unit, either kg or gram)for calculating average and some other things.
Hope i am able to explain the problem.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18655
    
    8

Kunal Lakhani wrote:Paul, can you please elaborate how to redesign my GUI so that the single choice appears in a single place, and then just add the component once to put it in that place


Not really. Especially since you didn't say anything about what it looks like now. And also because after you asked that you went on and asked another question which suggested that you didn't want to do that anyway. Why should I spend any time trying to explain something which you don't need to do?

My preference is to design the GUI before I start programming it. You should aim to do that too. So let's have the answer to the question I asked before. Do the gram/kg checkboxes apply to all of the products (or whatever "lft" and "sf" are supposed to mean) at once? Or do you want to have separate gram/kg checkboxes for each product? Or if you don't understand that, is it possible for the user to request 1 gram of lft and a kilogram of sf?
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Thanks Paul for your concern.

Yes,i want to have separate gram/kg checkboxes for each product. And its possible for the user to request 1 gram of lft and a kilogram of sf. After clicking the submit button, internally, i will recognize each and every product on the basis of gram or kg, will convert them into a particular unit, and then, calculate average and other things.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18655
    
    8

Then go ahead and have separate checkboxes for each of those choices. They will have to be separate components, of course.
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
That means, i will have to use , may be a dozens of checkboxes, all for same purpose.
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Kunal Lakhani wrote:That means, i will have to use , may be a dozens of checkboxes, all for same purpose.

They wouldn't be for the same purpose; each would be for a different product.

If user checks 'gram', the value entered represents the weight in gram. Similar for kg.

This doesn't make sense. Its sounds as if the two checkboxes are mutually exclusive - it makes sense only for one of them to be set. So these should be radio buttons or a menu with two options.

If you are using the textfield/checkbox combo several times throughout the code the GUI, then it makes sense to create a class that encapsulates both in a component that extends JPanel. Then you would only have to instantiate one of these objects for each of the products.
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
That was a wonderful answer Tim. I will implement it today, and see if it helps.

Thanks Paul, Michael, Tim.
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Tim, can you please provide the code for encapsulation? I am not able to do it.
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
I have no interest in doing your work for you; and besides, that way you won't learn nearly as much. You need to create a class that extend JPanel and on which you place a text field and the checkboxes as they should appear for each product. That class probably takes the names of the labels as parameters, and likely has methods to retrieve the value of the textfield and the checkbox(es).

What have you tried?
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
This is what i did.




But i don't know how to use this class in my Applet class.
First i need to create an object of this class. Then?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39395
    
  28
I don’t think that class will work for anything. Have you read about radio buttons, or the Java™ Tutorials link in that document?
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
So, what should i do?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39395
    
  28
Kunal Lakhani wrote:So, what should i do?
What did you find when you read the links in my last post?
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
Didn't get you. To what point are you pointing?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39395
    
  28
You have a link explaining how radio buttons work, so read it.
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
I read that. There is no problem with using radio buttons. The problem i am facing is that i want to have separate gram/kg radio buttons for each product. After clicking the submit button, internally, i will recognize each and every product on the basis of gram or kg, will convert them into a particular unit, and then, calculate average and other things.

I am seeking an option where i don't need to make as many radio buttons as the number of products. And moreover, each radio buttons set will be same. Hope, i am able to make you understand.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3646
    
  15
Your class should create any components (radio buttons, etc) itself. You shouldn't create then outside the class and then pass them in. So get rid of all your setters and getters. Your constructor should have a String parameter that represents the text you want to display and you should create all your components and add them to the panel in the constructor. You could perhaps also have parameters in your constructor to specify the initial state of your components if this won't always be the same.You will then need method(s) to return the state of your various components.


Joanne
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Kunal Lakhani wrote:...

Yes, we understand that. Now you need to learn how to create your own Swing components. The problem with what you have so far is that it doesn't create and show any components. Study the links Campbell posted; I'm sure there's an example of how to create such a custom component in there somewhere.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39395
    
  28
You could also create a factory method
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using same checkbox multiple times in an applet