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));
    }
}

Recommended Answers

All 5 Replies

If you want the change mad when the radio button is selected, you need to add a listener that will be called when the button is selected. In the listener method you can make the changes you want.

Alright. I understand that I need to create action lisenters for each RadioButton, but what exaclty do I put in these lisneters to make the GUI change?

That depends on what the changes are. You can add or remoe components, you can make components visible or invisible, you can change the text in fields ... whatever you want.

Still trying to figure out how to go about adding and removing. So I tried the following ActionListener, but it's not giving me the desired results.

It's my understanding that I need to create a new Panel within the ActionListener? I don't seem to have access to the others outside of it. Do I have to keep creating new Panels, or can I gain access to the ones I've already created? I'm trying to get it to look more or less like this: http://i40.tinypic.com/262sec7.png

    public class PartTime implements ActionListener {

        public void actionPerformed (ActionEvent event) {

            JPanel labels = new JPanel();
            labels.add(empID);
            labels.add(lastName);
            labels.add(firstName);
            labels.add(classRate);
            labels.add(numClass);
            labels.setPreferredSize(new Dimension(100, 150));
            labels.revalidate();
            labels.repaint();
            add(labels)
        }
    }

You should be able to access existing components from an inner class. Is the listener class defined inside of the class that contains the components you want to access? Otherwise you need to pass a reference to the class with the components to the listener class and add some accessor methods so you can get to the components you need.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.