469,271 Members | 1,740 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

AddActionListener method

153 100+
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??

Expand|Select|Wrap|Line Numbers
  1.  
  2. aButton.addActionListener(this);
  3.  
  4.  

Expand|Select|Wrap|Line Numbers
  1.  
  2. public void actionPerformed(ActionEvent e)
  3. {
  4.     String aString = inputArea.getText();
  5.     if(aString.equals("exit"))
  6.         {
  7.             socket.close();
  8.             System.exit();
  9.         }
  10.     else
  11.         {
  12.              outStream.println(aString);
  13.  
  14.         }
  15. }
  16.  
  17. }
  18.  
Oct 28 '08 #1
12 7307
JosAH
11,448 Expert 8TB
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
Oct 28 '08 #2
brendanmcdonagh
153 100+
Sorry Jos, just didn't want to ask you to read everything if you didn't need to

Expand|Select|Wrap|Line Numbers
  1.  
  2. /*
  3.  * Main.java
  4.  *
  5.  * Created on 28 October 2008, 13:19
  6.  *
  7.  * To change this template, choose Tools | Options and locate the template under
  8.  * the Source Creation and Management node. Right-click the template and choose
  9.  * Open. You can then make changes to the template in the Source Editor.
  10.  */
  11.  
  12. package clientside;
  13.  
  14. import java.io.*;
  15. import java.net.*;
  16. import java.util.*;
  17. import javax.swing.*;
  18. import java.awt.*;
  19.  
  20. public class Main implements ActionListener
  21. {
  22. public static void main(String[] args)
  23. {
  24. try
  25. {
  26. JFrame aFrame = new JFrame();
  27. aFrame.setSize(500, 500);
  28. aFrame.setTitle("Input your message");
  29. aFrame.setLayout(new FlowLayout());
  30. aFrame.setVisible(true);
  31.  
  32. JButton aButton = new JButton();
  33. aButton.setText("Press to send message");
  34. aButton.setSize(200, 25);
  35. aButton.addActionListener(this);
  36. aFrame.add(aButton);
  37. JButton aButton2 = new JButton();
  38. aButton2.setText("Press to clear message");
  39. aButton2.setSize(200, 25);
  40. aFrame.add(aButton2);
  41. aButton2.setVisible(true);
  42. JTextField inputArea = new JTextField(50);
  43. inputArea.setVisible(true);
  44. aFrame.add(inputArea);
  45.  
  46. Socket socket = new Socket("192.168.0.6", 4243);
  47. PrintStream outStream = new PrintStream(socket.getOutputStream());
  48.  
  49.  
  50. public void actionPerformed(ActionEvent e)
  51. {
  52.     String aString = inputArea.getText();
  53.     if(aString.equals("exit"))
  54.         {
  55.             socket.close();
  56.             System.exit();
  57.         }
  58.     else
  59.         {
  60.              outStream.println(aString);
  61.  
  62.         }
  63. }
  64.  
  65. }
  66. catch (Exception anException)
  67. {
  68.     System.out.println("Error: " + anException);
  69. }
  70. }
  71. }
  72.  
  73.  
  74.  
Oct 28 '08 #3
JosAH
11,448 Expert 8TB
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
Oct 28 '08 #4
brendanmcdonagh
153 100+
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??

Expand|Select|Wrap|Line Numbers
  1.  
  2. /*
  3.  * Main.java
  4.  *
  5.  * Created on 28 October 2008, 13:19
  6.  *
  7.  * To change this template, choose Tools | Options and locate the template under
  8.  * the Source Creation and Management node. Right-click the template and choose
  9.  * Open. You can then make changes to the template in the Source Editor.
  10.  */
  11.  
  12. package clientside;
  13.  
  14. import java.io.*;
  15. import java.net.*;
  16. import java.util.*;
  17. import javax.swing.*;
  18. import java.awt.*;
  19.  
  20. public class Main implements ActionListener
  21. {
  22. public static void main(String[] args)
  23. {
  24. try
  25. {
  26. JFrame aFrame = new JFrame();
  27. aFrame.setSize(500, 500);
  28. aFrame.setTitle("Input your message");
  29. aFrame.setLayout(new FlowLayout());
  30. aFrame.setVisible(true);
  31.  
  32. JButton aButton = new JButton();
  33. aButton.setText("Press to send message");
  34. aButton.setSize(200, 25);
  35. aButton.addActionListener(this);
  36. aFrame.add(aButton);
  37. JButton aButton2 = new JButton();
  38. aButton2.setText("Press to clear message");
  39. aButton2.setSize(200, 25);
  40. aFrame.add(aButton2);
  41. aButton2.setVisible(true);
  42. JTextField inputArea = new JTextField(50);
  43. inputArea.setVisible(true);
  44. aFrame.add(inputArea);
  45.  
  46. Socket socket = new Socket("192.168.0.6", 4243);
  47. PrintStream outStream = new PrintStream(socket.getOutputStream());
  48. }
  49. catch (Exception anException)
  50. {
  51.     System.out.println("Error: " + anException);
  52. }
  53. }
  54.  
  55. public void actionPerformed(ActionEvent e)
  56. {
  57.     String aString = inputArea.getText();
  58.     if(aString.equals("exit"))
  59.         {
  60.             socket.close();
  61.             System.exit();
  62.         }
  63.     else
  64.         {
  65.              outStream.println(aString);
  66.  
  67.         }
  68. }
  69.  
  70. }
  71.  
  72.  
  73.  
Oct 28 '08 #5
JosAH
11,448 Expert 8TB
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:

Expand|Select|Wrap|Line Numbers
  1. class Name extends APossibleOtherClass implements PossibleInterfaces {
  2.    <static variables>
  3.    <static initialization code>
  4.    <static methods>
  5.    <variables>
  6.    <initialization code>
  7.    <constructors>
  8.    <methods>
  9. }
  10.  
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
Oct 28 '08 #6
brendanmcdonagh
153 100+
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
Oct 28 '08 #7
brendanmcdonagh
153 100+
right, got the symbol errors sorted but im back to being stuck with the actionperform() and addactionlistener.

here's my update code

Expand|Select|Wrap|Line Numbers
  1.  
  2. /*
  3.  * Main.java
  4.  *
  5.  * Created on 28 October 2008, 13:19
  6.  *
  7.  * To change this template, choose Tools | Options and locate the template under
  8.  * the Source Creation and Management node. Right-click the template and choose
  9.  * Open. You can then make changes to the template in the Source Editor.
  10.  */
  11.  
  12. package clientside;
  13.  
  14. import java.io.*;
  15. import java.net.*;
  16. import java.util.*;
  17. import javax.swing.*;
  18. import java.awt.*;
  19.  
  20. public class Main implements ActionListener
  21. {
  22.     public JFrame aFrame;
  23.     public JButton aButton;
  24.     public JButton aButton2;
  25.     public JTextField inputArea;
  26.     static Socket socket;
  27.     static PrintStream outStream;
  28.  
  29.     public Main()
  30.     {
  31. aFrame = new JFrame();
  32. aFrame.setSize(500, 500);
  33. aFrame.setTitle("Input your message");
  34. aFrame.setLayout(new FlowLayout());
  35. aFrame.setVisible(true);
  36.  
  37. aButton = new JButton();
  38. aButton.setText("Press to send message");
  39. aButton.setSize(200, 25);
  40. aButton.addActionListener(this);
  41. aFrame.add(aButton);
  42.  
  43. aButton2 = new JButton();
  44. aButton2.setText("Press to clear message");
  45. aButton2.setSize(200, 25);
  46. aFrame.add(aButton2);
  47. aButton2.setVisible(true);
  48.  
  49.  inputArea = new JTextField(50);
  50. inputArea.setVisible(true);
  51. aFrame.add(inputArea);
  52.     }
  53. public static void main(String[] args)
  54. {
  55. try
  56. {     
  57.     socket = new Socket("192.168.0.6", 4243);
  58.     outStream = new PrintStream(socket.getOutputStream());
  59. }
  60. catch (Exception anException)
  61. {
  62.     System.out.println("Error: " + anException);
  63. }
  64. }
  65. public void actionPerformed(ActionEvent e)
  66. {
  67. String aString = inputArea.getText();
  68.     if(aString.equals("exit"))
  69.         {
  70.             socket.close();
  71.             System.exit(1);
  72.         }
  73.     else
  74.     {
  75.         outStream.println(aString);
  76.         aString = inputArea.getText();
  77.     }
  78. }
  79.  
  80. }
  81.  
  82.  
  83.  
here's the errors :

init:
deps-jar:
Compiling 1 source file to C:\ClientSide\build\classes
C:\ClientSide\src\clientside\Main.java:19: cannot find symbol
symbol: class ActionListener
public class Main implements ActionListener
C:\ClientSide\src\clientside\Main.java:64: cannot find symbol
symbol : class ActionEvent
location: class clientside.Main
public void actionPerformed(ActionEvent e)
C:\ClientSide\src\clientside\Main.java:39: addActionListener(java.awt.event.ActionListener) in javax.swing.AbstractButton cannot be applied to (clientside.Main)
aButton.addActionListener(this);
3 errors
Oct 28 '08 #8
JosAH
11,448 Expert 8TB
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
Oct 28 '08 #9
brendanmcdonagh
153 100+
Thanks Jos,

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

Expand|Select|Wrap|Line Numbers
  1.  
  2. /*
  3.  * Main.java
  4.  *
  5.  * Created on 28 October 2008, 13:19
  6.  *
  7.  * To change this template, choose Tools | Options and locate the template under
  8.  * the Source Creation and Management node. Right-click the template and choose
  9.  * Open. You can then make changes to the template in the Source Editor.
  10.  */
  11.  
  12. package clientside;
  13.  
  14. import java.io.*;
  15. import java.net.*;
  16. import java.util.*;
  17. import javax.swing.*;
  18. import java.awt.*;
  19. import java.awt.event.*;
  20.  
  21.  
  22. public class Main implements ActionListener
  23. {
  24.     public static JFrame aFrame;
  25.     public static JButton aButton;
  26.     public static JButton aButton2;
  27.     public static JTextField inputArea;
  28.     static Socket socket;
  29.     static PrintStream outStream;
  30.  
  31.     public Main()
  32.     {
  33. aFrame = new JFrame();
  34. aFrame.setSize(500, 500);
  35. aFrame.setTitle("Input your message");
  36. aFrame.setLayout(new FlowLayout());
  37. aFrame.setVisible(true);
  38.  
  39. aButton = new JButton();
  40. aButton.setText("Press to send message");
  41. aButton.setSize(200, 25);
  42. aButton.addActionListener(this);
  43. aFrame.add(aButton);
  44.  
  45. aButton2 = new JButton();
  46. aButton2.setText("Press to clear message");
  47. aButton2.setSize(200, 25);
  48. aFrame.add(aButton2);
  49. aButton2.setVisible(true);
  50.  
  51.  inputArea = new JTextField(50);
  52. inputArea.setVisible(true);
  53. aFrame.add(inputArea);
  54.     }
  55. public static void main(String[] args)
  56. {
  57. try
  58. {    
  59.     Main aMain = new Main();
  60.     socket = new Socket("192.168.0.6", 4242);
  61.     outStream = new PrintStream(socket.getOutputStream());
  62. }
  63. catch (Exception anException)
  64. {
  65.     System.out.println("Error: " + anException);
  66. }
  67. }
  68. public void actionPerformed(ActionEvent e)
  69. {
  70. String aString = Main.inputArea.getText();
  71.     if(aString.equals("exit"))
  72.         {
  73.             //socket.close();
  74.             System.exit(1);
  75.         }
  76.     else
  77.     {
  78.         Main.outStream.println(aString);
  79.         aString = Main.inputArea.getText();
  80.     }
  81. }
  82.  
  83. }
  84.  
  85.  
  86.  
Now to learn 2 way chat and multi threading!!

Thank you for your help Jos

Regards

Brendan
Oct 28 '08 #10
JosAH
11,448 Expert 8TB
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
Oct 28 '08 #11
I know what you mean Jos... I know what you mean. T_T
Nov 16 '10 #12
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:

Expand|Select|Wrap|Line Numbers
  1. ActionListener al = new ActionListener() {
  2.       public void actionPerformed(ActionEvent e) {
  3.           if (e.getActionCommand().equals("New Game")) {
  4.           System.out.println("New Game");
  5.         } else if (e.getActionCommand().equals("Solve")) {
  6.           System.out.println("Solving");
  7.         }
  8.       }
  9.     };
  10.  
  11.     Button b = new Button();
  12.     b.setName("New Game");
  13.     b.setActionCommand("New Game");
  14.     b.addActionListener(al);
  15.  
  16.     Button b1 = new Button();
  17.     b1.setName("Solve");
  18.     b1.setActionCommand("Solve");
  19.     b1.addActionListener(al);
  20.  
removing the need to add the void actionPerformed(ActionEvent e) method
Nov 19 '10 #13

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

11 posts views Thread by Dave Rahardja | last post: by
4 posts views Thread by daniel.w.gelder | last post: by
7 posts views Thread by greenflame | last post: by
5 posts views Thread by Nick Flandry | last post: by
10 posts views Thread by Mihai Osian | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.