0

I'd need to handle an error condition with the help of a JFrame:

catch (FormatterException e) {
   displayJFrame();
   System.out.println("Execution continues");
}

Basically, within the JFrame I'd enter manually the data that failed in a textarea and click on a JButton (for brevity this is just the JFrame and JButton):

static void displayJFrame() {
    JFrame frame = new JFrame("JFrame recovery window");

    // create our jbutton
    JButton showDialogButton = new JButton("Submit");

    // add the listener to the jbutton to handle the "pressed" event
    showDialogButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            frame.dispose();
        }
    });

    // put the button on the frame
    frame.getContentPane().setLayout(new FlowLayout());
    frame.add(showDialogButton);

    // set up the jframe, then display it
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setPreferredSize(new Dimension(300, 200));
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
} 

I'd need that, when the Button is pressed, the frame is disposed and execution of the main thread continues. Unfortunately it doesn't work like that. The main thread prints the "execution continues" before the frame is disposed. Is there a way to achieve that in Swing/AWT API?

Thanks

Carla
  • 3,064
  • 8
  • 36
  • 65
  • _The main thread prints the "execution continues" before the frame is disposed._ - Can you specify? After you pressed the button? Or regardless of pressing the button? What exactly do you mean by "_before the frame is disposed_"? (I currently cannot test this, so I want to understand what exactly you are seeing) – maloomeister Jun 14 '21 at 10:07
  • 1
    1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) The title itself suggests this would be better suited to a modal `JDialog` or a `JOptionPane`. – Andrew Thompson Jun 14 '21 at 10:18
  • Thanks for your feedback. Well, I've replaced the displayJFrame method with a simple JOptionPane (as in this example https://stackoverflow.com/questions/7765478/how-to-add-text-area-on-joptionpane ) and it worked as expected. That is, the main thread stopped when the JOptionPane became visible – Carla Jun 14 '21 at 12:18

0 Answers0