0

I have two almost similar code.

Code I

   JFrame pFrame=new NetBeansFrame();
   JPanel myPanel=new myPanel();
   pFrame.add(myPanel);
   Dimension windowDim=myPanel.getSize();

   pFrame.pack();
   pFrame.getContentPane().setSize(windowDim.width-100,windowDim.height-50);
   pFrame.setVisible(true);

Code II

   JFrame pFrame=new JFrame();
   JPanel myPanel=new myPanel();
   pFrame.add(myPanel);
   Dimension windowDim=myPanel.getSize();

   pFrame.pack();
   pFrame.getContentPane().setSize(windowDim.width-100,windowDim.height-50);
   pFrame.setVisible(true);

In Code I,NetBeansFrame is a frame that I created using netbeans->Jframe and named it NetBeansFrame.It contains nothing.I added the panel into it using codeI.

NetBeansFrame.java

public class NetBeansFrame extends javax.swing.JFrame {


public NetBeansFrame() {
    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() {

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 407, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 429, Short.MAX_VALUE)
    );

    pack();
}// </editor-fold>

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(NetBeansFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(NetBeansFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(NetBeansFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(NetBeansFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new NetBeansFrame().setVisible(true);
        }
    });
}
// Variables declaration - do not modify
// End of variables declaration

}

In Code II,I am creating a frame from JFrame.Logically both the codes are equivalent.

But on executing Code I,the panel does not appear in netbeansFrame while for Code II the panel appears.

So,I want to know that what may be the cause for this unusual behavior for almost same code.

3

1 Answer 1

3
  1. Dimension windowDim=myPanel.getSize(); returned only Dimension[0, 0], becasue returned Dimension from

    • already visible container (in your case with components JPanel)

    • after called pack();

  2. then pFrame.getContentPane().setSize(windowDim.width-100,windowDim.height-50); returned Dimension[-100, -50]

  3. you can

    • if JPanel is empty then returns its PreferredSize

    • if JPanel is not empty, then its JComponents returns PreferredSize you can to try that with to remove/disable code line pFrame.getContentPane().setSize(windowDim.width-100,windowDim.height-50);

2
  • Now my code is JFrame pFrame=new NetBeansFrame(); JPanel myPanel=new myPanel(); pFrame.add(myPanel); pFrame.pack(); pFrame.setVisible(true); even though it does not come.
    – Abhinav
    Jul 13, 2012 at 10:29
  • @Abhinav: Try adding component or two to myPanel. For better readability, edit your question to include new code.
    – trashgod
    Jul 13, 2012 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.