This discussion is archived
1 Reply Latest reply: Feb 17, 2013 11:12 PM by StanislavL RSS

How to load/add JPanel components to JFrame during run-time.

991388 Newbie
Currently Being Moderated
Hi All,

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

Could you please help me, how to add the components during run-time & correct the below piece of 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

}



Thanks & Regards,
Umesh.R

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points