-
September 22nd, 2011, 04:04 PM
#1
Help with ActionListener?
So I'm basically done with this program I've been working on for awhile...I made the entire thing to run in Eclipse's console, and now I've been setting it up to run in a jFrame/jPanel. I made the jFrame, added some jLabels and and jTextArea and a jTextField. Basically what I need the program to do is take what the user enters into the textfield and then 1. send that line to the text area and 2. set what they entered to a string. I've never used actionlisteners so I'm assuming the way I've written it is completely wrong. At the moment I enter words into the field and hit enter and nothing happens. Here's some snippets of the code (excluding the middle parts because it would be a long paste if I included the whole thing).
Code:
static ActionListener listener = null;
public static void main(String[] args){
-------------unimportant lines------------
AnswerBox.setText("Enter Text Here"); // answerbox is the jTextField declared earlier
AnswerBox.addActionListener(listener); //attempting to declare the action listener? wrongly?
--------much later on-------------
@Override
public void actionPerformed(ActionEvent e) {
jTextArea1.append(AnswerBox.getText());
textEntered = AnswerBox.getText();
if (e.getActionCommand().equals("1")) {
ready = AnswerBox.getText();
}
}
So I understand this is complicated to read but hopefully you can understand what I'm getting at. Basically I need the actionlistener to activate whenever the user hits enter, and to then do the thing determined under action performed. How can I make it work? Thanks for the help in advance.
-
September 22nd, 2011, 05:08 PM
#2
Re: Help with ActionListener?
I need the actionlistener to activate whenever the user hits enter
Where is the listener variable given a value? Once is has a value you will be able to add it as a listener to the text field.
Move most of the code out of the main method into the class's constructor.
Norm
-
September 22nd, 2011, 05:36 PM
#3
Re: Help with ActionListener?
Well I set the listener as null in the beginning as a global variable. I've researched about 4 different sites on using ActionListener and they all say to do different things, and I get errors doing all of them. If by value you mean null, it's declared as a global value right at the beginning. Otherwise I'm not sure what you mean exactly.
As for the moving the code out of the main method, which code exactly. Do you mean the entire jframe should be in it's own class, or do you just mean the actionlistener lines? Thanks again
-
September 22nd, 2011, 06:53 PM
#4
Re: Help with ActionListener?
By giving the listener variable a value I meant giving it the value of a valid listener. null is not a valid listener value.
If you move the code out of the main method, then you will be able to get to the class variables without having to define them as static.
If the class implements ActionListener then the constructor can do this:
jTF.addActionListener(this); // define this class as the listener for the text field: jTF
You would not need to use the listener variable.
Norm
-
September 22nd, 2011, 07:36 PM
#5
Re: Help with ActionListener?
ok well i moved it into its own method outside my main class. doing this got the word "this" to work (it didn't before) but it still does nothing when I click enter. is it supposed to automatically do the action when enter is clicked or do I have to code that in?
Code:
public void actionPerformed(ActionEvent evt) {
AnswerBox.addActionListener(this);
jTextArea1.append(AnswerBox.getText());
textEntered = AnswerBox.getText();
ready = AnswerBox.getText();
}
-
September 22nd, 2011, 07:53 PM
#6
Re: Help with ActionListener?
Does the text field have focus when you press Enter?
Type something into the text field and press Enter.
Where do you add the listener to the text field? I see this inside of the listener's code:
AnswerBox.addActionListener(this);
Why is the above code inside of the listener method?
Norm
-
September 23rd, 2011, 09:33 PM
#7
Re: Help with ActionListener?
hey sorry for taking so long to respond back. I put it inside the listener method because that was the only way i could do it where the word "this" wasn't underlined in red. When i put it in the main class it says "cannot use this in a static context." So I don't know what to do. Also, I put in a System.out line in the actionPerformed method and it was never printed, so that method is not currently being called.
-
September 24th, 2011, 06:14 AM
#8
Re: Help with ActionListener?
Can you show all your code, or if it is very long a cut down version that shows what you are actually doing.
For instance what is AnswerBox? As it has an upper case first letter it should be a class but I'm guessing it's your text field.
The general way of using ActionListners is:
- Create an instance of your component.
- Create an instance of your action listener.
- Add the action listener instance to your component
- Add your component to your GUI
-
September 24th, 2011, 09:44 AM
#9
Re: Help with ActionListener?
Hi, here is a pastebin link of my entire script. Like I said I've never used action listeners before so I have no idea how to implement them, everything I've read is very confusing.
http://pastebin.com/UG6ZtK0W
I made the script in order to quiz you on the polyatomic ions of AP Chemistry, I was bored and needed some more experience in programming. And yes, AnswerBox is the textfield. I tried something new by using netbeans IDE to create the GUI and then changed its code around but it happened to capitalize the A when it created it.
-
September 24th, 2011, 10:17 AM
#10
Re: Help with ActionListener?
Putting virtually all your code in your main method and having all your variables as static goes against all OO programming techniques and is the root cause of all your problems.
Also your code appears to be trying to use different approaches to adding an ActionListener. You need to choose just one approach. The easiest way to get your actionListener working is to do the following:
- Delete the actionPerformed method in the CalcReview class.
- Remove the local declarations of jLabel3, jLabel3, jTextField1 in the main method
- Create a CalcReview constructor and move all the code in the main method into it.
- Add the line new CalcReview(); to the now empty main method
- Remove the AnswerBox.addActionListener(this); line from the actionPerformed method in the keyEvents class.
- Remove the implements ActionListener from the CalcReview class declaration.
- Add the AnswerBox.addActionListener(this); line to the line after the line which assigns a JTextField to AnswerBox.
- Remove static from all your class variables
-
September 24th, 2011, 08:56 PM
#11
Re: Help with ActionListener?
Hi,
Thanks for all off your help, I just did everything you said. However, a few questions I have:
-you said to put the .addActionListener(this) line into the constructor, but when I do that the line is highlighted because the constructor doesn't implement ActionListener, but then when I make it implement it it doesn't allow that. I must have typed something wrong in one of the methods I guess? here is the updated version:
http://pastebin.com/BBf6sDEV
The way it's currently written, the only error I get is the addActionListener line, which says "The method addActionListener(ActionListener) in the type JTextField is not applicable for the arguments (CalcReview)" I'm assuming that's because it's not implemented.
My other question is, since I took all of the originally global, static variables and made them non-static and put them into the constructor, now if I want to use them in the other class, KeyEvents, they will not be recognized. How would I fix this? Thank you again SO MUCH for all of your help, I'm still learning and it's made such a difference already.
-
September 24th, 2011, 09:02 PM
#12
Re: Help with ActionListener?
because the constructor doesn't implement ActionListener
Its the class that should implement ActionListener not the constructor
I took all of the originally global, static variables and made them non-static and put them into the constructor
The definitions of the variable should be at the class level, not inside of the constructor.
They can be given values in the constructor.
Norm
-
September 24th, 2011, 09:07 PM
#13
Re: Help with ActionListener?
If you continue to have trouble, consider making a very small simple program with minimum features that you can post here that will compile and execute and demonstrate your problem.
Norm
-
September 24th, 2011, 09:22 PM
#14
Re: Help with ActionListener?
Alright I have an idea. Heres a small test program I just made, WITHOUT an action listener. If either of you could take it, then add the actionlistener so that the jTextField's text gets read and sent to the jTextArea, then post it back here so I can take a look at it. I feel like if I could see how the basics are done I will understand what I am doing wrong and I'll be able to fix my main program. I think this will solve my problem the easiest way. Thanks.
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestProgram {
public static void main(String[] args) {
new TestProgram();
}
public TestProgram(){
JFrame frame = new JFrame("Test");
JPanel pane = new JPanel(new BorderLayout());
frame.getContentPane().add(pane);
frame.setSize(200,200);
pane.setSize(200,200);
frame.setVisible(true);
javax.swing.JTextField jTextField1;
javax.swing.JTextArea textArea;
textArea = new JTextArea();
jTextField1 = new JTextField(10);
pane.add(jTextField1, BorderLayout.NORTH);
pane.add(textArea, BorderLayout.SOUTH);
}
}
-
September 25th, 2011, 08:25 AM
#15
Re: Help with ActionListener?
Go to this site and read about how Action Listeners:
http://download.oracle.com/javase/tu...nlistener.html
Then change your simple program as per the tutorial.
Norm
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|