Question:
Java: Do i need to nest actionPerformed?
sezak
2009-08-14 21:46:09 UTC
I have a button called solveIt and a textField called yourAnswer.
I addActionListener to button solveIt.
However, i have several buttons and thus i don't want to create a separate actionPerformed for each. Thus, i create ONE actionPerformed() method that checks for each button.
Inside actionPerformed, if the button is called solveIt, the focus goes to the textField called yourAnswer. I addActionListener to the textField here. But i am already in actionPerformed method. Do i make another actionPerformed function inside? I need to catch the text entered into the textField and read it into a string and compare it with solution, etc.
I don't want to allow the user to simply enter the textfield without pressing on the button solveIt.
How do i solve this problem?
Three answers:
Pramod Kumar
2009-08-17 01:58:03 UTC
//pk Try this



import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;



public class Test2 implements ActionListener {

JFrame f;

JPanel p;

JButton sovelt, btn;

JTextArea yourAnswer;



public Test2(){

f = new JFrame("Testing");

f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

p = new JPanel();

p.setLayout(new GridLayout(3,1));



yourAnswer = new JTextArea ("Press button Sovelt to type your answer");

yourAnswer.setEnabled(false);

JScrollPane sp = new JScrollPane (yourAnswer);

sovelt = new JButton ("Sovelt");

sovelt.addActionListener (this);



btn = new JButton ("Done");

btn.addActionListener (this);



p.add(sp);

p.add(sovelt);

p.add(btn);



f.getContentPane ().add(p);

f.setSize(300,200);

f.setVisible(true);

}



public void actionPerformed (ActionEvent evt){

if(evt.getSource ()==sovelt){ //when event fired from button = sovelt

yourAnswer.setEnabled(true);

yourAnswer.setText("");

JOptionPane.showMessageDialog (f,"Now, you can type your answer");

}

if(evt.getSource ()==btn){ //when event fired from button = btn

//yourAnswer.setFocusable(true);

JOptionPane.showMessageDialog (f,"Your answer is: "+yourAnswer.getText());

yourAnswer.setText ("Press button Sovelt to type your answer");

yourAnswer.setEnabled(false);

}

else if(evt.getSource ()==yourAnswer){//event fired from textarea

JOptionPane.showMessageDialog(f,"textarea");

}

}



public static void main (String[] args) {

new Test2();

}

}
anonymous
2016-04-05 05:40:51 UTC
If you are adding the actionPeformed methods using the addActionListener method of the object, then you do not need to check to see if the button is called "solveIt." It will always be that button since that ActionListener instance is only bound to that object. But to answer your question, you should never need to nest ActionListeners. If you just want to capture the text, then you can just put that code in the textbox's actionPerformed method. Your best bet is probably going to be to add a KeyListener instead because then you can check the value on every key press, not sure if that's what you want or not though.
Fudge
2009-08-14 21:57:04 UTC
Simply disable the text field until the user presses the button 'solvet', then in that action listener made the button enabled


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...