Results 1 to 3 of 3
- 10-28-2012, 09:55 AM #1
Member
- Join Date
- Oct 2012
- Posts
- 1
- Rep Power
- 0
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...
- 10-28-2012, 04:12 PM #2
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 10-28-2012, 04:14 PM #3
Re: How to load JPanel components to JFrame 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)
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Get all components in list and every time it finds one add ||
By tambry in forum New To JavaReplies: 11Last Post: 07-10-2012, 04:59 PM -
Multiple components in Jpanel
By shrutisurulkar in forum AWT / SwingReplies: 1Last Post: 01-07-2011, 02:13 PM -
Components Layout in a JPanel
By jboy in forum New To JavaReplies: 4Last Post: 10-11-2009, 12:08 PM -
How to place components on JPanel
By deshprateek in forum AWT / SwingReplies: 1Last Post: 02-22-2009, 05:34 AM -
Removing components from JPanel
By Echilon in forum New To JavaReplies: 0Last Post: 12-30-2007, 04:05 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks