tags:

views:

28

answers:

1

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);
 }
+3  A: 
ActionListener priceListener = new ActionListener() { 
        public void actionPerformed(ActionEvent e) { 
            if (e.getSource() == poolPrice) { 
                tubLabel.setEnabled(false); 
                tubField.setEnabled(false); 
                // Re-enable the previously disabled labels
                poolLabel.setEnabled(true);
                poolField.setEnabled(true);
                messageArea.setVisible(false); 
            } else if (e.getSource() == tubPrice) { 
                poolLabel.setEnabled(false); 
                poolField.setEnabled(false); 
                // Re-enable disabled labels
                tubLabel.setEnabled(true);
                tubField.setEnabled(true);

                messageArea.setVisible(false); 
            } 
            } 
        }; 

You need to re-enable the buttons you disabled.

I82Much
Alrighty, that works. Now for whatever reason when I go to enter only 1 set of numbers for each, IE if I wanted the volume of the pool to be 2, which would mean hot tub would be blank, I get errors. But if I fill in a number in hot tub area, and 2 in pool, I get a price of 200.Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:994) at java.lang.Double.parseDouble(Double.java:510) at la.GUI$1.actionPerformed(La.java:154)
David
double pool = Double.parseDouble (poolField.getText()); double tub = Double.parseDouble(tubField.getText()); If one of the text fields is blank, you're going to get a numberformatexception because you cannot convert a blank string ("") into a number. You need to handle that case.
I82Much