0
votes

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?

3
For within the ActionEvent, we have, better would be post an SSCCEmKorbel
Not enough context: Must both blah and meh be evaluated for a given click? Are x and z mutually exclusive? Can inputs be verified on entry?trashgod
What's the exception been thrown?MadProgrammer

3 Answers

0
votes

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();
    }
0
votes

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
        );
    }
}
0
votes

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.