0

Given a GUI app, a user will select one of the two radio buttons, JRadioButton a, or JRadioButton b. Depending on his selection, he will enter different inputs. However, to calculate a formula, he will click on a regular button, JButton c.

However, the trouble ensues when more than two member functions are called within the ActionListener.


      c = new JButton( "c" );
      c.addActionListener( new ActionListener() {
        @Override
        public void actionPerformed( ActionEvent e ) {
          cActionPerformed( e );
        }
      });

For within the ActionEvent, we have,


    public void cActionPerformed( ActionEvent ev ) {
      try {
        double f = foo.blah( x, y );
        double b = bar.meh( y, z );
      }
      catch( NumberFormatException e ) {
        JOptionPane.showConfirmDialog(
        null, "Error message.", "Error", JOptionPane.CANCEL_OPTION
        );
      }
    }

However, the program only goes down one level in the call stack, returning the catch exception dialog. How do I make it so that when the user presses button c, depending on whether a or b is selected, he gets f or b, respectively?

JonK
  • 2,097
  • 2
  • 25
  • 36
  • 1
    `For within the ActionEvent, we have,` better would be post an [SSCCE](http://mindprod.com/jgloss/sscce.html) – mKorbel Aug 05 '12 at 21:34
  • Not enough context: Must both `blah` and `meh` be evaluated for a given click? Are `x` and `z` mutually exclusive? Can inputs be [verified](http://stackoverflow.com/a/11818946/230513) on entry? – trashgod Aug 05 '12 at 23:35
  • What's the exception been thrown? – MadProgrammer Aug 05 '12 at 23:40

3 Answers3

0

You could use the getSource() method inherited from EventObject to discriminate the source of the event.

Example:

  public void actionPerformed(ActionEvent event) {
    if (event.getSource() == button1) {
      setSize(300, 200);
      doLayout();
    } else if (event.getSource() == button2) {
      setSize(400, 300);
      doLayout();
    } else if (event.getSource() == button3) {
      setSize(500, 400);
      doLayout();
    }
Amir Afghani
  • 37,814
  • 16
  • 84
  • 124
0

You can use the isSelected() option of a radio button.

JRadioButton f = new JRadioButton();
JRadioButton b = new JRadioButton();

public void cActionPerformed( ActionEvent ev ) {
      try {
        if(f.isSelected()){

            double f = foo.blah( x, y );

         } else if(b.isSelected()){

            double b = bar.meh( y, z );

         } else { // If none is selected
            // Do something
         }
      }
      catch( NumberFormatException e ) {
        JOptionPane.showConfirmDialog(
        null, "Error message.", "Error", JOptionPane.CANCEL_OPTION
        );
    }
}
La bla bla
  • 8,558
  • 13
  • 60
  • 109
0

Your problem with the program entering the catch block has nothing to do with determining which radio button is selected. The only way it can enter into the catch block is if a NumberFormatException is being thrown. The only way a NumberFormatException can be thrown is if it is coming from foo.blah( x, y ); or bar.meh( y, z );.

So - you need to figure out why that exception is being thrown by your functions first. Then you can apply La bla bla's answer.

A good way of figuring out where the error is being thrown is to use e.printStackTrace() in your catch. This will print a stack trace to console output pointing you to the exact line of code causing the problem.

Nick Rippe
  • 6,465
  • 14
  • 30
  • `java.lang.NumberFormatException: empty String at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source) at java.lang.Double.parseDouble(Unknown Source) at foobar.cActionPerformed(foobarFrame.java:86) at foobar$3.actionPerformed(foobarFrame.java:54) ... ` –  Aug 06 '12 at 05:12
  • So line 86 of your "LoanCalculatorFrame" class is throwing the exception. Which method is on that line? Can we see the source code for that method? I'm guessing you're converting a String to a number in that method. This is what the java docs says about this exception: "Thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format." – Nick Rippe Aug 06 '12 at 05:20
  • `double amt = Double.parseDouble( loanTextField.getText() );` –  Aug 06 '12 at 05:26
  • So the problem is that the text in `loanTextField` can't be parsed as a double because it's not in the right format. If you print out the text to the console window, you'll probably be able to see the problem (use `System.out.println("'" + loanTextField.getText() + "'");` - that way you can see any extra spaces at the ends) – Nick Rippe Aug 06 '12 at 05:30
  • I'm sorry - I must have misrepresented my intentions. I was willing to hold your hand helping you troubleshoot your problem because it's a good learning experience (for you). I think we've found the problem - you have a space or a % or some non-number character in the textbox you're trying to convert to a number. I'm pretty sure you can figure it out from here... (it's going to be the thing that's not one of these characters: `0123456789-.`) I'm going to sleep now - best of luck with finding the culprit! – Nick Rippe Aug 06 '12 at 06:00
  • Oh - And I should also mention that if the field is blank, that exception will also be thrown (you can't convert an empty string to a number). Come to think of it - this is probably what's happening. – Nick Rippe Aug 06 '12 at 06:08
  • Is it supposed to be blank, so that the user can enter something? –  Aug 06 '12 at 06:22
  • Yes - but if it's blank when you're trying to convert it into a number, you're going to get an exception. So you can wrap an `if` statement around it to make sure it's not blank before you do any calculations with it. – Nick Rippe Aug 06 '12 at 13:34