my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes JPanel to JPanel in a JFrame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JPanel to JPanel in a JFrame" Watch "JPanel to JPanel in a JFrame" New topic

JPanel to JPanel in a JFrame

Nicky Eng
Ranch Hand

Joined: Mar 26, 2005
Posts: 378
i have a JFrame, inside it i have 2 JPanel.

1st panel will appear in JFrame once i running it.
then i click one button in 1st panel, 2nd panel appear in the JFrame and 1st one will be gone.

the 1st panel appear. but once i click on the button in the 1st panel it cannot go to 2nd panel, but the frame is still there with empty since 1st panel i setVisible(false).

how to make the 1st panel to 2nd panel successfully?

note: i separate the 2 panels and jframe, means i got Panel1 class , Panel2 class , and ABC frame class.

From NickyEng
Diploma in Computer Studies
SCJP 1.4
Formula 1 app by Maxis (Playbook)
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
from your description, if the panels are added directly to the frame, and
you remove/add the frame's components, you need to call
after you add the new panel

if you are instead removing/adding from/to a JPanel, you need to call

in both the above, sometimes
also needs to be called

another option for you is to create an additional JPanel, and set it as a
CardLayout(). you then add both your other panels to this cardlayout panel
and use the button to switch between panels.
Nicky Eng
Ranch Hand

Joined: Mar 26, 2005
Posts: 378
thank you.
but i have another question.

i have some textfields in the 1st panel. when i click on a button to save into txt file, the textfields.getText() is not working. and empty space is save into txt file which i DO key in words into textfields.

why that happens?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
for questions like this you should be posting a sample program - just a frame,
textfield, button and actionListener code to save the text to a file.

without a sample program (which shows us how you have put it all together), all you can get is guesses.

guess (1) - duplicate declaration of the textfield

in the above, the 'tf' in the constructor is the one seen on the screen,
but it is not the one referenced in actionPerformed(), and will always save ""

guess (2) - do you close the file after you save the tf.getText()
Nicky Eng
Ranch Hand

Joined: Mar 26, 2005
Posts: 378
thank you for your reply.
i use the wrong object references.
thank you anyway,really.
I agree. Here's the link:
subject: JPanel to JPanel in a JFrame
Similar Threads
JFrame problem
[Netbeans] Integrating external Jpanel to a Jframe
How do you use javax.swing.Popup?
2 dimensional JTabbedPane
Event Handleing Help ...