Results 1 to 3 of 3
  1. #1
    umeshrama is offline Member
    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Default How to load JPanel components to JFrame during run-time.

    Hi All,

    Please go through the below code, when i tried to add a panel component to frame
    during run-time am unable display the panel & panel components in main frame.

    Please help me, how to add the components during run-time.

    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

    }

    Thank you in advance...

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: How to load JPanel components to JFrame during run-time.

    Please go through Guide For New Members and BB Code List - Java Programming Forum and edit your post accordingly.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: How to load JPanel components to JFrame during run-time.

    Quote Originally Posted by umeshrama View Post
    how to add the components during run-time.
    For starters, ditch the NetBeans visual designer. It's categorically not a beginners' tool.

    Learn how to write Swing code: Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 11
    Last Post: 07-10-2012, 05:59 PM
  2. Multiple components in Jpanel
    By shrutisurulkar in forum AWT / Swing
    Replies: 1
    Last Post: 01-07-2011, 03:13 PM
  3. Components Layout in a JPanel
    By jboy in forum New To Java
    Replies: 4
    Last Post: 10-11-2009, 01:08 PM
  4. How to place components on JPanel
    By deshprateek in forum AWT / Swing
    Replies: 1
    Last Post: 02-22-2009, 06:34 AM
  5. Removing components from JPanel
    By Echilon in forum New To Java
    Replies: 0
    Last Post: 12-30-2007, 05:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •