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.
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
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.
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?
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.
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.
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.
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.
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.
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.
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);
}
}
Bookmarks