I have a swing application that includes radio buttons, I am trying to set a string to what button is selected. I have made in in bold where the radio buttons are made and grouped. I am having problems getting a srting to set to “Hybred”, “Electric”, or “Outher” as to what button is selected
I have a swing application that includes radio buttons, I am trying
to set a string to what button is selected. I have made in in bold where the radio buttons are made and grouped. I am having problems getting a srting to set to “Hybred”, “Electric”, or “Outher” as to what button is selected. Can you help. I have a total of 4 classes. I have encluded the Project2 class.
Thank you
package Project2;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JLayeredPane;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.JRadioButton;
import java.util.Locale;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class Project2 {
private JFrame frmAotomobileSalesTax;
private JTextField textMakeAndModle;
private JTextField textSalesPrice;
private JTextField textMPG;
private JTextField textWeightPounds;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Project2 window = new Project2();
window.frmAotomobileSalesTax.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Project2() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
@SuppressWarnings(“unused”)
private void initialize() {
frmAotomobileSalesTax = new JFrame();
frmAotomobileSalesTax.getContentPane().setFocusable(false);
frmAotomobileSalesTax.getContentPane().setFocusTraversalKeysEnabled(false);
frmAotomobileSalesTax.getContentPane().setEnabled(false);
frmAotomobileSalesTax.getContentPane().setName(“Automobile Type”);
frmAotomobileSalesTax.setTitle(“Aotomobile Sales Tax Calculator”);
frmAotomobileSalesTax.setBounds(100, 100, 450, 300);
frmAotomobileSalesTax.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmAotomobileSalesTax.getContentPane().setLayout(null);
JLabel lblNewLabel = new JLabel(“Make and Modle”);
lblNewLabel.setBounds(90, 33, 94, 13);
frmAotomobileSalesTax.getContentPane().add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel(“Sales Price”);
lblNewLabel_1.setBounds(90, 56, 81, 13);
frmAotomobileSalesTax.getContentPane().add(lblNewLabel_1);
textMakeAndModle = new JTextField();
textMakeAndModle.setBounds(194, 30, 166, 19);
frmAotomobileSalesTax.getContentPane().add(textMakeAndModle);
textMakeAndModle.setColumns(10);
textSalesPrice = new JTextField();
textSalesPrice.setBounds(194, 53, 166, 19);
frmAotomobileSalesTax.getContentPane().add(textSalesPrice);
textSalesPrice.setColumns(10);
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setFocusable(false);
layeredPane.setName(“Automobile Type”);
layeredPane.setLocale(Locale.US);
layeredPane.setBorder(new LineBorder(Color.ORANGE));
layeredPane.setBounds(36, 96, 372, 97);
frmAotomobileSalesTax.getContentPane().add(layeredPane);
JRadioButton hybridButton = new JRadioButton(“Hybrid”);
hybridButton.setBounds(6, 17, 77, 21);
layeredPane.add(hybridButton);
hybridButton.setActionCommand(“Hybrid”);
JRadioButton electricButton = new JRadioButton(“Electric”);
electricButton.setBounds(6, 40, 77, 21);
layeredPane.add(electricButton);
electricButton.setActionCommand(“Electric”);
JRadioButton otherButton = new JRadioButton(“Other”);
otherButton.setBounds(6, 63, 103, 21);
layeredPane.add(otherButton);
otherButton.setActionCommand(“Other”);
ButtonGroup buttonTypeGroup = new ButtonGroup(); //Group Radio Buttons so only
buttonTypeGroup.add(hybridButton); //one can be selected at a time
buttonTypeGroup.add(electricButton);
buttonTypeGroup.add(otherButton);
JLabel lblNewLabel_3 = new JLabel(“Miles per Gallon”);
lblNewLabel_3.setBounds(101, 21, 114, 13);
layeredPane.add(lblNewLabel_3);
JLabel lblNewLabel_4 = new JLabel(“Weight in Pounds”);
lblNewLabel_4.setBounds(101, 44, 114, 13);
layeredPane.add(lblNewLabel_4);
textMPG = new JTextField();
textMPG.setBounds(214, 22, 127, 19);
layeredPane.add(textMPG);
textMPG.setColumns(10);
textWeightPounds = new JTextField();
textWeightPounds.setBounds(214, 41, 127, 19);
layeredPane.add(textWeightPounds);
textWeightPounds.setColumns(10);
JLabel lblNewLabel_2 = new JLabel(“Automobile Type”);
lblNewLabel_2.setAutoscrolls(true);
lblNewLabel_2.setDisplayedMnemonicIndex(0);
lblNewLabel_2.setForeground(Color.BLACK);
lblNewLabel_2.setBorder(new EmptyBorder(0, 0, 0, 0));
lblNewLabel_2.setBackground(Color.LIGHT_GRAY);
lblNewLabel_2.setBounds(55, 88, 94, 13);
frmAotomobileSalesTax.getContentPane().add(lblNewLabel_2);
JButton btnComputeSalesTaxButton = new JButton(“Compute Sales Tax”);
btnComputeSalesTaxButton.setBounds(55, 203, 150, 21);
frmAotomobileSalesTax.getContentPane().add(btnComputeSalesTaxButton);
JButton btnClearFeildsButton = new JButton(“Clear Fields”);
btnClearFeildsButton.setBounds(55, 234, 150, 21);
frmAotomobileSalesTax.getContentPane().add(btnClearFeildsButton);
JButton btnDisplayReportButton = new JButton(“Display Report”);
btnDisplayReportButton.setBounds(215, 234, 134, 21);
frmAotomobileSalesTax.getContentPane().add(btnDisplayReportButton);
JPanel panel = new JPanel();
panel.setBorder(new LineBorder(Color.ORANGE));
panel.setBounds(215, 203, 134, 21);
frmAotomobileSalesTax.getContentPane().add(panel);
}
}