Hi there,

Im not very good with coding UIs but having to do one for a program im writing atm....

I have two windows in seperate classes and they both extend JFrame.

From Window A i open Window B from the menu options but when i then want to close Window B, Window A automatically closes as well (as if a System.exit(0) is called)....

How can i set it up so that i can close Window B and keep Window A open?

Cheers,

Logi.

You probably have the default close operation set to EXIT_ON_CLOSE, which will indeed call System.exit(0). You can instead set that to DISPOSE_ON_CLOSE or another option of your choosing with the JFrame.setDefaultCloseOperation(int) method. The available constants are listed there in the API doc.

P.S. Two full-fledged Frames in an application is usually a mistake. Especially when one is opened as a result from the other. Usually that "second" Frame should be a Dialog (i.e. extend JDialog rather than JFrame in the "second" Class).

P.S. Two full-fledged Frames in an application is usually a mistake. Especially when one is opened as a result from the other. Usually that "second" Frame should be a Dialog (i.e. extend JDialog rather than JFrame in the "second" Class).

Very interesting. Why is that?

A Frame is meant to encapsulate an application. Something that is only temporarily open, and especially when it is a part of the same application as the previous frame, it should, more correctly, be a Dialog. A JDialog can contain anything a Frame can, so you don't lose anything. The only thing it doesn't have is the minimise/maximise buttons. And, you also don't need to "be careful" about what default close operation you give, since you don't give it any. But you can still assign a WindowListener to it, in case you do want to do something specific when it closes.

Thanks for that masijade.

It really depends on how you want your application to behave. It's not always a bad choice to have multiple top-level windows. Sometimes it can be very desirable. Our particular application here has many components that open in separate frames and it's very convenient for our users. Each of these frames is a separate component working within the context of a particular data analysis project and can receive notifications from a central application controller for purposes of data stream navigation and coordination. The users all have multiple monitor set-ups and arrange the various components as they like for a particular task.

That said, I would agree that it's not a good choice to create and destroy the top-level windows for the purpose of switching screens in an app that is designed to operate in a single screen context and does not utilize multiple windows open at one time.

I agree. Like I said, usually. ;)

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.