GUI Question (Jlabe/JTextField)
This is my first time working with JPanels, etc I am trying to make a panel with a bunch of JTextFields and a bunch of JLabels, I want to make the JLabels centered above the JTextFields.
Code :
//Created textfields and labels
JTextField jtxtFN = new JTextField("", 10);
JTextField jtxtLN = new JTextField("", 10);
JTextField jtxtWage = new JTextField("", 8);
JTextField jtxtEmp = new JTextField("", 5);
JLabel jlFN = new JLabel("First Name");
JLabel jlLN = new JLabel("Last Name");
JLabel jlWage = new JLabel("Wage");
JLabel jlJob = new JLabel("Job Type");
JLabel jlEmp = new JLabel("Employee ID");
JLabel jlGend = new JLabel("Gender");
//Adding text fields and labels
p3.add(jlFN);
p3.add(jtxtFN);
p3.add(jlLN);
p3.add(jtxtLN);
p3.add(jlWage);
p3.add(jtxtWage);
p3.add(jlJob);
//Combo Box
JComboBox jcJob = new JComboBox();
jcJob.addItem("Hourly");
jcJob.addItem("Salaried");
jcJob.addItem("Consultant");
p3.add(jcJob);
p3.add(jlEmp);
p3.add(jtxtEmp);
p3.add(jlGend);
//Radio Buttons with Button group
JRadioButton jbMale = new JRadioButton("Male");
JRadioButton jbFemale = new JRadioButton("Female");
JRadioButton jbUnkown = new JRadioButton("Unknown");
ButtonGroup bg = new ButtonGroup();
bg.add(jbMale);
bg.add(jbFemale);
bg.add(jbUnkown);
p3.add(jbMale);
p3.add(jbFemale);
p3.add(jbUnkown);
Re: GUI Question (Jlabe/JTextField)
I suggest you downloading netbeans it has a visual editor, where you can drag and drop your GUI components onto a frame. Since you are new, netbeans would be ideal.
Re: GUI Question (Jlabe/JTextField)
Quote:
Originally Posted by
Ajan
I suggest you downloading
netbeans it has a visual editor, where you can drag and drop your GUI components onto a frame. Since you are new, netbeans would be ideal.
I have netbeans but I need to learn it this way first before moving onto that way. Any guidance would be much appreciated.
Re: GUI Question (Jlabe/JTextField)
Quote:
Originally Posted by
aandcmedia
This is my first time working with JPanels, etc I am trying to make a panel with a bunch of JTextFields and a bunch of JLabels, I want to make the JLabels centered above the JTextFields.
To arrange components within a JFrame/JPanel, you should use some kind of layout managers. For your situation, I suggest you dive into GroupLayout or GridBagLayout.
Re: GUI Question (Jlabe/JTextField)
Quote:
Originally Posted by
Ajan
I suggest you downloading
netbeans it has a visual editor, where you can drag and drop your GUI components onto a frame. Since you are new, netbeans would be ideal.
Opinions vary, and mine is that I do not recommend this if you are a beginner. GUI builders are for more advanced users, or at least those familiar with the basics of Swing. In my experience those without any knowledge of Swing cannot customize, adapt, read, or even debug code written by a builder
@aandcmedia, as mentioned using the appropriate LayoutManager: see A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)
Re: GUI Question (Jlabe/JTextField)
I wish that was an option buy my Java class requires we write out the entire thing, my program is looking fine except for this one problem I seem to be facing. I will eventually figure it out, will get back to everyone when I do. After a bit of brain power and suggestions of boxes within boxes etc.
Code :
public class Layout extends JFrame{
public Layout (){
//Create 1 master panel (p1), than 2 panels that will go inside of it
JPanel p1 = new JPanel(new BorderLayout());
JPanel p2 = new JPanel(new GridLayout(1,5));
JPanel p3 = new JPanel(new FlowLayout(FlowLayout.CENTER));
JPanel p4 = new JPanel(new FlowLayout(FlowLayout.CENTER));
//Setting up font for centering
//Populating the grid with buttons
JButton jbtAdd = new JButton("Add Employee");
JButton jbtChange = new JButton("Change Employee");
JButton jbtClear = new JButton("Clear Fields");
JButton jbtRead = new JButton("Read File");
JButton jbtWrite = new JButton("Write File");
p2.add(jbtAdd);
p2.add(jbtChange);
p2.add(jbtClear);
p2.add(jbtRead);
p2.add(jbtWrite);
//Created textfields and labels
JTextField jtxtFN = new JTextField("", 10);
JTextField jtxtLN = new JTextField("", 10);
JTextField jtxtWage = new JTextField("", 8);
JTextField jtxtEmp = new JTextField("", 5);
JLabel jlFN = new JLabel("First Name");
JLabel jlLN = new JLabel("Last Name");
JLabel jlWage = new JLabel("Wage");
JLabel jlJob = new JLabel("Job Type");
JLabel jlEmp = new JLabel("Employee ID");
JLabel jlGend = new JLabel("Gender");
//Center All Labels:
jlFN.setHorizontalAlignment(JLabel.CENTER);
jlLN.setHorizontalAlignment(JLabel.CENTER);
jlWage.setHorizontalAlignment(JLabel.CENTER);
jlJob.setHorizontalAlignment(JLabel.CENTER);
jlEmp.setHorizontalAlignment(JLabel.CENTER);
jlGend.setHorizontalAlignment(JLabel.CENTER);
//Laying out the text boxes with labels to center
JPanel f1 = new JPanel(new GridLayout(2,1));
JPanel f2 = new JPanel(new GridLayout(2,1));
JPanel f3 = new JPanel(new GridLayout(2,1));
JPanel f4 = new JPanel(new GridLayout(2,1));
JPanel f5 = new JPanel(new GridLayout(2,1));
JPanel f6 = new JPanel(new GridLayout(2,1));
JPanel f7 = new JPanel(new GridLayout(1,3));
f1.add(jlFN);
f1.add(jtxtFN);
f2.add(jlLN);
f2.add(jtxtLN);
f3.add(jlWage);
f3.add(jtxtWage);
p3.add(f1);
p3.add(f2);
p3.add(f3);
//Combo Box with proper labeling
f4.add(jlJob);
JComboBox jcJob = new JComboBox();
jcJob.addItem("Hourly");
jcJob.addItem("Salaried");
jcJob.addItem("Consultant");
f4.add(jcJob);
p3.add(f4);
f5.add(jlEmp);
f5.add(jtxtEmp);
p3.add(f5);
f6.add(jlGend);
//Radio Buttons with Button group
JRadioButton jbMale = new JRadioButton("Male");
JRadioButton jbFemale = new JRadioButton("Female");
JRadioButton jbUnkown = new JRadioButton("Unknown");
ButtonGroup bg = new ButtonGroup();
bg.add(jbMale);
bg.add(jbFemale);
bg.add(jbUnkown);
f7.add(jbMale);
f7.add(jbFemale);
f7.add(jbUnkown);
f6.add(f7);
p3.add(f6);
//Creating the list
JLabel jlEmpList = new JLabel("Employee List");
JList jlList = new JList();
p4.add(jlEmpList);
p4.add(jlList);
add(p4, BorderLayout.WEST);
add(p2, BorderLayout.SOUTH);
add(p3, BorderLayout.CENTER);
}
After Scouring the internets I've found everything I've needed to make my GUI! Now its time to learn how to use handling!