1 Reply Latest reply: May 18, 2013 6:05 AM by anotherAikman RSS

    ActionListener and getActionCommand

    1009654
      I do not understand the getActionCommand and ActionListener concepts. This code does not resolve the volume for length, width, and height.

      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;
      import java.text.DecimalFormat;
      import java.awt.Container;

      public class rectangularSolid extends JApplet implements ActionListener {

           public void init() {
                
                String in = JOptionPane.showInputDialog("1(volume),2(surface area),3(diagonal)");
                int input = Integer.parseInt(in);
                
                switch(input) {
                
                case 1:
                     JLabel prompt;
                     JLabel prompt2;
                     JLabel prompt3;
                     JLabel prompt4;
                     Container c = getContentPane();
                     c.setLayout(new FlowLayout());
                     JTextField insert;
                     JTextField insert2;
                     JTextField insert3;
                     JTextField insert4;
                     prompt = new JLabel("enter length");
                     insert = new JTextField(10);     
                     c.add(prompt);
                     c.add(insert);
                     prompt2 = new JLabel("enter width");
                     insert2 = new JTextField(10);               
                     c.add(prompt2);
                     c.add(insert2);
                     prompt3 = new JLabel("enter height");               
                     insert3 = new JTextField(10);
                     c.add(prompt3);               
                     c.add(insert3);
                     prompt4 = new JLabel("answer is ");
                     insert4 = new JTextField(10);
                     c.add(prompt4);
                     c.add(insert4);
                     insert4.addActionListener(this);
                     break;
                case 2:
                     JTextArea outputArea;
                     outputArea = new JTextArea(2,10);
                     Container c1 = getContentPane();
                     c1.add(outputArea);
                     DecimalFormat twoDigits = new DecimalFormat("0.00");
                     String sa = JOptionPane.showInputDialog("enter length");
                     double length2 = Double.parseDouble(sa);
                     String w2 = JOptionPane.showInputDialog("enter width");
                     double width2 = Double.parseDouble(w2);
                     String h2 = JOptionPane.showInputDialog("enter height");
                     double height2 = Double.parseDouble(h2);
                     String output = "";
                     output = (" Surface area = "
                               + twoDigits.format(surfaceArea(length2,width2,height2)));
                     outputArea.setText(output);
                     JOptionPane.showMessageDialog(null,outputArea,null,
                               JOptionPane.INFORMATION_MESSAGE);
                     break;}
                     }
           public void actionPerformed(ActionEvent e ) {
                double length = Double.parseDouble(e.getActionCommand() );     
                double width = Double.parseDouble(e.getActionCommand());
                double height = Double.parseDouble(e.getActionCommand());
                showStatus("Volume is " + volume ( length, width, height ) );
           }
                public double volume(double length, double width, double height) {
                return length * width * height;
      }
                public double surfaceArea(double length2, double width2, double height2) {
                     return ( 2 * length2 * width2 + 2 * length2 * height2 + 2 * width2 * height2);
                
           }
      }
        • 1. Re: ActionListener and getActionCommand
          anotherAikman
          Well for one thing, you assign your length, height and width the return value from the ActionEvent's getActionCommand() method. That's always the same value (probably 0). Better would be to parse the values in the JTextfields directly. I'm not entirely sure wether that is even the proper use of getActionCommand(). I think you want a TextListener, which would complicate things, or (Goto 10) you just call your method with the parsed values from the JTextfields.

          Also, your code is hard to read in the current state. Please repost the relevant portion of it(in this case the pieces that deal with getting the Data and calculating the volume) in code tags:

          \
           
          public void aMethod()
          {
              doSomething();
          }
           \
          becomes
          public void aMethod()
          {
              doSomething();
          }
          Readable code in a question is more likely to lead to an answer. Many experienced programmers here will not even look at unformatted code.

          Edited by: anotherAikman on 18.05.2013 04:04