'How can I let the user choose only one radiobutton? and how do I put a price for each one of them and let the user pay for the ticket he choosed?
This is what ive done so far I tried the add the ButtonGroup but it keeps showing me that there is an error T_T
public class tram extends JFrame implements ActionListener, KeyListener {
TextArea output = new TextArea(6, 30);
JButton cancel = new JButton("Cancel");
JButton exit = new JButton("Exit");
JRadioButton Single = new JRadioButton("Single");
JRadioButton Double = new JRadioButton("Double");
JRadioButton ZoneA = new JRadioButton("Zone A");
JRadioButton ZoneA_B = new JRadioButton("Zone A&B");
I wrote the Button.Group here with changing the "new JRadioButton();" and it didnt work I am not sure if I should use if statement because I could not find a way to do it ^.^"
public tram(){
JLabel title = new JLabel("Please select the type of ticket you wish to purchase");
setLayout(new BorderLayout());
setSize(450, 300);
setTitle("Redwich Tram");
JFrame frm = new JFrame();
JPanel top = new JPanel();
top.setBackground(Color.white);
top.add(title);
title.setFont(new Font("Courier", Font.BOLD, 12));
add("North", top);
JPanel middle = new JPanel();
middle.setBackground(Color.WHITE);
top.add(new JLabel("Select an option by clicking one of the buttons"));
add("Center", middle);
middle.add(Single, BorderLayout.NORTH);
Single.setBackground(Color.white);
middle.add(Double, BorderLayout.CENTER);
Double.setBackground(Color.white);
middle.add(ZoneA, BorderLayout.SOUTH);
ZoneA.setBackground(Color.white);
middle.add(ZoneA_B, BorderLayout.SOUTH);
ZoneA_B.setBackground(Color.white);
middle.setBackground(Color.WHITE);
middle.add(output);
JPanel bottom = new JPanel();
bottom.setBackground(Color.white);
add("South", bottom);
bottom.add(cancel,"South");
cancel.setBackground(Color.white);
bottom.add(exit,"South");
exit.setBackground(Color.white);
setResizable(false);
setVisible(true);
}
public static void main(String[] args) {
new tram();
}
public void onRadioButtonClicked(View view) {
}
}
Solution 1:[1]
For JRadioButtons you have to add them to a ButtonGroup and set one of the buttons as selected.
Here's a small example:
JRadioButton b1 = new JRadioButton('option1);
b1.setSelected(true);
ButtonGroup g = new ButtonGroup();
g.add(b1);
Solution 2:[2]
As seen in the documentation of JRadioButton:
An implementation of a radio button -- an item that can be selected or deselected, and which displays its state to the user. Used with a ButtonGroup object to create a group of buttons in which only one button at a time can be selected. (Create a ButtonGroup object and use its add method to include the JRadioButton objects in the group
You must first create a ButtonGroup and add the JRadioButton in it
Solution 3:[3]
for this--How can I let the user choose only one radio button
see this it will help.
ToggleGroup radioGroup=new ToggleGroup();
option1radio.setToggleGroup(radioGroup);
option2radio.setToggleGroup(radioGroup);
option3radio.setToggleGroup(radioGroup);
option4radio.setToggleGroup(radioGroup);
here option1,2,3,4radio are the radiobtn id's; Go through this it will help.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | JFPicard |
| Solution 3 | abhishek kr sahu |
