user3944062
user3944062

Reputation:

Java Eclipse checkboxes

I have two extra checkboxes which are set at .50 each when the checkbox is selected it adds the .50 to the total, so far no problems.

How can I make it so that the user can change the checkbox text to the topping they like?

Here is my code:

chckbxAddTopp1 = new JCheckBox("Add Topping1");
chckbxAddTopp1.setBackground(new Color(204, 255, 255));
chckbxAddTopp1.addActionListener(listener);


chckbxAddTopp2 = new JCheckBox("Add Topping2");
chckbxAddTopp2.setBackground(new Color(204, 255, 255));
chckbxAddTopp2.addActionListener(listener);

Upvotes: 0

Views: 1434

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347204

Associate the JCheckBox with JTextField or better yet a JComboBox, which will limit what the use can select. Place these in an array...

JCheckBox[] toppingsSelection = new JCheckBox[2];
JComboBox[] toppingTypes = new JComboBox[2];

For easier access (ie, element 0 of each array is associated together). Layout the checkbox and the associated field on the same line within the UI.

You can the use a loop to check which toppings are selected and use the same index value to get the type of topping.

Another idea would be to use a JComboBox, a JButton and a JList.

The user would select the type of topping they want and press the "Add" button and it would be added to the JList. You would simply need to check the number of elements in the JList to determine the extra charge

Have a look at How to Use Buttons, Check Boxes, and Radio Buttons and How to Use Lists and How to Use Combo Boxes for more details

Upvotes: 1

Josh M
Josh M

Reputation: 11937

A JCheckBox isn't the appropriate type of component to use for what you are trying to achieve. A JComboBox seems more appropriate. You could store all of the possible toppings (or if you want make the combo box editable so the user can enter what they want) in the model and have a JButton that adds the topping to the total cost.

Upvotes: 0

Related Questions