tags:

views:

53

answers:

3

I'm new at Java and I'm trying to do what the title says, my application has page A, a jpanel with some controls and a specific button, and when the user clicks the button i want page A to disappear and page B to appear (page B has controls that depend on the choices that are made by the user on page A).

This has been asked before but there was no satisfactory answer. In the ActionListener implementation, namely public void ActionPerformed(ActionEvent e) from my jpanelForPageA class i can comfortably write this.setVisible(false), but how can i set page B to a visible state?

A: 

For some reason, I can never to get setVisible() to work for me to do what you're describing. Instead, I do this:

frame.remove(panelA);
frame.add(panelB);

"frame" is just the JFrame you want to put the panels in. Try this if the setVisible() method doesn't work :)

To your original question, all you have to do is (like aioobe said):

panelB.setVisible(true);

((btw, posting some of your code would help me figure out what you're trying to ask))

And this is just a guess as to what you're trying to do -- I'm guessing your JPanels are in different classes. Then, you'll need to do this:

class pages extends JFrame implements ActionListener
{
    public pages()
    {
        panelA a = new panelA(this)
    }

    changeToA(panelB b)
    {
        remove(panelB);
        add(new panelA(this));
    }        

    changeToB(panelA a)
    {
        remove(panelA);
        add(new panelB(this));
    }
}

class panelA extends JPanel implements ActionListener
{
    pages p;
    public panelA(pages p)
    {
        this.p = p
    }
    // all that actionlistener code stuff
        p.changeToB(this);
}

class panelB extends JPanel implements ActionListener
{
    pages p;
    public panelB(pages p)
    {
        this.p = p
    }
    // all that actionlistener code stuff
        p.changeToA(this);
}

You pass the pages class to the panels so the panels can tell the pages class to remove themselves. ((I don't know if there is an easier way, but this is what I do all the time))

I hope I helped :)

DDP
+1  A: 

You can do the removal of panel a and then the addition of panel b trick. Another is to use a CardLayout.

When you create your panels, you add them to a containing JPanel that you initialize with a CardLayout:

JPanel container = new JPanel(new CardLayout());
containter.add(getPanelA(), "PANEL_A"); 
containter.add(getPanelB(), "PANEL_B"); 

Then, in your actionPerformed, when you want to show panelB, you do this:

CardLayout cl = (CardLayout) container.getLayout();
cl.show("PANEL_B");

Take a look at this tutorial for some more ideas.

akf
Oh, how interesting. That method looks so much more efficient than the one I outlined for this question. +1 :D
DDP
A: 

You have to remove Panel A from the frame, add Panel B to the frame, and call invalidate on the frame (or containing panel). At least in Swing, I'm not sure about AWT, there you might need repaint or revalidate instead of invalidate.

You could also just create a whole new JFrame and dispose the one containing panel A.

Yishai