0

I am doing a program and I need that a JFrame change their components according to one seleccion in a main JFrame, I tried to do so:

public void agregarPanelSegunPrueba(FrmBoleto frm)
{
    //this panel is a JPanel make with the graphical editor in netbeans
    PnlPruebaDCExesoVelocidad pnl = new PnlPruebaDCExesoVelocidad();
    pnl.repaint();
    pnl.revalidate();
    frm.getContentPane().remove(frm.getPnlPruebasDistanciaTiempo());
    frm.getContentPane().add(pnl);
    frm.pack();
    frm.setVisible(true);

}

somebody know which is wrong here. Thanks in advance.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Premier
  • 766
  • 2
  • 10
  • 20
  • 2
    Look into using a [`CardLayout`](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) instead of adding an removing entire panels. You can see a simple example [here](http://stackoverflow.com/a/21460065/2587435) – Paul Samsotha Mar 07 '14 at 12:20
  • I want to make the JPanel with netbeans editor without cardlayout. thanks – Premier Mar 07 '14 at 12:23
  • 1
    It still looks like a good situation for a `CardLayout`. See [**this answer**](http://stackoverflow.com/a/21898439/2587435) for how to use `CardLayout` in the design view of Netbeans GUI Builder. – Paul Samsotha Mar 07 '14 at 12:25
  • you are right. I think that is a better option. – Premier Mar 07 '14 at 12:36

2 Answers2

1

You need to revalidate();repaint(); not JPanel which you add,but just container to that you add your panel.

You need to call:

frm.getContentPane().revalidate();
frm.getContentPane().repaint();

instead of :

pnl.repaint();
pnl.revalidate();

after that line: frm.getContentPane().add(pnl);

alex2410
  • 10,904
  • 3
  • 25
  • 41
  • does not work, but don't matter I going to use the cardlayout. thaks a lot – Premier Mar 07 '14 at 12:52
  • @Premier to call revalidate(); & repaint(); once time, as last code line, after all changes to Swing GUI are executed – mKorbel Mar 07 '14 at 13:17
  • @mKorbel seems that there other problem because `frm.pack();` solves revalidate problem – alex2410 Mar 07 '14 at 13:24
  • right [valid/only in the case that you don't want to change JFrames bounds, stays unchanged](http://stackoverflow.com/a/6989230/714968), and its contents can be schrinked – mKorbel Mar 07 '14 at 13:58
0
public void agregarPanelSegunPrueba(FrmBoleto frm)
{
    //this panel is a JPanel make with the graphical editor in netbeans
    PnlPruebaDCExesoVelocidad pnl = new PnlPruebaDCExesoVelocidad();
    pnl.repaint();
    pnl.revalidate();
    frm.getContentPane().removeAll();
    frm.getContentPane().add(pnl);
    frm.pack();
    frm.setVisible(true);

}
rpax
  • 4,468
  • 7
  • 33
  • 57