I'm learning right now the basics of the ActionListeners and I've been searching for help over here but can't quite find/figure what I'm doing wrong.
I've got a class (Client) which implements the main call:
...
public static void main(String[] args) {
Myframe test = new Myframe();
N = test.setVisible(); // N is an integer
...
}
Then the code from my frame:
public class test extends JFrame {
private JPanel contentPane;
private int N;
public int setVisible(){
this.setVisible(true);
return N;
}
/**
* Create the frame.
*/
public test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JButton btnOk = new JButton("OK");
btnOk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
N=5;
dispose();
}
});
contentPane.add(btnOk, BorderLayout.SOUTH);
}
}
And the problem: the program doesn't wait for the button to be pressed before keep going and N results in some trash value thus giving error. What should I do to make it handle correctly it without sleeping the thread?
Some ways to fix this. Use a JDialog - provides modal blocking by default, Listener mechanism - call back with value later, or make your code blocking
public class test extends JDialog {
...
private int N;
public int setVisible() {
this.setVisible(true);
return N;
}
public test() {
super(null, ModalityType.APPLICATION_MODAL);
// <== pass parent window here if you have one, you don't seem to..
...
}
Code
public class test extends JFrame {
....
private CountDownLatch latch;
private int N;
public int setVisible() throws InterruptedException{
latch = new CountDownLatch(1);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
setVisible(true);
}
});
latch.await(); // <== block until countDown called
return N;
}
public test() {
...
btnOk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
N=5;
latch.countDown(); <== will unblock await() call
dispose();
}
});
...
}
}
public class test extends JFrame {
...
private Listener listener;
public static interface Listener {
void setN(int n);
}
public void setVisible(Listener listener) throws InterruptedException {
this.listener = listener; // <== save reference to listener
setVisible(true);
}
public test() {
...
btnOk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
listener.setN(5); // <== call listener
dispose();
}
});
}
Use a modal JDialog
instead of a JFrame
, which are designed to block at the point they are made visible until they are closed...
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JDialog frame = new JDialog();
TestPane testPane = new TestPane();
frame.setTitle("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(testPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
System.out.println("The value was - " + testPane.getValue());
}
});
}
public class TestPane extends JPanel {
private int n;
public TestPane() {
JButton btnOk = new JButton("OK");
btnOk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
n = 5;
SwingUtilities.windowForComponent(TestPane.this).dispose();
}
});
}
}
public int getValue() {
return n;
}
}
Take a look at How to Make Dialogs for more details
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.