Java Swing open existing form - java

I'm trying to call a method that opens a new existing frame, but all I get is an empty frame without content.
This is the code that calls the function when a button is preesed:
JButton btnPersonalInfo = new JButton("Personal Info");
btnPersonalInfo.setBounds(10, 5, 120, 23);
btnPersonalInfo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFrame PersonalInfo = new JFrame("Personal Info");
PersonalInfo content = new PersonalInfo();
PersonalInfo.setContentPane(content);
PersonalInfo.setSize(700,700);
PersonalInfo.setLocation(100,15);
PersonalInfo.setDefaultCloseOperation( JFrame.HIDE_ON_CLOSE );
PersonalInfo.setResizable(false);
PersonalInfo.setVisible(true);
}
});
panel_1.add(btnPersonalInfo);
This is how I initialize the PersonalInfo function:
public PersonalInfo() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.CENTER);
panel.setLayout(null);
JLabel lblPersonalInfo = new JLabel("Personal Information");
lblPersonalInfo.setFont(new Font("Arial", Font.BOLD, 16));
lblPersonalInfo.setBounds(110, 11, 185, 14);
panel.add(lblPersonalInfo);
JLabel lblFullName = new JLabel("Full Name");
lblFullName.setBounds(10, 36, 58, 14);
panel.add(lblFullName);
JLabel lblNationality = new JLabel("Nationality");
lblNationality.setBounds(10, 61, 78, 14);
panel.add(lblNationality);
JLabel lblDateBirth = new JLabel("Date of Birth");
lblDateBirth.setBounds(10, 86, 78, 14);
panel.add(lblDateBirth);
JLabel lblGender = new JLabel("Gender");
lblGender.setBounds(10, 111, 46, 14);
panel.add(lblGender);
JLabel lblAddress = new JLabel("Address");
lblAddress.setBounds(10, 164, 58, 14);
panel.add(lblAddress);
JLabel lblMobile = new JLabel("Mobile");
lblMobile.setBounds(10, 189, 46, 14);
panel.add(lblMobile);
JLabel lblEmail = new JLabel("E-mail");
lblEmail.setBounds(10, 214, 46, 14);
panel.add(lblEmail);
JRadioButton rdbtnM_2 = new JRadioButton("M");
rdbtnM_2.setBounds(74, 133, 109, 23);
panel.add(rdbtnM_2);
JRadioButton rdbtnF = new JRadioButton("F");
rdbtnF.setBounds(74, 107, 109, 23);
panel.add(rdbtnF);
}
}
Basically I'm expecting to view the frame from the PersonalInfo method when I press the btnPersonalInfo button and right now I only get an empty frame.
Thank you and sorry if this is a duplicate, but it's my first question here.

Remove frame = new JFrame(); from your initialize()
Simply:
JPanel panel = new JPanel();
add(panel, BorderLayout.CENTER);//It will be added to the ContentPane by default.
panel.setLayout(null);
...
Then when calling, remove the following block:
JFrame PersonalInfo = new JFrame("Personal Info");
PersonalInfo content = new PersonalInfo();
PersonalInfo.setContentPane(content);
Replace it by:
PersonalInfo personalInfo = new PersonalInfo();
personalInfo.setSize(700,700);
personalInfo.setLocation(100,15);
....
So this should be your method call:
public void actionPerformed(ActionEvent e) {
PersonalInfo personalInfo = new PersonalInfo();
personalInfo.setSize(700,700);
personalInfo.setLocation(100,15);
personalInfo.setDefaultCloseOperation( JFrame.HIDE_ON_CLOSE );
personalInfo.setResizable(false);
personalInfo.setVisible(true);
}
And this should be your Class:
public class PersonalInfo extends JFrame
{
public PersonalInfo() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
JPanel panel = new JPanel();
add(panel, BorderLayout.CENTER);
panel.setLayout(null);
JLabel lblPersonalInfo = new JLabel("Personal Information");
lblPersonalInfo.setFont(new Font("Arial", Font.BOLD, 16));
lblPersonalInfo.setBounds(110, 11, 185, 14);
panel.add(lblPersonalInfo);
JLabel lblFullName = new JLabel("Full Name");
lblFullName.setBounds(10, 36, 58, 14);
panel.add(lblFullName);
JLabel lblNationality = new JLabel("Nationality");
lblNationality.setBounds(10, 61, 78, 14);
panel.add(lblNationality);
JLabel lblDateBirth = new JLabel("Date of Birth");
lblDateBirth.setBounds(10, 86, 78, 14);
panel.add(lblDateBirth);
JLabel lblGender = new JLabel("Gender");
lblGender.setBounds(10, 111, 46, 14);
panel.add(lblGender);
JLabel lblAddress = new JLabel("Address");
lblAddress.setBounds(10, 164, 58, 14);
panel.add(lblAddress);
JLabel lblMobile = new JLabel("Mobile");
lblMobile.setBounds(10, 189, 46, 14);
panel.add(lblMobile);
JLabel lblEmail = new JLabel("E-mail");
lblEmail.setBounds(10, 214, 46, 14);
panel.add(lblEmail);
JRadioButton rdbtnM_2 = new JRadioButton("M");
rdbtnM_2.setBounds(74, 133, 109, 23);
panel.add(rdbtnM_2);
JRadioButton rdbtnF = new JRadioButton("F");
rdbtnF.setBounds(74, 107, 109, 23);
panel.add(rdbtnF);
}
}

Related

I couldnt set location of components in Java for usinf JFrame and contentPane?

I want to create a simple profile information page with JFrame and contentPane. I added all the components correctly. But I cant set their location. How can i set it? I used setBoundary() method bur it doesnt work.
My Anket.java class is:
import java.awt.*;
import javax.swing.*;
public class Anket {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(600, 300));
frame.setTitle("Profile Information");
Container contentPane = frame.getContentPane();
contentPane.setBackground(Color.GRAY);
JRadioButton rdbtnNewRadioButton = new JRadioButton("Age(10-20)");
rdbtnNewRadioButton.setBounds(412, 50, 50, 20);
frame.getContentPane().add(rdbtnNewRadioButton);
JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("Age(21-30)");
rdbtnNewRadioButton.setBounds(412, 20, 50, 20);
frame.getContentPane().add(rdbtnNewRadioButton_1);
JRadioButton rdbtnNewRadioButton_2 = new JRadioButton("Age(31-40)");
rdbtnNewRadioButton.setBounds(412, 30, 50, 20);
frame.getContentPane().add(rdbtnNewRadioButton_2);
JRadioButton rdbtnNewRadioButton_3 = new JRadioButton("Age(41-50)");
rdbtnNewRadioButton.setBounds(412, 40, 50, 20);
frame.getContentPane().add(rdbtnNewRadioButton_3);
JLabel lblNewLabel = new JLabel("PLEASE ENTER YOUR INFORMATION");
lblNewLabel.setBounds(149, 0, 182, 14);
frame.getContentPane().add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("Enter Your Name");
lblNewLabel_1.setBounds(127, 25, 86, 14);
frame.getContentPane().add(lblNewLabel_1);
JLabel lblNewLabel_2 = new JLabel("Enter Your Lastname");
lblNewLabel_2.setBounds(272, 25, 100, 14);
frame.getContentPane().add(lblNewLabel_2);
JLabel lblNewLabel_3 = new JLabel("Enter Your Height");
lblNewLabel_3.setBounds(127, 103, 86, 14);
frame.getContentPane().add(lblNewLabel_3);
JTextField textField = new JTextField();
textField.setBounds(216, 100, 86, 20);
frame.getContentPane().add(textField);
textField.setColumns(10);
JTextField textField_1 = new JTextField();
textField_1.setBounds(127, 48, 86, 20);
frame.getContentPane().add(textField_1);
textField_1.setColumns(10);
JTextField textField_2 = new JTextField();
textField_2.setColumns(10);
textField_2.setBounds(412, 48, 86, 20);
frame.getContentPane().add(textField_2);
JLabel lblNewLabel_4 = new JLabel(new ImageIcon("C:/Users/PC/eclipse-workspace/Project/src/Berguzar.jpg"));
lblNewLabel_4.setBounds(10, 21, 100, 85);
frame.getContentPane().add(lblNewLabel_4);
frame.setLayout(new FlowLayout());
frame.setVisible(true);
}
}
The program doesnt have any error. How can i fix this problem?
My output screen is:
output
You should replace
Container contentPane = frame.getContentPane();
contentPane.setBackground(Color.GRAY);
with
JPanel contentPane = new JPanel(null); // The null is important
contentPane.setBackground(Color.GRAY);
frame.setContentPane(contentPane); // each time you change the content pane you need to revalidate the jframe (frame.setVisible(true) also revalidate the jframe so in this case it is not needed to revalidate the jframe)

Java Swing: Change JButton to an image button

I have been trying this for hours. I want to change each of the JButton components to image buttons (They were radio buttons that's why the variable names don't make sense). The for loop inside is just displaying all the images under the button's. Each image is called Option1, Option2, etc. So what I have been trying to do is get rid of the for loop and just have 10 buttons with images on them that you can click.
If someone could help me figure that out that would be great. All I've been able to do is have the button created with an image but the button appears on a completely different window.
public class JDialog2 extends JDialog {
private final JPanel contentPanel = new JPanel();
/**
* Create the dialog.
*/
public JDialog2(Quiz quiz) {
setBounds(100, 100, 450, 600);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
JLabel lblNewLabel_1 = new JLabel("2、Favourite subject at Hogwarts");
lblNewLabel_1.setFont(new Font("Arial", Font.PLAIN, 14));
lblNewLabel_1.setBounds(94, 10, 248, 15);
contentPanel.add(lblNewLabel_1);
ButtonGroup btGroup = new ButtonGroup();
for(int i=1; i<=7; i++) {
ImageIcon image1 = new ImageIcon("Option" + i + ".jpg");
image1.setImage(image1.getImage().getScaledInstance(50, 50, Image.SCALE_DEFAULT));
JLabel imageLablel1=new JLabel(image1);
contentPanel.add(imageLablel1);
imageLablel1.setBounds(29, 75 * i - 25, 50, 50);
}
JButton radioButton_1 = new JButton("1.Care of Magical Creatures");
radioButton_1.setBounds(29, 25, 226, 23);
contentPanel.add(radioButton_1);
JButton radioButton_2 = new JButton("2.Charms");
radioButton_2.setBounds(29, 50, 158, 23);
contentPanel.add(radioButton_2);
JButton radioButton_3 = new JButton("3.Defense Against the Dark Arts");
radioButton_3.setBounds(29, 75, 255, 23);
contentPanel.add(radioButton_3);
JButton radioButton_4 = new JButton("4.Divination");
radioButton_4.setBounds(29, 100, 121, 23);
contentPanel.add(radioButton_4);
JButton radioButton_5 = new JButton("5.Herbology");
radioButton_5.setBounds(29, 125, 179, 23);
contentPanel.add(radioButton_5);
JButton radioButton_6 = new JButton("6.History of Magic");
radioButton_6.setBounds(29, 150, 158, 23);
contentPanel.add(radioButton_6);
JButton radioButton_7 = new JButton("7.Muggle Studies");
radioButton_7.setBounds(29, 175, 121, 23);
contentPanel.add(radioButton_7);
JButton radioButton_8 = new JButton("8.Potions");
radioButton_8.setBounds(29, 200, 121, 23);
contentPanel.add(radioButton_8);
JButton radioButton_9 = new JButton("9.Study of Ancient Runes");
radioButton_9.setBounds(29, 220, 255, 23);
contentPanel.add(radioButton_9);
JButton radioButton_10 = new JButton("10.Transfiguration");
radioButton_10.setBounds(29, 245, 179, 23);
contentPanel.add(radioButton_10);
btGroup.add(radioButton_1);
btGroup.add(radioButton_2);
btGroup.add(radioButton_3);
btGroup.add(radioButton_4);
btGroup.add(radioButton_5);
btGroup.add(radioButton_6);
btGroup.add(radioButton_7);
btGroup.add(radioButton_8);
btGroup.add(radioButton_9);
btGroup.add(radioButton_10);
JButton btnCommit = new JButton("Commit");
btnCommit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Question question = quiz.getNextQuestion();
int choice = 0;
Enumeration<AbstractButton> en = btGroup.getElements();
while (en.hasMoreElements()) {
AbstractButton ab = en.nextElement();
choice++;
if (ab.isSelected()) {
break;
}
}
question.setSelectedAnswer(choice - 1);
JDialog3 dialog3 = new JDialog3(quiz);
dialog3.setVisible(true);
exit();
}
});
btnCommit.setBounds(300, 138, 93, 23);
contentPanel.add(btnCommit);
}
public void exit(){
this.setVisible(false);
}
}

Issue in Radio Button Action Listener

I have written some code when one of the option in radio button is clicked
it should display one jlabel and jtext field. And when other option in the radio button is clicked it should hide the previous shown jlabel and jtext field and display new jlabel and jtext field.
In the output when I click on one of the radio button it is displaying nothing unless and until I maximize my Window. After geting my jlabel and jtextfield If I click on other radio button the jlabel and jtextfield is hidden but Im not able to see new jlabel and jtextfield for that radiobutton.
enter code here
public class Emp4 {
private JFrame frame;
private JTextField jtxtName;
private JTextField jtxtAge;
private JTextField jtxtSal;
private JTextField jtxtHour_Pay;
private JTextField jtxtHour_Worked;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Emp4 window = new Emp4();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Emp4() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(0, 0, 1000, 800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBorder(new LineBorder(new Color(0, 0, 0), 3));
panel.setBounds(30, 11, 414, 36);
frame.getContentPane().add(panel);
panel.setLayout(null);
JLabel lblEmployeeDatabase = new JLabel("Employee Database");
lblEmployeeDatabase.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblEmployeeDatabase.setBounds(157, 7, 193, 25);
panel.add(lblEmployeeDatabase);
JPanel panel_1 = new JPanel();
panel_1.setBorder(new LineBorder(new Color(0, 0, 0), 3));
panel_1.setBounds(10, 61, 464, 230);
frame.getContentPane().add(panel_1);
panel_1.setLayout(null);
JLabel jlblEmpName = new JLabel("Employee Name");
jlblEmpName.setBounds(10, 11, 110, 14);
panel_1.add(jlblEmpName);
jtxtName = new JTextField();
jtxtName.setBounds(114, 8, 120, 20);
panel_1.add(jtxtName);
jtxtName.setColumns(10);
JLabel jlblEmpAge = new JLabel("Employee Age");
jlblEmpAge.setBounds(10, 52, 110, 14);
panel_1.add(jlblEmpAge);
jtxtAge = new JTextField();
jtxtAge.setColumns(10);
jtxtAge.setBounds(114, 49, 120, 20);
panel_1.add(jtxtAge);
JLabel jlblEmpType = new JLabel("Employee Type");
jlblEmpType.setBounds(10, 95, 110, 14);
panel_1.add(jlblEmpType);
JRadioButton jrdbuttonFullTime = new JRadioButton("Full Time");
JRadioButton jrdbtnContract = new JRadioButton("Contract ");
JLabel jlblEmpHour = new JLabel("Hourly Rate");
jlblEmpHour.setBounds(5, 121, 66, 14);
ButtonGroup group =new ButtonGroup();
JLabel jlblEmpSal = new JLabel("Salary");
jlblEmpSal.setBounds(114, 121, 66, 14);
JLabel jlblEmpWork = new JLabel("Hours Worked");
jlblEmpWork.setBounds(150, 120, 86, 24);
jtxtSal = new JTextField();
jtxtSal.setColumns(10);
jtxtSal.setBounds(164, 121, 109, 23);
jtxtHour_Pay = new JTextField();
jtxtHour_Pay.setColumns(10);
jtxtHour_Pay.setBounds(75, 121, 59, 23);
jtxtHour_Worked = new JTextField();
jtxtHour_Worked.setColumns(10);
jtxtHour_Worked.setBounds(243, 121, 109, 23);
group.add(jrdbuttonFullTime);
group.add(jrdbtnContract);
jrdbuttonFullTime.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(jrdbuttonFullTime.isSelected()){
//jrdbtnContract.setSelected(false);
panel_1.add(jlblEmpSal);
panel_1.add(jtxtSal);
jlblEmpHour.setVisible(false);
jtxtHour_Pay.setVisible(false);
jtxtHour_Worked.setVisible(false);
jlblEmpWork.setVisible(false);
}
}
});
jrdbuttonFullTime.setBounds(113, 91, 109, 23);
panel_1.add(jrdbuttonFullTime);
jrdbtnContract.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(jrdbtnContract.isSelected()){
//jrdbuttonFullTime.setSelected(false);
panel_1.add(jlblEmpHour);
panel_1.add(jtxtHour_Pay);
panel_1.add(jlblEmpWork);
panel_1.add(jtxtHour_Worked);
jlblEmpSal.setVisible(false);
jtxtSal.setVisible(false);
}
}
});
jrdbtnContract.setBounds(218, 91, 109, 23);
panel_1.add(jrdbtnContract);
}
}
Insteed of adding and removing components, simply add all and hide/show them on radiobox selection like this:
panel_1.add(jlblEmpSal);
panel_1.add(jtxtSal);
panel_1.add(jlblEmpHour);
panel_1.add(jtxtHour_Pay);
panel_1.add(jlblEmpWork);
panel_1.add(jtxtHour_Worked);
ActionListener myAction = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
jlblEmpHour.setVisible(jrdbtnContract.isSelected());
jtxtHour_Pay.setVisible(jrdbtnContract.isSelected());
jtxtHour_Worked.setVisible(jrdbtnContract.isSelected());
jlblEmpWork.setVisible(jrdbtnContract.isSelected());
jlblEmpSal.setVisible(jrdbuttonFullTime.isSelected());
jtxtSal.setVisible(jrdbuttonFullTime.isSelected());
}
};
myAction.actionPerformed(null); // to initialize labels first
jrdbuttonFullTime.addActionListener(myAction); // add actionlisteners
jrdbtnContract.addActionListener(myAction);// add actionlisteners
As you can see, you dont even need 2 separate action listeners as one but shared instance is just enough.
So the complete app will look like this:
public class Emp4 {
private JFrame frame;
private JTextField jtxtName;
private JTextField jtxtAge;
private JTextField jtxtSal;
private JTextField jtxtHour_Pay;
private JTextField jtxtHour_Worked;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Emp4 window = new Emp4();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Emp4() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(0, 0, 1000, 800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBorder(new LineBorder(new Color(0, 0, 0), 3));
panel.setBounds(30, 11, 414, 36);
frame.getContentPane().add(panel);
panel.setLayout(null);
JLabel lblEmployeeDatabase = new JLabel("Employee Database");
lblEmployeeDatabase.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblEmployeeDatabase.setBounds(157, 7, 193, 25);
panel.add(lblEmployeeDatabase);
JPanel panel_1 = new JPanel();
panel_1.setBorder(new LineBorder(new Color(0, 0, 0), 3));
panel_1.setBounds(10, 61, 464, 230);
frame.getContentPane().add(panel_1);
panel_1.setLayout(null);
JLabel jlblEmpName = new JLabel("Employee Name");
jlblEmpName.setBounds(10, 11, 110, 14);
panel_1.add(jlblEmpName);
jtxtName = new JTextField();
jtxtName.setBounds(114, 8, 120, 20);
panel_1.add(jtxtName);
jtxtName.setColumns(10);
JLabel jlblEmpAge = new JLabel("Employee Age");
jlblEmpAge.setBounds(10, 52, 110, 14);
panel_1.add(jlblEmpAge);
jtxtAge = new JTextField();
jtxtAge.setColumns(10);
jtxtAge.setBounds(114, 49, 120, 20);
panel_1.add(jtxtAge);
JLabel jlblEmpType = new JLabel("Employee Type");
jlblEmpType.setBounds(10, 95, 110, 14);
panel_1.add(jlblEmpType);
JRadioButton jrdbuttonFullTime = new JRadioButton("Full Time");
JRadioButton jrdbtnContract = new JRadioButton("Contract ");
JLabel jlblEmpHour = new JLabel("Hourly Rate");
jlblEmpHour.setBounds(5, 121, 66, 14);
ButtonGroup group = new ButtonGroup();
JLabel jlblEmpSal = new JLabel("Salary");
jlblEmpSal.setBounds(114, 121, 66, 14);
JLabel jlblEmpWork = new JLabel("Hours Worked");
jlblEmpWork.setBounds(150, 120, 86, 24);
jtxtSal = new JTextField();
jtxtSal.setColumns(10);
jtxtSal.setBounds(164, 121, 109, 23);
jtxtHour_Pay = new JTextField();
jtxtHour_Pay.setColumns(10);
jtxtHour_Pay.setBounds(75, 121, 59, 23);
jtxtHour_Worked = new JTextField();
jtxtHour_Worked.setColumns(10);
jtxtHour_Worked.setBounds(243, 121, 109, 23);
group.add(jrdbuttonFullTime);
group.add(jrdbtnContract);
panel_1.add(jlblEmpSal);
panel_1.add(jtxtSal);
panel_1.add(jlblEmpHour);
panel_1.add(jtxtHour_Pay);
panel_1.add(jlblEmpWork);
panel_1.add(jtxtHour_Worked);
ActionListener myAction = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
jlblEmpHour.setVisible(jrdbtnContract.isSelected());
jtxtHour_Pay.setVisible(jrdbtnContract.isSelected());
jtxtHour_Worked.setVisible(jrdbtnContract.isSelected());
jlblEmpWork.setVisible(jrdbtnContract.isSelected());
jlblEmpSal.setVisible(jrdbuttonFullTime.isSelected());
jtxtSal.setVisible(jrdbuttonFullTime.isSelected());
}
};
myAction.actionPerformed(null); // to initialize labels first
jrdbuttonFullTime.addActionListener(myAction);
jrdbtnContract.addActionListener(myAction);
jrdbtnContract.setBounds(218, 91, 109, 23);
jrdbuttonFullTime.setBounds(113, 91, 109, 23);
panel_1.add(jrdbuttonFullTime);
panel_1.add(jrdbtnContract);
}
}
I've revised your code a little. This should get you going on the right path:
public class Emp4 {
private JFrame frame;
private JTextField jtxtName;
private JTextField jtxtAge;
private JTextField jtxtSal;
private JTextField jtxtHour_Pay;
private JTextField jtxtHour_Worked;
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable() {
public void run()
{
try {
Emp4 window = new Emp4();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Emp4()
{
initialize();
}
private void initialize()
{
frame = new JFrame();
frame.setBounds(0, 0, 1000, 800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBorder(new LineBorder(new Color(0, 0, 0), 3));
panel.setBounds(30, 11, 414, 36);
frame.getContentPane().add(panel);
panel.setLayout(null);
JLabel lblEmployeeDatabase = new JLabel("Employee Database");
lblEmployeeDatabase.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblEmployeeDatabase.setBounds(157, 7, 193, 25);
panel.add(lblEmployeeDatabase);
JPanel panel_1 = new JPanel();
panel_1.setBorder(new LineBorder(new Color(0, 0, 0), 3));
panel_1.setBounds(10, 61, 464, 230);
frame.getContentPane().add(panel_1);
panel_1.setLayout(null);
JLabel jlblEmpName = new JLabel("Employee Name");
jlblEmpName.setBounds(10, 11, 110, 14);
panel_1.add(jlblEmpName);
jtxtName = new JTextField();
jtxtName.setBounds(114, 8, 120, 20);
panel_1.add(jtxtName);
jtxtName.setColumns(10);
JLabel jlblEmpAge = new JLabel("Employee Age");
jlblEmpAge.setBounds(10, 52, 110, 14);
panel_1.add(jlblEmpAge);
jtxtAge = new JTextField();
jtxtAge.setColumns(10);
jtxtAge.setBounds(114, 49, 120, 20);
panel_1.add(jtxtAge);
JLabel jlblEmpType = new JLabel("Employee Type");
jlblEmpType.setBounds(10, 95, 110, 14);
panel_1.add(jlblEmpType);
JRadioButton jrdbuttonFullTime = new JRadioButton("Full Time");
JRadioButton jrdbtnContract = new JRadioButton("Contract ");
JLabel jlblEmpHour = new JLabel("Hourly Rate");
jlblEmpHour.setBounds(5, 121, 66, 14);
ButtonGroup group = new ButtonGroup();
JLabel jlblEmpSal = new JLabel("Salary");
jlblEmpSal.setBounds(114, 121, 66, 14);
JLabel jlblEmpWork = new JLabel("Hours Worked");
jlblEmpWork.setBounds(150, 120, 86, 24);
jtxtSal = new JTextField();
jtxtSal.setColumns(10);
jtxtSal.setBounds(164, 121, 109, 23);
jtxtHour_Pay = new JTextField();
jtxtHour_Pay.setColumns(10);
jtxtHour_Pay.setBounds(75, 121, 59, 23);
jtxtHour_Worked = new JTextField();
jtxtHour_Worked.setColumns(10);
jtxtHour_Worked.setBounds(243, 121, 109, 23);
//*******************************************************************
// Add all your salary fields here, not in ActionListeners
// Start them off invisible
//*******************************************************************
jlblEmpSal.setVisible(false);
panel_1.add(jlblEmpSal);
jtxtSal.setVisible(false);
panel_1.add(jtxtSal);
panel_1.add(jlblEmpHour);
jlblEmpHour.setVisible(false);
panel_1.add(jtxtHour_Pay);
jtxtHour_Pay.setVisible(false);
panel_1.add(jlblEmpWork);
jlblEmpWork.setVisible(false);
jtxtHour_Worked.setVisible(false);
panel_1.add(jtxtHour_Worked);
group.add(jrdbuttonFullTime);
group.add(jrdbtnContract);
jrdbuttonFullTime.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if (jrdbuttonFullTime.isSelected()) {
//jrdbtnContract.setSelected(false);
// ****************************************************
// In ActionListeners for radiobuttons, hide the fields you
// don't want to see, make visible the ones you do want to see
// ****************************************************
jlblEmpSal.setVisible(true);
jtxtSal.setVisible(true);
jlblEmpHour.setVisible(false);
jtxtHour_Pay.setVisible(false);
jtxtHour_Worked.setVisible(false);
jlblEmpWork.setVisible(false);
}
}
});
jrdbuttonFullTime.setBounds(113, 91, 109, 23);
panel_1.add(jrdbuttonFullTime);
jrdbtnContract.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if (jrdbtnContract.isSelected()) {
//jrdbuttonFullTime.setSelected(false);
// ****************************************************
// In ActionListeners for radiobuttons, hide the fields you
// don't want to see, make visible the ones you do want to see
// ****************************************************
jlblEmpHour.setVisible(true);
jtxtHour_Pay.setVisible(true);
jlblEmpWork.setVisible(true);
jtxtHour_Worked.setVisible(true);
jlblEmpSal.setVisible(false);
jtxtSal.setVisible(false);
}
}
});
jrdbtnContract.setBounds(218, 91, 109, 23);
panel_1.add(jrdbtnContract);
}
}
Your radio buttons start off both unchecked, so you see no salary detail initially. When you click one or the other, the corresponding details appear.
In which case(which radiobutton checked ?) you want to show which controls?

Java Swing GUI Invisible

basically I have a very simple GUI, but for some reason when I run the code the JButtons only appear when I mouse over them, and the JTextField only appear if I click on them. How do I fix this so that they are visible when the program runs? Thanks.
Here is my code:
//JFrame + settings
JFrame frmFormSubmission = new JFrame();
frmFormSubmission.setSize(new Dimension(350, 165));
frmFormSubmission.setTitle("Form Submission - Client");
frmFormSubmission.setLocationRelativeTo(null);
frmFormSubmission.setVisible(true);
frmFormSubmission.setResizable(false);
frmFormSubmission.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//JPanel
JPanel panel = new JPanel();
frmFormSubmission.getContentPane().add(panel, BorderLayout.CENTER);
panel.setLayout(null);
//JLabels
//Name
JLabel lblName = new JLabel("Name:");
lblName.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblName.setBounds(46, 12, 40, 14);
panel.add(lblName);
//Address
JLabel lblAddress = new JLabel("Address:");
lblAddress.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblAddress.setBounds(33, 37, 53, 14);
panel.add(lblAddress);
//Phone
JLabel lblPhone = new JLabel("Phone #:");
lblPhone.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblPhone.setBounds(28, 62, 58, 14);
panel.add(lblPhone);
//Email
JLabel lblEmail = new JLabel("Email:");
lblEmail.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblEmail.setBounds(46, 87, 36, 14);
panel.add(lblEmail);
//JTextFields
//Name
nameField = new JTextField();
nameField.setBounds(93, 11, 137, 20);
panel.add(nameField);
nameField.setColumns(10);
//Address
addressField = new JTextField();
addressField.setColumns(10);
addressField.setBounds(93, 36, 137, 20);
panel.add(addressField);
//Phone
phoneField = new JTextField();
phoneField.setColumns(10);
phoneField.setBounds(93, 61, 137, 20);
panel.add(phoneField);
//Email
emailField = new JTextField();
emailField.setColumns(10);
emailField.setBounds(93, 86, 137, 20);
panel.add(emailField);
//JButtons
//Submit
JButton btnSubmit = new JButton("Submit");
btnSubmit.setBounds(240, 10, 89, 23);
panel.add(btnSubmit);
//Cancel
JButton btnCancel = new JButton("Cancel");
btnCancel.setBounds(240, 44, 89, 23);
panel.add(btnCancel);
//Flush
JButton btnFlush = new JButton("Flush");
btnFlush.setBounds(240, 76, 89, 23);
panel.add(btnFlush);
//Checkbox
JCheckBox chckbxPromotions = new JCheckBox("Email me with new and promotions!");
chckbxPromotions.setBounds(25, 108, 205, 23);
panel.add(chckbxPromotions);
Any help is very much appreciated!
Call frmFormSubmission.setVisible(true); as the last thing you do. setVisible will layout your components so they can be properly drawn. If you add components without laying them out, then you'll have graphical issues.
For more information on what that means, check out the documentation on the validate method.
Also, try
frmFormSubmission.invalidate();
frmFormSubmission.validate();
frmFormSubmission.repaint();
If this still does not work, you can try this:
SwingUtilities.updateComponentTreeUI(frmFormSubmission);

JFrame design panel looks empty

I created a window using the design panel in JFrame, it didn't have any problem. But suddenly, When I try to edit my window using design, nothing shows up. I can't even run that window, but if i start running it from the previous page, it shows up (I use the setVisible method to do this). You can see my code below:
import java.awt.EventQueue;
public class seat extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
seat frame = new seat();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public seat(String from, String to, String time, String date) {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(380, 230, 50, 300);
this.setTitle("Flight Reservation System");
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel time1 = DefaultComponentFactory.getInstance().createTitle("- " + time);
time1.setBackground(SystemColor.windowBorder);
time1.setForeground(Color.BLACK);
time1.setFont(new Font("Tahoma", Font.PLAIN, 11));
time1.setBounds(338, 16, 55, 25);
contentPane.add(time1);
JLabel from1 = DefaultComponentFactory.getInstance().createTitle(from.substring(0, 3));
from1.setFont(new Font("Tahoma", Font.PLAIN, 11));
from1.setBounds(65, 16, 38, 25);
contentPane.add(from1);
JLabel to1 = DefaultComponentFactory.getInstance().createTitle("- " + to.substring(0, 3));
to1.setFont(new Font("Tahoma", Font.PLAIN, 11));
to1.setBounds(84, 16, 55, 25);
contentPane.add(to1);
date = date.replace(".", "/");
JLabel date1 = DefaultComponentFactory.getInstance().createTitle(date.substring(0, 11));
date1.setFont(new Font("Tahoma", Font.PLAIN, 11));
date1.setBounds(277, 16, 67, 25);
contentPane.add(date1);
JLabel label_14 = new JLabel("");
label_14.setIcon(new ImageIcon("C:\\Users\\Emre\\Desktop\\asdasd.png"));
label_14.setBounds(60, 47, 19, 108);
contentPane.add(label_14);
JLabel a1 = new JLabel(" ");
a1.setIcon(new ImageIcon("C:\\Users\\Emre\\Desktop\\Untitled.png"));
a1.setBounds(71, 58, 39, 43);
contentPane.add(a1);
JLabel koltuk1 = new JLabel(" ");
koltuk1.setIcon(new ImageIcon("C:\\Users\\Emre\\Desktop\\Untitled.png"));
koltuk1.setBounds(109, 58, 38, 43);
contentPane.add(koltuk1);
JLabel koltuk2 = new JLabel(" ");
koltuk2.setIcon(new ImageIcon("C:\\Users\\Emre\\Desktop\\Untitled.png"));
koltuk2.setBounds(147, 58, 38, 43);
contentPane.add(koltuk2);
JLabel label_1 = new JLabel(" ");
label_1.setIcon(new ImageIcon("C:\\Users\\Emre\\Desktop\\Untitled.png"));
label_1.setBounds(185, 58, 38, 43);
contentPane.add(label_1);
JLabel label_2 = new JLabel(" ");
label_2.setIcon(new ImageIcon("C:\\Users\\Emre\\Desktop\\Untitled.png"));
label_2.setBounds(223, 58, 38, 43);
contentPane.add(label_2);
JLabel label_3 = new JLabel(" ");
label_3.setIcon(new ImageIcon("C:\\Users\\Emre\\Desktop\\Untitled.png"));
label_3.setBounds(261, 58, 38, 43);
contentPane.add(label_3);
JLabel label_4 = new JLabel(" ");
label_4.setIcon(new ImageIcon("C:\\Users\\Emre\\Desktop\\Untitled.png"));
label_4.setBounds(299, 58, 38, 43);
contentPane.add(label_4);
JLabel label_5 = new JLabel(" ");
label_5.setIcon(new ImageIcon("C:\\Users\\Emre\\Desktop\\Untitled.png"));
label_5.setBounds(337, 58, 38, 43);
contentPane.add(label_5);
JLabel lblNewLabel_2 = new JLabel(" ");
lblNewLabel_2.setIcon(new ImageIcon("C:\\Users\\Emre\\Desktop\\cccccccccc.png"));
lblNewLabel_2.setBounds(71, 97, 304, 20);
contentPane.add(lblNewLabel_2);
JLabel label_6 = new JLabel(" ");
label_6.setIcon(new ImageIcon("C:\\Users\\Emre\\Desktop\\Untitled.png"));
label_6.setBounds(71, 112, 38, 43);
contentPane.add(label_6);
JLabel label_7 = new JLabel(" ");
label_7.setIcon(new ImageIcon("C:\\Users\\Emre\\Desktop\\Untitled.png"));
label_7.setBounds(109, 112, 38, 43);
contentPane.add(label_7);
JLabel label_8 = new JLabel(" ");
label_8.setIcon(new ImageIcon("C:\\Users\\Emre\\Desktop\\Untitled.png"));
label_8.setBounds(147, 112, 38, 43);
contentPane.add(label_8);
JLabel label_9 = new JLabel(" ");
label_9.setIcon(new ImageIcon("C:\\Users\\Emre\\Desktop\\Untitled.png"));
label_9.setBounds(185, 112, 38, 43);
contentPane.add(label_9);
JLabel label_10 = new JLabel(" ");
label_10.setIcon(new ImageIcon("C:\\Users\\Emre\\Desktop\\Untitled.png"));
label_10.setBounds(223, 112, 38, 43);
contentPane.add(label_10);
JLabel label_11 = new JLabel(" ");
label_11.setIcon(new ImageIcon("C:\\Users\\Emre\\Desktop\\Untitled.png"));
label_11.setBounds(261, 112, 38, 43);
contentPane.add(label_11);
JLabel label_12 = new JLabel(" ");
label_12.setIcon(new ImageIcon("C:\\Users\\Emre\\Desktop\\Untitled.png"));
label_12.setBounds(299, 112, 38, 43);
contentPane.add(label_12);
JLabel label_13 = new JLabel(" ");
label_13.setIcon(new ImageIcon("C:\\Users\\Emre\\Desktop\\Untitled.png"));
label_13.setBounds(337, 112, 38, 43);
contentPane.add(label_13);
JLabel lblNewLabel_3 = new JLabel("");
lblNewLabel_3.setIcon(new ImageIcon("C:\\Users\\Emre\\Desktop\\adsd.png"));
lblNewLabel_3.setBounds(63, 40, 312, 26);
contentPane.add(lblNewLabel_3);
JLabel lblNewLabel_4 = new JLabel("");
lblNewLabel_4.setIcon(new ImageIcon("C:\\Users\\Emre\\Desktop\\seats\\empty.png"));
lblNewLabel_4.setBounds(81, 163, 46, 43);
contentPane.add(lblNewLabel_4);
JLabel label_15 = new JLabel("");
label_15.setIcon(new ImageIcon("C:\\Users\\Emre\\Desktop\\seats\\selected.png"));
label_15.setBounds(164, 166, 46, 43);
contentPane.add(label_15);
JLabel lblNewLabel_5 = new JLabel("");
lblNewLabel_5.setIcon(new ImageIcon("C:\\Users\\Emre\\Desktop\\seats\\booked.png"));
lblNewLabel_5.setBounds(254, 163, 46, 43);
contentPane.add(lblNewLabel_5);
JLabel lblSelected = new JLabel("Selected");
lblSelected.setBounds(202, 176, 57, 14);
contentPane.add(lblSelected);
JLabel lblBooked = new JLabel("Booked");
lblBooked.setBounds(292, 176, 46, 14);
contentPane.add(lblBooked);
JLabel lblEmpty = new JLabel("Empty");
lblEmpty.setBounds(118, 176, 46, 14);
contentPane.add(lblEmpty);
JButton btnContinue = new JButton("Next");
btnContinue.setBounds(359, 227, 62, 23);
contentPane.add(btnContinue);
btnContinue.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
data page = new data();
page.setVisible(true);
setVisible(false);
}
});
}
public seat() {
// TODO Auto-generated constructor stub
}
}
Within the public static void main only the empty constructor is called. You have to call constructor with the parameters which contains the code which builds the GUI.
Instead of calling the standard constructor with
seat frame = new seat();
you have to call it with
seat frame = new seat("from", "to", "time", "date");

Categories

Resources