aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Using the 'Enter' button in an ActionListener method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Using the Watch "Using the New topic
Author

Using the 'Enter' button in an ActionListener method

Simon Evans
Ranch Hand

Joined: Mar 27, 2008
Posts: 93
Dear Programmers,
Much thanks for your help proffered thus far.
I have now gotten the code to work.
There is one item that stops it from running as I had wanted it to, though.

This would be to get anything written to the GUI (as generated by class ClientGUI) to transfer from the textfield to the GUI's text area by pressing the 'Enter' key, as well as by pressing the 'SEND' button on the GUI.

I have tried putting an 'OR'(||) in the Listener in class ClientGUI, and as the source of the ActionPerformed object as instantiated, should it equate with a TextField input (as when the Enter key is hit), ie:

if(label.equals("SEND")||e.getSource() == TeF)
{

sendMessage();
}
might effect the sendMessage() method,but though this compiles and runs ok, it doesn't work, or rather it half works - as the Gui's 'SEND' button still transfers text from TextField TeF to TextArea TeA, but hitting the 'Enter' after typing to TextField, gives no response.

As said, the code compiles and runs okay, if you would wish to try it out. The only drawback is getting the coding to effect text transfer upon the pressing of the 'Enter' key.

Thank you for your assistance (code is as below).

Yours

Simon






import java.io.*;
import java.net.*;

class Connection extends Thread
{
Socket connection;
BufferedReader in;
PrintStream out;
String message;


Connection(Socket connection)
{
this.connection = connection;
try
{
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

out = new PrintStream(connection.getOutputStream());
}
catch(IOException e)
{
System.out.println("Connection - Connection():" +e);
}
start();
}
public void run()
{
try
{
while(true)
{
message = in.readLine();
sendMessage();
}
}
catch(IOException e)
{
System.out.println("Connection - run(): " + e);
}
}
private void sendMessage()
{
out.println(message);
}
}

[/code]


Simon
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 687
    
    1
Here's some code I wrote long ago two work with AWT. I think Swing might have better ways.



Swing has the method: registerKeyboardAction() that looks like it would solve your problem.
[ July 28, 2008: Message edited by: Norm Radder ]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
when you post code, it is much better if you don't post your program as most
of the code will be unrelated to the question/problem. As in all your socket code
having nothing to do with the question.

instead, take the time to create another demo program with just enough to
produce the problem, so we can copy/paste/compile/run and see the problem.

this is all you needed to post (frame with textfield/textArea/button)
(and have included the 1-line addition, which should be what you're after)

Gulsum Ramazanoglu
Ranch Hand

Joined: Jun 27, 2008
Posts: 51
Hi Simon

If I didnt get the point wrong, this link helps you....especially I have benefited Rob Prime's reply..

Have a good day
Simon Evans
Ranch Hand

Joined: Mar 27, 2008
Posts: 93
Dear Programmers,
Thank you for your advice.I thought it would be helpful to write seperately to those who wrote to me, so am replying to you by name, in order of reply:-

Dear Norm Radder, Thanks for the gen on registerKeyboardAction(); I am going to have a look at it on the Sun/Java site now. As I have gone this far using swing, it'd be beyond me to reprogram it in AWT - I am only a mere mortal.

Dear Michael Dunn:-
For better or worse I put in the Server program as I was asked previously why I wanted to thread ClientGUI, ie subclass it from Thread without a run method - I considered that in running a thread (in class Servers instantiation of class Connection) in response to ClientGUi, class Server's inclusion would help explain why I wanted to subclass ClientGUI
from Thread. Perhaps it doesn't explain any such thing - but at present that is my understanding of the program/s.
I have tried to incorporate the code you suggested ie:

); f.getRootPane().setDefaultButton(sendButton);//<---------works for ENTER

into the ClientGUI programs setupGUI method, see below, but though it compiles, when I run it, I get a 'null pointer exception' return. The GUI instantiates, but again only its 'SEND' button effects text transfer - and not the 'Enter' key.

I accept that I might have used it wrongly, but from the code it suggests to me this line of code should go into the buildGUI, being three curly brackets down or up - placing it within method buildGUI's statement.

Hopefully you can tell me where I am going wrong.

I am putting the code below as I understood it ought be placed (notwithstanding the fact it doesn't run to plan) in ClientGUI, below:-





Dear Guslum Ramazanoglu :-
I've tried Rob Prime's ActionTest program, but don't think it applies to what I am seeking. If I (or rather my computer) understands it rightly, it produces a GUI with two buttons, which don't really do that much?

I want the text transfer (from Gui's TextField to its TextArea) to be effected by the pressing of the'Enter' button on the keyboard as much as the 'SEND' button instantiated on the Gui.

Thank you for your assistance.

Yours

Simon.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

You all sure know how to make things complicated. Simon, did you read Michael's post? That should work. The following will also work:



GenRocket - Experts at Building Test Data
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
your NPE is related to
class ClientGUI extends JFrame
and
private JFrame frame;

in this, 'frame' is null, and is only referenced by
frame.getRootPane().setDefaultButton(b1);

the rest of your code references the extended JFrame e.g.
setSize(400,380);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

change
frame.getRootPane().setDefaultButton(b1);
to
getRootPane().setDefaultButton(b1);
Simon Evans
Ranch Hand

Joined: Mar 27, 2008
Posts: 93
Dear Programmers,
Thank you for your assistance. You have put me out of my misery - for the present.I thought it'd be best to reply to each sender in the order of their response:-


Dear Michael Dunn,

Thank you for your advice - it works, don't know how though, - but who's
complaining?


Dear Norman Radder,
Incidentally, the method registerKeyboardAction(); is now obselete, according to the Sun Java site, and apparently has been replaced by getActionMap().


Dear Gregg Bolinger, I have checked out Michael's post (see above). I tried running class Testing, but Compiler returned the following:-

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\simon.BLUECHIP-KDOCDA>cd C:\JavaRanch Code

C:\JavaRanch Code>set PATH=c:\Sun\AppServer\jdk\bin;%PATH%

C:\JavaRanch Code>javac Testing.java
Testing.java:43: cannot resolve symbol
symbol : variable textfieldText
location: class Testing
ta.append(textfieldText+"\n");
^
Testing.java:43: cannot resolve symbol
symbol : variable ta
location: class Testing
ta.append(textfieldText+"\n");
^
Testing.java:44: cannot resolve symbol
symbol : variable tf
location: class Testing
tf.setText("");
^
3 errors

C:\JavaRanch Code>


Anyway, much thanks for your help.

Yours

Simon.
 
wood burning stoves
 
subject: Using the 'Enter' button in an ActionListener method