• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPanel to JPanel in a JFrame

 
Nicky Eng
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from your description, if the panels are added directly to the frame, and
you remove/add the frame's components, you need to call
frame.validate();
after you add the new panel

if you are instead removing/adding from/to a JPanel, you need to call
panel.revalidate();

in both the above, sometimes
repaint();
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
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for your reply.
i use the wrong object references.
hehe.
thank you anyway,really.
thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic