AddActionListener method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • brendanmcdonagh
    New Member
    • Nov 2007
    • 153

    AddActionListener method

    Hi all I'm confused about how to get my button to do something when it is clicked, my understanding after searching is that the following should work:

    However im getting an illegal start of of expression for my method header and also a ';' expected at the the last }

    Anyone see something I don't??

    Code:
    aButton.addActionListener(this);

    Code:
    public void actionPerformed(ActionEvent e)
    {
        String aString = inputArea.getText();
        if(aString.equals("exit"))
            {
                socket.close();
                System.exit();
            }
        else
            {
                 outStream.println(aString);
                
            }
    }
    
    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by brendanmcdonagh
    Anyone see something I don't??
    Nope, because you're seeing a whole lot of context (where the syntax errors
    come from) which we don't see. As it is we can't answer your question.

    kind regards,

    Jos

    Comment

    • brendanmcdonagh
      New Member
      • Nov 2007
      • 153

      #3
      Sorry Jos, just didn't want to ask you to read everything if you didn't need to

      Code:
      /*
       * Main.java
       *
       * Created on 28 October 2008, 13:19
       *
       * To change this template, choose Tools | Options and locate the template under
       * the Source Creation and Management node. Right-click the template and choose
       * Open. You can then make changes to the template in the Source Editor.
       */
      
      package clientside;
      
      import java.io.*;
      import java.net.*;
      import java.util.*;
      import javax.swing.*;
      import java.awt.*;
      
      public class Main implements ActionListener
      {
      public static void main(String[] args)
      {
      try
      {
      JFrame aFrame = new JFrame();
      aFrame.setSize(500, 500);
      aFrame.setTitle("Input your message");
      aFrame.setLayout(new FlowLayout());
      aFrame.setVisible(true);
      
      JButton aButton = new JButton();
      aButton.setText("Press to send message");
      aButton.setSize(200, 25);
      aButton.addActionListener(this);
      aFrame.add(aButton);
      JButton aButton2 = new JButton();
      aButton2.setText("Press to clear message");
      aButton2.setSize(200, 25);
      aFrame.add(aButton2);
      aButton2.setVisible(true);
      JTextField inputArea = new JTextField(50);
      inputArea.setVisible(true);
      aFrame.add(inputArea);
            
      Socket socket = new Socket("192.168.0.6", 4243);
      PrintStream outStream = new PrintStream(socket.getOutputStream());
      
      
      public void actionPerformed(ActionEvent e)
      {
          String aString = inputArea.getText();
          if(aString.equals("exit"))
              {
                  socket.close();
                  System.exit();
              }
          else
              {
                   outStream.println(aString);
                  
              }
      }
      
      }
      catch (Exception anException)
      {
          System.out.println("Error: " + anException);
      }
      }
      }

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        You're curly brackets are incorrect; as it is now you're trying to define a method
        inside another method (which doesn't work of course). Check them all.

        kind regards,

        Jos

        Comment

        • brendanmcdonagh
          New Member
          • Nov 2007
          • 153

          #5
          I had a feeling it was something like that but couldnt find any answers

          So i can't have a actionperformed method in a main method?

          if not, should i end the main method and have the try/catch in the main method and then have my actionperformed method before the end of the class bracket??

          I ve just tried the above and it's giving me a load cannot find symbols so there obviously out of scope, how can i account for this if i can 't have a method in a method??

          Code:
          /*
           * Main.java
           *
           * Created on 28 October 2008, 13:19
           *
           * To change this template, choose Tools | Options and locate the template under
           * the Source Creation and Management node. Right-click the template and choose
           * Open. You can then make changes to the template in the Source Editor.
           */
          
          package clientside;
          
          import java.io.*;
          import java.net.*;
          import java.util.*;
          import javax.swing.*;
          import java.awt.*;
          
          public class Main implements ActionListener
          {
          public static void main(String[] args)
          {
          try
          {
          JFrame aFrame = new JFrame();
          aFrame.setSize(500, 500);
          aFrame.setTitle("Input your message");
          aFrame.setLayout(new FlowLayout());
          aFrame.setVisible(true);
          
          JButton aButton = new JButton();
          aButton.setText("Press to send message");
          aButton.setSize(200, 25);
          aButton.addActionListener(this);
          aFrame.add(aButton);
          JButton aButton2 = new JButton();
          aButton2.setText("Press to clear message");
          aButton2.setSize(200, 25);
          aFrame.add(aButton2);
          aButton2.setVisible(true);
          JTextField inputArea = new JTextField(50);
          inputArea.setVisible(true);
          aFrame.add(inputArea);
                
          Socket socket = new Socket("192.168.0.6", 4243);
          PrintStream outStream = new PrintStream(socket.getOutputStream());
          }
          catch (Exception anException)
          {
              System.out.println("Error: " + anException);
          }
          }
          
          public void actionPerformed(ActionEvent e)
          {
              String aString = inputArea.getText();
              if(aString.equals("exit"))
                  {
                      socket.close();
                      System.exit();
                  }
              else
                  {
                       outStream.println(aString);
                      
                  }
          }
          
          }

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by brendanmcdonagh
            I had a feeling it was something like that but couldnt find any answers

            So i can't have a actionperformed method in a main method?

            if not, should i end the main method and have the try/catch in the main method and then have my actionperformed method before the end of the class bracket??

            I ve just tried the above and it's giving me a load cannot find symbols so there obviously out of scope, how can i account for this if i can 't have a method in a method??
            If you need variables in a method that you also need in another method and you
            can't call one method from the other, make those variables private variables, then
            they're global to your object or, when you make them static variables, global to the
            class you're defining.

            Every class has the following structure:

            Code:
            class Name extends APossibleOtherClass implements PossibleInterfaces {
               <static variables>
               <static initialization code>
               <static methods>
               <variables>
               <initialization code>
               <constructors>
               <methods>
            }
            The <static variables> or <variables> section can be classes or interfaces again.
            None of the methods are nested but the order of static/non-static methods/variables
            is more or less arbitrary but I do suggest you keep a more or less consistent
            order in them.

            kind regards,

            Jos

            Comment

            • brendanmcdonagh
              New Member
              • Nov 2007
              • 153

              #7
              So by your kind response Jos, my variables, fram, textfield, button, etc should go after the start of the static method so there accesible everywhere,

              Ive tried to put them after public sttaic void main(String[] args) but before the try and also made them private. still same symbol error. I ve also tried other places for my variables without luck.

              I really want to complete this gui (it's my first) tonight and i really am trying before asking questions. Could you help me by telling where in the class i should put JTextField inputArea = new JTextField(50); for instance and ill figure out the rest.

              Regards

              Brendan

              Comment

              • brendanmcdonagh
                New Member
                • Nov 2007
                • 153

                #8
                right, got the symbol errors sorted but im back to being stuck with the actionperform() and addactionlisten er.

                here's my update code

                Code:
                /*
                 * Main.java
                 *
                 * Created on 28 October 2008, 13:19
                 *
                 * To change this template, choose Tools | Options and locate the template under
                 * the Source Creation and Management node. Right-click the template and choose
                 * Open. You can then make changes to the template in the Source Editor.
                 */
                
                package clientside;
                
                import java.io.*;
                import java.net.*;
                import java.util.*;
                import javax.swing.*;
                import java.awt.*;
                
                public class Main implements ActionListener
                {
                    public JFrame aFrame;
                    public JButton aButton;
                    public JButton aButton2;
                    public JTextField inputArea;
                    static Socket socket;
                    static PrintStream outStream;
                    
                    public Main()
                    {
                aFrame = new JFrame();
                aFrame.setSize(500, 500);
                aFrame.setTitle("Input your message");
                aFrame.setLayout(new FlowLayout());
                aFrame.setVisible(true);
                
                aButton = new JButton();
                aButton.setText("Press to send message");
                aButton.setSize(200, 25);
                aButton.addActionListener(this);
                aFrame.add(aButton);
                
                aButton2 = new JButton();
                aButton2.setText("Press to clear message");
                aButton2.setSize(200, 25);
                aFrame.add(aButton2);
                aButton2.setVisible(true);
                
                 inputArea = new JTextField(50);
                inputArea.setVisible(true);
                aFrame.add(inputArea);
                    }
                public static void main(String[] args)
                {
                try
                {     
                    socket = new Socket("192.168.0.6", 4243);
                    outStream = new PrintStream(socket.getOutputStream());
                }
                catch (Exception anException)
                {
                    System.out.println("Error: " + anException);
                }
                }
                public void actionPerformed(ActionEvent e)
                {
                String aString = inputArea.getText();
                    if(aString.equals("exit"))
                        {
                            socket.close();
                            System.exit(1);
                        }
                    else
                    {
                        outStream.println(aString);
                        aString = inputArea.getText();
                    }
                }
                
                }
                here's the errors :

                init:
                deps-jar:
                Compiling 1 source file to C:\ClientSide\b uild\classes
                C:\ClientSide\s rc\clientside\M ain.java:19: cannot find symbol
                symbol: class ActionListener
                public class Main implements ActionListener
                C:\ClientSide\s rc\clientside\M ain.java:64: cannot find symbol
                symbol : class ActionEvent
                location: class clientside.Main
                public void actionPerformed (ActionEvent e)
                C:\ClientSide\s rc\clientside\M ain.java:39: addActionListen er(java.awt.eve nt.ActionListen er) in javax.swing.Abs tractButton cannot be applied to (clientside.Mai n)
                aButton.addActi onListener(this );
                3 errors

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  The classes it couldn't find are in packages you didn't include. See the first article
                  in this section, titled "Read This First"; it has a link from which you can download
                  the complete API documentation; download and install it, it has great value.

                  kind regards,

                  Jos

                  Comment

                  • brendanmcdonagh
                    New Member
                    • Nov 2007
                    • 153

                    #10
                    Thanks Jos,

                    Just coming back to paste my succesful code!!!!!!!!!!! ! (in case anyone has similar problems in future)

                    Code:
                    /*
                     * Main.java
                     *
                     * Created on 28 October 2008, 13:19
                     *
                     * To change this template, choose Tools | Options and locate the template under
                     * the Source Creation and Management node. Right-click the template and choose
                     * Open. You can then make changes to the template in the Source Editor.
                     */
                    
                    package clientside;
                    
                    import java.io.*;
                    import java.net.*;
                    import java.util.*;
                    import javax.swing.*;
                    import java.awt.*;
                    import java.awt.event.*;
                    
                    
                    public class Main implements ActionListener
                    {
                        public static JFrame aFrame;
                        public static JButton aButton;
                        public static JButton aButton2;
                        public static JTextField inputArea;
                        static Socket socket;
                        static PrintStream outStream;
                        
                        public Main()
                        {
                    aFrame = new JFrame();
                    aFrame.setSize(500, 500);
                    aFrame.setTitle("Input your message");
                    aFrame.setLayout(new FlowLayout());
                    aFrame.setVisible(true);
                    
                    aButton = new JButton();
                    aButton.setText("Press to send message");
                    aButton.setSize(200, 25);
                    aButton.addActionListener(this);
                    aFrame.add(aButton);
                    
                    aButton2 = new JButton();
                    aButton2.setText("Press to clear message");
                    aButton2.setSize(200, 25);
                    aFrame.add(aButton2);
                    aButton2.setVisible(true);
                    
                     inputArea = new JTextField(50);
                    inputArea.setVisible(true);
                    aFrame.add(inputArea);
                        }
                    public static void main(String[] args)
                    {
                    try
                    {    
                        Main aMain = new Main();
                        socket = new Socket("192.168.0.6", 4242);
                        outStream = new PrintStream(socket.getOutputStream());
                    }
                    catch (Exception anException)
                    {
                        System.out.println("Error: " + anException);
                    }
                    }
                    public void actionPerformed(ActionEvent e)
                    {
                    String aString = Main.inputArea.getText();
                        if(aString.equals("exit"))
                            {
                                //socket.close();
                                System.exit(1);
                            }
                        else
                        {
                            Main.outStream.println(aString);
                            aString = Main.inputArea.getText();
                        }
                    }
                    
                    }
                    Now to learn 2 way chat and multi threading!!

                    Thank you for your help Jos

                    Regards

                    Brendan

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by brendanmcdonagh
                      Thanks Jos,
                      You're welcome of course; organize your development environment: install the
                      API documentation, textually organize your classes; organize your package
                      hierarchy and your code layout style (most IDEs can do that for you).

                      The first hurdles are the worst; you've passed a couple of them and the remaining
                      ones will be easier to handle. All that organizing makes coding boring as it should
                      be because a human being should spend his/her time on design, not on banging
                      their keyboard while pulling their hair. Good luck and

                      kind regards,

                      Jos

                      Comment

                      • OhGod Coding

                        #12
                        I know what you mean Jos... I know what you mean. T_T

                        Comment

                        • Parth Gallen
                          New Member
                          • Nov 2010
                          • 3

                          #13
                          button b prints out "New Game", button b1 "Solving".
                          Not saying it's a perfect way but it looks simple enough to me this way:

                          Code:
                          ActionListener al = new ActionListener() {
                                public void actionPerformed(ActionEvent e) {
                                    if (e.getActionCommand().equals("New Game")) {
                                    System.out.println("New Game");
                                  } else if (e.getActionCommand().equals("Solve")) {
                                    System.out.println("Solving");
                                  }
                                }
                              };
                          
                              Button b = new Button();
                              b.setName("New Game");
                              b.setActionCommand("New Game");
                              b.addActionListener(al);
                              
                              Button b1 = new Button();
                              b1.setName("Solve");
                              b1.setActionCommand("Solve");
                              b1.addActionListener(al);
                          removing the need to add the void actionPerformed (ActionEvent e) method

                          Comment

                          Working...