tags:

views:

169

answers:

2

I'm using netbeans to create a GUI application. I've made a main form with a panel that I want other jPanels I make to be placed in. It seems like this should be simple to do seeing as the create new context menu allows me to make plain java panels. I've made all the variables public on the new frame also.

EDIT:

I have a separate class extending the jPanel that is public, I'm trying to load it into a panel I have on the main GUI using the following code:

private void qmcatActionPerformed(java.awt.event.ActionEvent evt) { 
    qmcat jQmcat = new qmcat(); 
    jQmcat.setVisible(true); 
    jPanel.add(jQmcat); 
    } 
A: 

You'll need to decide what layout to use for the container that holds your nested panels. Here's a very simple grid of panels:

alt text

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class MyPanel extends JPanel {

    public MyPanel() {
        super(true);
        this.setLayout(new GridLayout(1, 1));
        this.setPreferredSize(new Dimension(120, 60));
        this.setBorder(BorderFactory.createLineBorder(Color.blue));
        this.add(new JLabel("Hello, world!", JLabel.CENTER));
    }

    private static void create() {
        JFrame f = new JFrame();
        f.setLayout(new GridLayout(0, 3));
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        for (int i = 0; i < 9; i++) f.add(new MyPanel());
        f.pack();
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                create();
            }
        });
    }
}
trashgod
jPanel.add(new qmcat()); jPanel.setVisible(true);I'm using that but of course I'm using panels and not frames, I'll try and change it to frames but I thought panels was what I needed to use.
Steven Sayers
You can add all the sub-panels to a `JPanel` and then add that to the frame; the example adds them directly. In any case you need some top-level container like JFrame: http://java.sun.com/docs/books/tutorial/uiswing/components/toplevel.html
trashgod
A: 

I was using the wrong method, it is .add(name, then component) , this is sad.

Steven Sayers
this should not have made any difference, except maybe on certain layouts..
jeshan