Reputation: 81
I have created an applet where when you press the button "Forgot Pass" I erase the current JPanel on the applet & create a new JPanel that displays the JComponents related to Retrieving/Forgetting a password.
I can successfully clear the JPanel using .removeAll(); BUT after I create all my new JComponents & add them to the content pane (main JPanel) the applet just goes gray & doesn't show the new JPanel & components UNLESS I resize the applet, then it repaints & works.
I have tried putting .invalidate() after I have created all the new JComponents but that still doesn't refresh the applet?
How can I make my JPanel appear after I clear it with .removeAll() & add different JComponents to it?
Code:
public class App extends JApplet
{
JPanel mainPanel;
public void init()
{
SwingUtilities.invokeAndWait( new Runnable() {
public void run()
{
showLoginPanel(); // this shows fine on loading
}
});
}
public void showForgotPassPanel()
{
mainPanel.removeAll();
mainPanel = (JPanel) getContentPane();
Box hBox = Box.createHorizontalBox();
Box vBox = Box.createVerticalBox();
mainPanel.setLayout( new BorderLayout() );
... create components
... add components to mainPanel
mainPanel.invalidate(); // doesn't make new layout visible, not unless I resize the applet
}
}
Upvotes: 5
Views: 12033
Reputation: 1
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import java.awt.HeadlessException;
public class PruebaVentana extends JFrame {
JButton b1, b2;
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel contenedor = new JPanel();
public PruebaVentana() throws HeadlessException, InterruptedException {
super("Abriendo nuevo JPanel por medio de un botón");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setBounds(50, 50, 500, 300);
iniciarPanel();
}// fin de constructor PruebaVentana
public void iniciarPanel() {
this.getContentPane().add(contenedor);
contenedor.setLayout(null);
contenedor.add(this.panel1);
contenedor.add(this.panel2);
panel2.setBackground(new Color(0, 10, 150));
panel1.setBackground(new Color(150, 0, 0));
panel1.setBounds(0, 0, 500, 230);
panel2.setBounds(0, 0, 500, 230);
panel1.setLayout(null);
panel2.setLayout(null);
panel2.setVisible(false);
b1 = new JButton("Abre Panel 2");
b2 = new JButton("Abre Panel 1");
b1.setBounds(10, 10, 150, 30);
b1.setForeground(new Color(200, 100, 50));
b2.setBounds(170, 10, 150, 30);
b2.setForeground(new Color(50, 100, 200));
panel1.add(b1);
panel2.add(b2);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel2.setVisible(true);
panel1.setVisible(false);
}// fin de actionPerformed
});// fin de actionListener
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel1.setVisible(true);
panel2.setVisible(false);
}// fin de actionPerformed
});// fin de actionListener
}// fin de iniciarPanel
public static void main(String[] args) throws HeadlessException, InterruptedException {
PruebaVentana v = new PruebaVentana();
v.setVisible(true);
}// fin de método main
}// fin de clase PruebaVentana
Upvotes: 0
Reputation: 285405
Another "clean" option is to swap views with a CardLayout. It does all the dirty work for you behind the scenes.
Upvotes: 0
Reputation: 762
Use mainPanel.revalidate();
and/or mainPanel.repaint();
methods.
Upvotes: 5