The Kraken
The Kraken

Reputation: 3158

Add GUI Designer JPanel to JFrame

I have a class extending JPanel in NetBeans. The class is working with the "form designer", where you can just drag and drop GUI elements on the screen. I have another class extending JFrame that is creating an instance of this class and adding it to its content frame.

JPanel class:

/**
 * Creates new form OpenPanel
 */
public OpenPanel() {
    initComponents();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jLabel2 = new javax.swing.JLabel();

    jLabel2.setFont(new java.awt.Font("Arial", 0, 48)); // NOI18N
    jLabel2.setText("Jungle Tracks");

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
            .addContainerGap(87, Short.MAX_VALUE)
            .addComponent(jLabel2)
            .addGap(86, 86, 86))
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
            .addContainerGap(21, Short.MAX_VALUE)
            .addComponent(jLabel2)
            .addGap(20, 20, 20))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(0, 225, Short.MAX_VALUE))
    );
}// </editor-fold>

// Variables declaration - do not modify
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}

The main JFrame class (into which I'm trying to add the JPanel), is as follows:

public class MainClass extends JFrame {

static final int WIDTH = 800;
static final int HEIGHT = 600;

public MainClass() throws FileNotFoundException, IOException, UnsupportedAudioFileException, LineUnavailableException, BackingStoreException {
    super("MainClass");

    setSize(WIDTH, HEIGHT);
    setBackground(Color.WHITE);

    OpenPanel open = new OpenPanel();
    ((OpenPanel)open).setFocusable(true);

            getContentPane().add(open);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);              
    setVisible(true);
}

public static void main(String[] args) throws FileNotFoundException, IOException, UnsupportedAudioFileException, LineUnavailableException, BackingStoreException {
    MainClass mc = new MainClass();
}

}

But the JPanel and it's UI elements created in NetBean's design view aren't shown, or to my knowledge--even added to the JFrame. What am I doing wrong?

Upvotes: 1

Views: 1868

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

I'm guessing that your paint(...) method does not call the super.paint(...) method within it, and so the JPanel is not painting its own components well. If this is so, then I suggest:

  • Don't override paint(...) as this is rarely needed, and if done, must be done with care since this method is responsible for painting not only the component but also its borders and its child components -- the very problem you seem to be running into.
  • Instead override paintComponent(...) but be sure to call the super.paintComponent(...) in this method, usually as the very first line of the method.

Upvotes: 3

Related Questions