Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How to Add JPanel to JFrame during run-time

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post How to Add JPanel to JFrame during run-time

    Hi All,

    Please find the below code, when i tried to add a panel to frame
    during run-time, am unable display the panel in main frame.

    how to add the panel during run-time.
    please findout the issue in the below code

     
    Panel class
     
    import java.awt.ComponentOrientation;
    import java.util.Locale;
     
    public class PanelOne extends javax.swing.JPanel {
     
    /**
    * Creates new form PanelOne
    */
    public PanelOne() {
    initComponents();
    }
     
    public boolean initApp() {
     
    applyComponentOrientation(ComponentOrientation.get Orientation(Locale.getDefault()));
    setVisible(true);
    return true;
    }
     
    private void initComponents() {
     
    jPanel1 = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    jTextField1 = new javax.swing.JTextField();
     
    jPanel1.setBorder(new javax.swing.border.MatteBorder(null));
     
    jLabel1.setText("PanelOne");
     
    jTextField1.setText("jTextField1");
     
    org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
    jPanel1Layout.createParallelGroup(org.jdesktop.lay out.GroupLayout.LEADING)
    .add(jPanel1Layout.createSequentialGroup()
    .addContainerGap()
    .add(jPanel1Layout.createParallelGroup(org.jdeskto p.layout.GroupLayout.LEADING)
    .add(jLabel1)
    .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 119, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
    .addContainerGap(165, Short.MAX_VALUE))
    );
    jPanel1Layout.setVerticalGroup(
    jPanel1Layout.createParallelGroup(org.jdesktop.lay out.GroupLayout.LEADING)
    .add(jPanel1Layout.createSequentialGroup()
    .add(22, 22, 22)
    .add(jLabel1)
    .addPreferredGap(org.jdesktop.layout.LayoutStyle.R ELATED)
    .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
    .addContainerGap(99, Short.MAX_VALUE))
    );
     
    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(org.jdesktop.layout.Gro upLayout.LEADING)
    .add(layout.createSequentialGroup()
    .addContainerGap()
    .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
    .addContainerGap(org.jdesktop.layout.GroupLayout.D EFAULT_SIZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(org.jdesktop.layout.Gro upLayout.LEADING)
    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
    .addContainerGap(org.jdesktop.layout.GroupLayout.D EFAULT_SIZE, Short.MAX_VALUE)
    .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
    .addContainerGap(org.jdesktop.layout.GroupLayout.D EFAULT_SIZE, Short.MAX_VALUE))
    );
    }// </editor-fold> 
    // Variables declaration - do not modify 
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration 
    }
     
     
    Main JFrame Class
     
     
    import java.awt.BorderLayout;
    import javax.swing.JFrame;
     
    public class Frame extends javax.swing.JFrame {
     
    private PanelOne one;
     
    /**
    * Creates new form JFrame
    */
    public Frame() {
    initComponents();
    }
     
    public void initFrame() {
     
    one = new PanelOne(); 
     
    if (one.initApp()) {
     
    add(one, BorderLayout.CENTER);
    setTitle("Main Frame");
    pack();
    setLocationRelativeTo(null);
    setVisible(true);
     
    }
     
    }
     
    /**
    * 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.WindowConstan ts.EXIT_ON_CLOSE);
     
    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(org.jdesktop.layout.Gro upLayout.LEADING)
    .add(0, 557, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(org.jdesktop.layout.Gro upLayout.LEADING)
    .add(0, 481, Short.MAX_VALUE)
    );
     
    pack();
    }// </editor-fold> 
     
    public static void main(String args[]) {
    Frame f = new Frame();
    f.initFrame();
    }
     
     
    /**
    * @param args the command line arguments
    */
    // Variables declaration - do not modify 
    // End of variables declaration 
     
    }


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: How to Add JPanel to JFrame during run-time

    Moved to AWT/Swing forum.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    I'm a beginner but...can solve your problem to create the new panel object `one' in Frame constructor instead in a separate method?

Similar Threads

  1. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM
  2. how to add a JFrame listener for a JPanel
    By netll in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 27th, 2012, 06:54 AM
  3. Dynamically add a Jpanel to a Jframe
    By Closet_Rambo in forum AWT / Java Swing
    Replies: 6
    Last Post: October 3rd, 2011, 03:51 AM
  4. Add Image to JPanel
    By bgroenks96 in forum Java Theory & Questions
    Replies: 10
    Last Post: June 16th, 2011, 02:44 PM
  5. Add JScrollPane to a JPanel
    By alibm in forum AWT / Java Swing
    Replies: 2
    Last Post: March 7th, 2011, 02:59 PM