user1868607
user1868607

Reputation: 357

Add panel to a panel

i'm building a Java program. The core of this program is visualized in a JFrame with a JMenuBar and various JMenuItem and JMenu. The point is that I added a centralPanel to all the frame,but if I add something to the centralPanel it shows only if i resize the main frame, reducing it or enlarging it! Here's the code:

This is the constructor:

    public UserFrame(Sistema system)
    {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    this.setSize(screenSize.width, screenSize.height);
    storicoPanel = new JPanel();
    carrelloPanel = new JPanel();
    carrelloFrame = new JFrame();
    pane = new JScrollPane(storicoArea);
    close = new JButton("Chiudi");
    this.sistema = system;
    menu = new JMenuBar();
    this.setJMenuBar(menu);

    centralPanel = new JPanel();
    add(centralPanel);

Here i added the centralPanel, and here, in an ActionListener, i try to add something to it, but it doesnt' work:

public ActionListener createVisualizzaStorico(final ArrayList<Acquisto> array)
{
    class Visualize implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        storicoPanel.removeAll();
        for(Acquisto a : array)
        {
            Articolo temp = a.getArticolo();
            if(temp instanceof Vacanza)
                storicoPanel.add(new VacanzaPanel((Vacanza)temp));
            else if(temp instanceof BeneDiConsumo)
                storicoPanel.add(new BeneDiConsumoPanel((BeneDiConsumo)temp));
            else if(temp instanceof Cena)
                storicoPanel.add(new CenaPanel((Cena)temp));
            else 
                storicoPanel.add(new PrestazioniOperaPanel((PrestazioneOpera)temp));

        }

        centralPanel.add(storicoPanel);
        centralPanel.repaint();

Could you please help me? Thanks!

Upvotes: 0

Views: 88

Answers (2)

Paul Samsotha
Paul Samsotha

Reputation: 209102

Use a CardLayout instead of trying to add and remove component/panels. It's much cleaner and you don't have to worry about the things that may go wrong, like what you're facing here.

See this example to see how easy and cleaner it is. Also see How to Use CardLayout tutorial


Side Notes

  • A component can only have one parent container. Though I don't think this is causing a problem for you. It's good to know. First I see you trying to add storicoPanel to a JScrollPane, JScrollPane that you never add to the centerPanel. Then you later add the storicoPanel to the centerPanel. The JScrollPane will no longer be the parent after this.

  • I'm not sure what you're using this carrelloFrame = new JFrame(); for, but you're class is already a JFrame, why create another?

  • Just FYI, when adding components dynamically, you need to revalidate() and repaint(). Though, in your situation, I am totally against the adding and removing of components, because this looks like a perfect case for a CardLayout.

Upvotes: 3

JavaTechnical
JavaTechnical

Reputation: 9357

Try these..

centralPanel.updateUI(); // or    
SwingUtilities.updateComponentTreeUI(getRootPane());

  1. Execute your frame code in SwingUtilities.invokeLater()

  2. Instead of repaint() call updateUI() or SwingUtilities.updateComponentTreeUI(getRootPane()) to update the user interface.

Upvotes: 0

Related Questions