473,915 Members | 6,883 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

AddActionListen er method

153 New Member
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 7579
JosAH
11,448 Recognized Expert MVP
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 New Member
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 Recognized Expert MVP
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 New Member
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 Recognized Expert MVP
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 New Member
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 New Member
right, got the symbol errors sorted but im back to being stuck with the actionperform() and addactionlisten er.

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\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
Oct 28 '08 #8
JosAH
11,448 Recognized Expert MVP
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 New Member
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

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

Similar topics

11
3644
by: Dave Rahardja | last post by:
OK, so I've gotten into a philosophical disagreement with my colleague at work. He is a proponent of the Template Method pattern, i.e.: class foo { public: void bar() { do_bar(); } protected: virtual void do_bar() {} };
5
15395
by: Chris | last post by:
Hi I have a scenario where I've created another AppDomain to dynamically load a DLL(s) into. In this newly loaded DLL I want to call a static method on a class. The problem arise is that I have the same class/static method definition statictly linked to my EXE and when I call InvokeMember(...), even though I got the Type from the new AppDomain, it calls the static method that I am staticly linked to and not the static method in the dynamicly...
4
2913
by: daniel.w.gelder | last post by:
I wrote a template class that takes a function prototype and lets you store and call a C-level function, like this: inline string SampleFunction(int, bool) {..} functor<string (int, bool)> myFunctor = SampleFunction; string result = myFunctor(7, true); Works great thanks to the help from this group. Here's the guts so far for two-arity:
7
1757
by: greenflame | last post by:
I am trying to make a matrix object. I have given it some properites. I am trying to add a method. When I call the method by Test.showDims(...) I want to only enter one input, that is the method by which to do it. As you can see from the object definition that it corresponds to a function that takes two inputs. When I try to run the script it does nothing. So what is wrong and how do I fix it? Here is the script. function...
5
3449
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS 5), but fails on IIS 6 running on a Win2003 server. The web uses Pages derived from a custom class I wrote (which itself derives from Page) to provide some common functionality. The Page_Load handler the failing webpage starts out like this: ...
18
4772
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the class where only the "searched" property has a value. I expected to get the index into the arraylist where I could then get the entire class instance. However, the 'indexof' is never calling my overloaded, overrides Equals method. Here is the...
10
3394
by: Mihai Osian | last post by:
Hi everyone, Given the code below, can anyone tell me: a) Is this normal behaviour ? b) If it is, what is the reason behind it ? I would expect the A::method(int) to be inherited by B. Compiler: gcc 4.1, Linux Thanks,
9
5862
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to the static Parse method of the conversion class. if (InValue is string) return T.Parse((string)InValue); else return base.ConvertFrom(context, culture, InValue);
3
1360
by: allendowney | last post by:
Hi All. In a complex inheritance hierarchy, it is sometimes difficult to find where a method is defined. I thought it might be possible to get this info from the method object itself, but it looks like maybe not. Here is the test case I tried: class A(object):
0
10039
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9881
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11354
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11066
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9732
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8100
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7256
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.