0

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
            }
        });
   }
  }
Soumali Chatterjee
  • 309
  • 1
  • 4
  • 13
  • 2
    Post an [mre] demonstrating the problem. Maybe the code that invoke the "display()" method is the problem. – camickr May 13 '20 at 14:06
  • Are you trying to display a dialog window from an applet (when the user presses the _restart_ button in the applet) ? – Abra May 16 '20 at 05:17
  • Have you seen this? [How to open a modal dialog in Java applet?](https://stackoverflow.com/questions/3955772/how-to-open-a-modal-dialog-in-java-applet) By the way, applets were deprecated in JDK 9 and removed in JDK 11 – Abra May 16 '20 at 05:18
  • [SWT](https://www.eclipse.org/swt/) is **not** _Swing_ – Abra May 16 '20 at 07:00
  • @ Abra sure, thanks for pointing out, also, followed https://stackoverflow.com/questions/28647063/updating-swing-components-within-edt without avail – Soumali Chatterjee May 16 '20 at 07:34
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/213988/discussion-between-abra-and-soumali-chatterjee). – Abra May 16 '20 at 08:01
  • its not possible to recreate the scenario through a reproducible program, because, in actual case, main thread creates a complex Swing window, which has a restart button, which should call the above dialog. So, the dialog will get repainted only when other EDT tasks in progess, are completed – Soumali Chatterjee May 16 '20 at 14:47

0 Answers0