faisal
faisal

Reputation: 11

Need a solution to press a button in the first jframe and it transfers me to another jframe

I'm using netbeans for Java and I have created a file including 2 JFrames and I have placed a button in the first JFrame , and I want to know how do I make this button transfer me to the other JFrame?

Upvotes: 1

Views: 794

Answers (3)

trashgod
trashgod

Reputation: 205785

@AlexR is correct and I prefer @Hovercraft Full Of Eels approach, but you might also like to experiment with this example of buttons and frames.

Upvotes: 1

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

Rather than swapping out actual windows on a screen, I suggest that you have one main JFrame application and that you swap out JPanels that it displays based on the state of the application. This is more in line with most professional applications that we use including word processors and IDEs and will seem more natural-seeming to the user. To achieve this in Java look into having your Container (usually a JPanel that holds other JPanels and components) use a CardLayout. Then you would treat the JPanels it holds as cards and swap them via methods in the layout. The exception to this is when you need a dialog window on top of the main GUI, and this can be achieved simply by use of one of the JOptionPane methods, or in more depth by creating and using a modal or non-modal JDialog.

CardLayout Tutorial Link

Upvotes: 2

AlexR
AlexR

Reputation: 115338

The following magic lines do what you want. Obviously frame there is the target frame, i.e. one you want to move focus to.

frame.requestFocus();
frame.toFront();

Upvotes: 2

Related Questions