I am trying to figure out how to properly use listeners in this example. What I want it to do is display a pop-up after clicking the submit button showing what boxes are checked, but right now I get a popup after each box is clicked. I have tried implementing an action listener for the radio buttons and an item listener for the check boxes, but I'm not sure if this is the right thing to do. What am I missing?
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class CarSelector extends JFrame implements ActionListener, ItemListener{
JButton submit = new JButton("Submit");
JLabel label1 = new JLabel("Select Vehicle type and options");
JLabel carLabel = new JLabel("Vehicle Type");
JLabel options = new JLabel("Options");
ButtonGroup group = new ButtonGroup();
JRadioButton carRadio = new JRadioButton("Car", true);
JRadioButton vanRadio = new JRadioButton("Minivan");
JRadioButton truckRadio = new JRadioButton("Pickup Truck");
JRadioButton suvRadio = new JRadioButton("SUV");
JCheckBox leather = new JCheckBox("Leather Seats");
JCheckBox ac = new JCheckBox("Air Conditioning");
JCheckBox sat = new JCheckBox("Sattelite Radio");
JCheckBox warmer = new JCheckBox("Seat Warmers");
String optionsSelected;
String carSelected;
ActionListener listen = new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae){
JOptionPane.showMessageDialog(
CarSelector.this, sb.toString + ssb.toString());
}
};
CarSelector(){
super("Vehicle Selector");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300, 300);
CarGUI();
}
public void CarGUI(){
JPanel vehicleTypes = new JPanel();
JPanel carOptions = new JPanel();
JPanel submitButton = new JPanel();
carRadio.addActionListener(listen);
vanRadio.addActionListener(listen);
truckRadio.addActionListener(listen);
suvRadio.addActionListener(listen);
leather.addActionListener(listen);
ac.addActionListener(listen);
sat.addActionListener(listen);
warmer.addActionListener(listen);
submit.addActionListener(listen);
add(submitButton);
submitButton.setLayout(new BoxLayout(submitButton, BoxLayout.X_AXIS));
submitButton.setBounds(100, 150, 100, 100);
add(vehicleTypes);
vehicleTypes.setLayout(new BoxLayout(vehicleTypes, BoxLayout.Y_AXIS));
vehicleTypes.setBounds(150,0,125,125);
add(carOptions);
carOptions.setLayout(new BoxLayout(carOptions, BoxLayout.Y_AXIS));
vehicleTypes.add(carLabel);
vehicleTypes.add(carRadio);
vehicleTypes.add(vanRadio);
vehicleTypes.add(truckRadio);
vehicleTypes.add(suvRadio);
group.add(carRadio);
group.add(vanRadio);
group.add(truckRadio);
group.add(suvRadio);
carOptions.add(options);
carOptions.add(leather);
carOptions.add(ac);
carOptions.add(sat);
carOptions.add(warmer);
submitButton.add(submit);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void itemStateChanged(ItemEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
This is the code for running the program.
public class GUITest {
public static void main (String[] args){
CarSelector car = new CarSelector();
}
}
To further my comment above, for your use case, you should only be adding an
ActionListenerto yoursubmitbutton.Within the
ActionListeneryou will need to callisSelectedon each of yourJRadioButtons andJCheckBoxes.Something like this will do the trick:
A cleaner and more sustainable way to handle this sort of thing would be to put your
JRadioButtons andJCheckBoxes inArrays. It would make yourActionListenercode a lot cleaner and it would be easier to add more options because your would just need to insert a new checkbox or radiobutton into your arrays and not have to change any of yourActionListenercode.