I am having some issues with my code, I cant see whats wrong with the logic, but here it is. I want them to have the radio button to choose either or, and when one is selected(radio button) the text area isn't available and vise versa. Here is the segment of the code. When I go back and forth on the radio buttons, both become not selectable, and I am unsure why.
private void PriceTab()
{
pricePanel = new JPanel(new FlowLayout());
final JRadioButton poolPrice= new JRadioButton("Pool");
final JRadioButton tubPrice = new JRadioButton("Hot Tub");
poolPrice.setSelected(true);
ButtonGroup group = new ButtonGroup();
group.add(poolPrice);
group.add(tubPrice);
pricePanel.add(poolPrice);
pricePanel.add(tubPrice);
final JLabel poolLabel = new JLabel("Enter the pool's volume: ");
final JTextField poolField = new JTextField(10);
pricePanel.add(poolLabel);
pricePanel.add(poolField);
final JTextField tubField = new JTextField(10);
final JLabel tubLabel = new JLabel ("Enter the tub's volume: ");
pricePanel.add(tubLabel);
pricePanel.add(tubField);
JButton calculatePrice = new JButton("Calculate Price");
calculatePrice.setMnemonic('C');
pricePanel.add(calculatePrice);
pricePanel.add(createExitButton());
pricePanel.add(new JLabel("The price is: "));
final JTextField priceField = new JTextField(10);
priceField.setEditable(false);
pricePanel.add(priceField);
final JTextArea messageArea = createMessageArea(1, 25,
"*Please only select one section");
pricePanel.add(messageArea);
calculatePrice.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
double pool = Double.parseDouble (poolField.getText());
double tub = Double.parseDouble(tubField.getText());
double price;
if (poolPrice.isSelected()) {
price = pool * 100;
} else {
price = tub * 75;
}
priceField.setText(df.format(price));
}
});
ActionListener priceListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == poolPrice) {
tubLabel.setEnabled(false);
tubField.setEnabled(false);
messageArea.setVisible(false);
} else if (e.getSource() == tubPrice) {
poolLabel.setEnabled(false);
poolField.setEnabled(false);
messageArea.setVisible(false);
}
}
};
poolPrice.addActionListener(priceListener);
tubPrice.addActionListener(priceListener);
}