Reputation: 189
I have created a blank Swing GUI in NetBeans. I then created a separate Java class (empClass) with all the objects (buttons, textboxes, labels, etc.) that I want to have displayed on the Swing form (employees). The design of the form will be to have a panel that has a series of labels and textboxes in it for manipulating employee information. Outside the panel will be two buttons, a label at the top of the Form as a title, and a JList. I have created all the objects in the class and according to online research all the necessary coding to call the objects from the class onto the Swing Form is correct but when I run the application the Form remains blank and does not display the objects created in the other class. How can I get this to work?
Here is the coding in the Swing Form (employees) to call the Java class:
public class employees extends javax.swing.JFrame {
public employees() {
initComponents();
this.setPreferredSize(new Dimension(720,520));
this.add(ec.getpanel());
}
empClass ec = new empClass();
Here is the entire coding of the java class (empClass):
package SV_Operations;
import java.awt.*;
import javax.swing.*;
public class empClass
{
public JButton bfunction = new javax.swing.JButton("Add");
public JButton cancel = new javax.swing.JButton("Cancel");
public JList empList = new javax.swing.JList();
public JLabel picon = new javax.swing.JLabel();
public ImageIcon icon = new ImageIcon(empClass.class.getResource("/Pictures/Carbon.jpg"));
public JPanel panel = new javax.swing.JPanel();
public JLabel lbltitle = new javax.swing.JLabel("Admin Page");
public JLabel lblemp = new javax.swing.JLabel("Employee ID: ");
public JTextField txtempid = new javax.swing.JTextField();
public JLabel lblidno = new javax.swing.JLabel("ID Number:");
public JTextField txtidno = new javax.swing.JTextField();
public JLabel lblname = new javax.swing.JLabel("Name:");
public JTextField txtname = new javax.swing.JTextField();
public JLabel lblsurname = new javax.swing.JLabel("Surname: ");
public JTextField txtsurname = new javax.swing.JTextField();
public JLabel lblcell = new javax.swing.JLabel("Cellphone no: ");
public JTextField txtcell = new javax.swing.JTextField();
public JLabel lblalt = new javax.swing.JLabel("Alternative no: ");
public JTextField txtalt = new javax.swing.JTextField();
public JLabel lblemail = new javax.swing.JLabel("Emial: ");
public JTextField txtemail = new javax.swing.JTextField();
public JLabel lbladdress = new javax.swing.JLabel("Address: ");
public JTextField txtaddress = new javax.swing.JTextField();
public JLabel lblpass = new javax.swing.JLabel("Password: ");
public JTextField txtpass = new javax.swing.JTextField();
public empClass()
{
}
public JPanel getpanel()
{
return panel;
}
public void layoutUI()
{
panel.setLayout(null);
panel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
panel.setBounds(230, 80, 450, 310);
panel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
panel.setLayout(null);
panel.setVisible(true);
panel.add(panel);
lbltitle.setFont(new java.awt.Font("Excelerate", 1, 42));
lbltitle.setBounds(60, 0, 620, 70);
lbltitle.setForeground(Color.RED);
lblemp.setFont(new java.awt.Font("Alexis", 1, 18));
lblemp.setBounds(45, 30, 170, 13);
lblemp.setForeground(Color.RED);
lblemp.setVisible(true);
panel.add(lblemp);
txtempid.setBounds(200, 20, 230, 30);
txtempid.setVisible(true);
panel.add(txtempid);
lblidno.setFont(new java.awt.Font("Alexis", 1, 18));
lblidno.setBounds(72, 60, 170, 13);
lblidno.setForeground(Color.RED);
lblidno.setVisible(true);
panel.add(lblidno);
txtidno.setBounds(200, 50, 230, 30);
txtidno.setVisible(true);
panel.add(txtidno);
lblname.setFont(new java.awt.Font("Alexis", 1, 18));
lblname.setBounds(131, 90, 180, 13);
lblname.setForeground(Color.RED);
lblname.setVisible(true);
panel.add(lblname);
txtname.setBounds(200, 80, 230, 30);
txtname.setVisible(true);
panel.add(txtname);
lblsurname.setFont(new java.awt.Font("Alexis", 1, 18));
lblsurname.setBounds(85, 120, 180, 13);
lblsurname.setForeground(Color.RED);
lblsurname.setVisible(true);
panel.add(lblsurname);
txtsurname.setBounds(200, 110, 230,13);
txtsurname.setVisible(true);
panel.add(txtsurname);
lblcell.setFont(new java.awt.Font("Alexis", 1, 18));
lblcell.setBounds(34, 150, 190, 13);
lblcell.setForeground(Color.RED);
lblcell.setVisible(true);
panel.add(lblcell);
txtcell.setBounds(200, 140, 230, 30);
txtcell.setVisible(true);
panel.add(txtcell);
lblalt.setFont(new java.awt.Font("Alexis", 1, 18));
lblalt.setBounds(18, 180, 190, 13);
lblalt.setForeground(Color.RED);
lblalt.setVisible(true);
panel.add(lblalt);
txtalt.setBounds(200, 170, 230, 30);
txtalt.setVisible(true);
panel.add(txtalt);
lblemail.setFont(new java.awt.Font("Alexis", 1, 18));
lblemail.setBounds(129, 210, 190, 13);
lblemail.setForeground(Color.RED);
lblemail.setVisible(true);
panel.add(lblemail);
txtemail.setBounds(200, 200, 230, 30);
txtemail.setVisible(true);
panel.add(txtemail);
lbladdress.setFont(new java.awt.Font("Alexis", 1, 18));
lbladdress.setBounds(92, 240, 190, 13);
lbladdress.setForeground(Color.RED);
lbladdress.setVisible(true);
panel.add(lbladdress);
txtaddress.setBounds(200, 260, 230, 30);
txtaddress.setVisible(true);
panel.add(txtaddress);
lblpass.setFont(new java.awt.Font("Alexis", 1, 18));
lblpass.setBounds(75, 270, 190, 13);
lblpass.setForeground(Color.RED);
lblpass.setVisible(true);
panel.add(lblpass);
txtpass.setBounds(200, 260, 230, 30);
txtpass.setVisible(true);
panel.add(txtpass);
picon.setBounds(0, 0, 450, 310);
picon.setIcon(icon);
picon.setVisible(true);
panel.add(picon);
empList.setBounds(20, 80, 190, 300);
empList.setModel(new javax.swing.AbstractListModel()
{
String[]strings = {"EMP123", "EMP124", "EMP125"};
public int getSize() {return strings.length;}
public Object getElementAt(int i) {return strings [i];}
});
empList.setVisible(true);
bfunction.setBounds(230, 400, 150, 39);
bfunction.setVisible(true);
bfunction.addActionListener(new java.awt.event.ActionListener()
{
@Override
public void actionPerformed(java.awt.event.ActionEvent evt)
{
b1ActionPerformed(evt);
}
});
cancel.setBounds(530, 400, 150, 39);
cancel.setVisible(true);
cancel.addActionListener(new java.awt.event.ActionListener()
{
@Override
public void actionPerformed(java.awt.event.ActionEvent evt)
{
cancelActionPerformed(evt);
}
});
}
private void b1ActionPerformed(java.awt.event.ActionEvent evt)
{
//Left blank for now
}
private void cancelActionPerformed(java.awt.event.ActionEvent evt)
{
System.exit(0);
}
}
Upvotes: 0
Views: 80
Reputation: 4377
It's because you've forgot to call the method in the constructor of your empClass
class.
So your constructor would be:
public empClass()
{
layoutUI(); // you forgot this line!
}
But it is not appropriate to use null layout at all. Professionals don't use null layout. The most important feature you will lose when you are using null as your layout manager is Resizing the frame. See tutorials for GridBagLayout
, I like it so much because I found it as a the most customizable one. You can also find out about other layout managers like GroupLayout, BorderLayout, FlowLayout, GridLayout and ... and the way of mixing them for creating professional designs.
Good Luck
Upvotes: 3