I am trying to display a swing dialog using JFrame, while being in EDT thread with the check of SwingUtilities.isEventDispatchThread(). When in main thread, the components get correctly painted by SwingUtilities.invokeLater() method, but when inside EDT thread, components added to the frame are not appearing. Below is the code:
private void showDialog(String title) {
frame = new JFrame();
frame.setTitle(title);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JButton cancelLogin = new JButton("cancel login");
cancelLogin.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
fireLoginCanceledEvent();
disposeFrame();
}
});
JPanel bottomPanel = new JPanel();
bottomPanel.add(cancelLogin);
panel.add(bottomPanel, BorderLayout.SOUTH);
JLabel info = new JLabel("login initiated...");
panel.add(info, BorderLayout.NORTH);
frame.getContentPane().add(panel);
frame.setLocationRelativeTo(null);
frame.setSize(300, 100);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
private synchronized void disposeFrame() {
if (frame != null) {
if (frame.isDisplayable()) {
frame.dispose();
}
public void display() {
if (SwingUtilities.isEventDispatchThread()) {
showDialog(windowTitle); // Components not appearing
frame.revalidate();
frame.repaint();
} else {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
showDialog(windowTitle); // Components are correctly rendered
}
});
}
}