1 Reply Latest reply on May 18, 2013 11:05 AM by anotherAikman

    ActionListener and getActionCommand

      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);     
                     prompt2 = new JLabel("enter width");
                     insert2 = new JTextField(10);               
                     prompt3 = new JLabel("enter height");               
                     insert3 = new JTextField(10);
                     prompt4 = new JLabel("answer is ");
                     insert4 = new JTextField(10);
                case 2:
                     JTextArea outputArea;
                     outputArea = new JTextArea(2,10);
                     Container c1 = getContentPane();
                     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)));
           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
          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()
          public void aMethod()
          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