Per the title, I'm trying to make my GUI change with the RadioButton selection. Specifically, I want to control which JLabels and JTextFields are displayed based on the RadioButton selection.
Right now, my GUI displays the Employee ID, Last Name, First Name, Credit Rate, and Credit # Labels, and their corresponding fields.
When I select the Part Time Faculty RadioButton, I would like for the employeeID, lastName, firstName, classRate, numClass labels to appear, and their corresponding fields (empIDField, lastField, firstField, classRateField, numClassField), without the creditRate and numCred fields, and creditRateField and numCredField.
I imagine I can get the other RadioButtons working in a similar manner once I understand the method. Here's my code, if it helps.
import javax.swing.JFrame;
public class EmployeeFrame {
public static void main(String[] args) {
JFrame frame = new JFrame ("Employee Management");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
EmployeePanel panel = new EmployeePanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
And my Panel
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class EmployeePanel extends JPanel {
private JLabel empID, lastName, firstName, creditRate, classRate, numCred, numClass, salary, hours;
private JTextField empIDField, lastField, firstField, creditRateField, classRateField, numCredField,
numClassField, salaryField, hourField;
private JButton addEmp, writeReport, displayReport, readFile, exit;
private JRadioButton fullTime, partTime, admin, maint;
public EmployeePanel() {
//JLabels
empID = new JLabel("Employee ID");
lastName = new JLabel("Last Name");
firstName = new JLabel("First Name");
creditRate = new JLabel("Credit Rate");
classRate = new JLabel("Class Rate");
numCred = new JLabel("Credit #");
numClass = new JLabel("Class #");
salary = new JLabel("Salary");
hours = new JLabel("Hours");
classRate.setFont(new Font("Arial", Font.PLAIN, 17));
numClass.setFont(new Font("Arial", Font.PLAIN, 17));
salary.setFont(new Font("Arial", Font.PLAIN, 17));
hours.setFont(new Font("Arial", Font.PLAIN, 17));
empID.setFont(new Font("Arial", Font.PLAIN, 17));
lastName.setFont(new Font("Arial", Font.PLAIN, 17));
firstName.setFont(new Font("Arial", Font.PLAIN, 17));
creditRate.setFont(new Font("Arial", Font.PLAIN, 17));
numCred.setFont(new Font("Arial", Font.PLAIN, 17));
//JRadioButtons
fullTime = new JRadioButton("Full Time Faculty", true);
partTime = new JRadioButton("Part Time Faculty");
admin = new JRadioButton("Administration");
maint = new JRadioButton("Maintenance");
//JButtons
addEmp = new JButton("Add Employee");
writeReport = new JButton("Write Report to File");
displayReport = new JButton("Display Summary Pay Report");
readFile = new JButton("Read From File");
exit = new JButton("Exit");
//JTextFields
empIDField = new JTextField(11);
lastField = new JTextField(11);
firstField = new JTextField(11);
creditRateField = new JTextField(11);
classRateField = new JTextField(11);
numCredField = new JTextField(11);
numClassField = new JTextField(11);
salaryField = new JTextField(11);
hourField = new JTextField(11);
//Buttongroup for RadioButtons
ButtonGroup type = new ButtonGroup();
type.add(fullTime);
type.add(partTime);
type.add(admin);
type.add(maint);
//Radio Button Panel
JPanel radioPanel = new JPanel();
radioPanel.setBorder(BorderFactory.createTitledBorder("Type of Employee"));
radioPanel.add(fullTime);
radioPanel.add(partTime);
radioPanel.add(admin);
radioPanel.add(maint);
JPanel firstButtons = new JPanel();
firstButtons.add(addEmp);
firstButtons.add(writeReport);
JPanel secButtons = new JPanel();
secButtons.add(displayReport);
secButtons.add(readFile);
secButtons.add(exit);
JPanel labels = new JPanel();
labels.add(empID);
labels.add(lastName);
labels.add(firstName);
labels.add(creditRate);
labels.add(numCred);
labels.setPreferredSize(new Dimension(100, 150));
JPanel fields = new JPanel();
fields.add(empIDField);
fields.add(lastField);
fields.add(firstField);
fields.add(creditRateField);
fields.add(numCredField);
fields.setPreferredSize(new Dimension(130, 150));
add(radioPanel);
add(labels);
add(fields);
add(firstButtons);
add(secButtons);
setPreferredSize (new Dimension(480, 300));
}
}