I am a little bit confused on exactly how to program this. I have a class extending JFrame with two textfields. I want to be able to get input from JFrame. Somehow just wait for the input from my mainMaybe class…Any ideas?
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class mainMaybe extends JFrame{
JTextField login;
JTextField pass;
public mainMaybe() throws InterruptedException{
login = new JTextField();
pass = new JTextField();
pass.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
System.out.println("DO SOMETHING");
}
});
add(login,BorderLayout.NORTH);
add(pass,BorderLayout.SOUTH);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void returnEmail(){
//SOME HOW WAIT FOR THE ACTION LISTENER?
}
public static void main(String[] args) throws InterruptedException {
new mainMaybe().returnEmail();
//CONTROL SHOULD STAY WITH mainMaybe until an email is returned
}
}
Java Swing is based on the concept of
event-driven architecture. As such, events trigger actions and based on these events you would perform some kind of operation.Using Swing Component would be a good point to start to understand and consequently implement what you want to achieve in terms of your UI and its corresponding behavior. So I would suggest that you read a bit about it and then try to tackle what you’re trying to achieve.
I’m not so sure of what you’re trying to achieve but if I were you, I would have two text fields, maybe some labels attached to it and a “Submit” button. And I would add an
ActionListenerto this button to perform some kind of action.Maybe an SSCCE might help you get started. So have a look at this code:
Note: I generated this code using NetBeans IDE, so I didn’t have to handle much about the layout or anything. If you want to learn about Layouts, here’s an interesting tutorial: “A Visual Guide to Layout Managers”
The main idea for me was to show you how events are triggered. So what I did was I added an
actionlistenerto my “Submit” button and when someone clicks on the button, I perform some kind of action. Hope you get the picture.