-1

I am working on a JAVA desktop application that has two frames, users clicks a button on frame 1, response based on the selected option would be updated to the database. While the response is being updated, frame 2 should be displayed.

If I set the frame 2 to visible before the database update, frame 2 is displayed but with empty content i.e panels of frame 2 are not displayed. But once the database update is completed, the contents of the frame are shown.

button1.addActionListener(new ActionListener()
{
    public void actionPerformed(ActionEvent e)
    {

        frame1.setVisible(false);
                frame2.setVisible(true);
                Utilities.updateToDB("clicked button1");    
            }
        });

Content of Frame 2 should be shown while the database update is taking place or before it.

  • I suggest that you check out JDesktopPane. If you still need help, please provide a more fleshed out example? See [mcve] for tips on creating a working example. – Code-Apprentice Jul 15 '19 at 18:34
  • 1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 2) *"Frame 2 should be shown while the database update is taking place or before it."* Don't block the EDT (Event Dispatch Thread). The GUI will 'freeze' when that happens. See [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for details and the fix. – Andrew Thompson Jul 16 '19 at 00:48

1 Answers1

0

For this particular case I suggest you to use:

A Swing Worker as shown in this example and the tutorial

Something along the lines:

protected String doInBackground() throws Exception {
    try {
        Thread.sleep(5000); //Simulates long running task (your database update)
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return "Updated"; //Returns the text to be set on your status JLabel once the update is done
}

@Override
protected void done() {
    super.done();
    try {
        // Here update your GUI or do whatever you want to do when the update is done.
        textfield.setText(get()); //Set to the textField the text given from the long running task
    } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }
}

And on your ActionListener something like:

private ActionListener listener = (e -> {
    frame2.setVisible(true); //suggested use of cardlayout and display nextCard here.
    worker.execute(); //Initializes long running task
});

However I suggest you not to use multiple JFrames: The Use of Multiple JFrames: Good or Bad Practice? and use a CardLayout instead, for example

Frakcool
  • 10,915
  • 9
  • 50
  • 89