Hi, I have an application that has the feature to change the users with the click of a button.

For the first attempt it works fine; it closes the JFrame and opens the login JFrame. However if the users clicks on the button to change user for the secound time, the main JFrame stays open and the login JFrame reopens as it should. Now what I want is to always close the main JFrame when the user clicks the mentioned button.

To close the main frame I am using the following code:

mainPanel.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

And in the action performed I am using the following code:

mainPanel.dispose();

Can anyone please let me know of a better way to avoid this issue.

Thanks

You really haven't posted enough code to show where the problem lies, but from those two lines here's a guess...
are you sure you are updating the mainPanel variable when opening the second main JFrame?

what do you mean by updating the mainPanel variable?

Becuase the JFrame disposes correctly the first time, that shows the mainPanel variable correctly refers to that JPanel. Then you create a new JFrame for the second user, but the dispose doesn't work. So I wondered: is mainPanel still referring to the first JPanel, or do you update it to refer to the new JFrame you just created?

no the mainPanel is still referring to the first JFrame.

How can I update it to refer to the new JFrame?

Thanks

Somewhere you have some code that creates the JFrame - somewhere in there you need a mainPanel = (your new JPanel) - without seeing the code I can't be more explicit.

In the class before all the methods I am using this code:

JFrame mainPanel = new JFrame("FlexBox Quotation");
Container conn;

Now in the method where I am "constructing" the GUI/Frame, I have the following:

conn = new Container();
conn = mainPanel.getContentPane();
mainPanel.getContentPane().revalidate();

please re_read 1st. answer here by @JamesCherrill. especially 1st. sentence there

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.