• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using same checkbox multiple times in an applet

 
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?





 
Saloon Keeper
Posts: 7582
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What prevents you from instantiating two more JCheckBox objects?
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 7582
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> 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
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That means, i will have to use , may be a dozens of checkboxes, all for same purpose.
 
Tim Moores
Saloon Keeper
Posts: 7582
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That was a wonderful answer Tim. I will implement it today, and see if it helps.

Thanks Paul, Michael, Tim.
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tim, can you please provide the code for encapsulation? I am not able to do it.
 
Tim Moores
Saloon Keeper
Posts: 7582
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, what should i do?
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Didn't get you. To what point are you pointing?
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have a link explaining how radio buttons work, so read it.
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Tim Moores
Saloon Keeper
Posts: 7582
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could also create a factory method
reply
    Bookmark Topic Watch Topic
  • New Topic