I am attempting to create JButtons through the use of a JMenuItem. I have a New Employee JMenuItem which when clicked opens up a frame with four JTextFields: Employee Name, Employee IDNumber, Employee Pay Rate, and Employee Hours Worked, and one JButton, Apply. After the user inputs the data into the JTextFields and hits the Apply JButton, the main window is supposed to add a JButton with the inputted data.
Here is the code for the Program Display
public ProgramDisplay() {
setTitle("Fluid Accounts");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(5, 5));
account = new JButton("<html>" + "Samuel Tayman" + "<br>" + "Wages: " + findPay(hours, payRate));
add(account);
account = new JButton("<html>" + "Janice" + "<br>" + "Wages: " + findPay(hours, payRate));
add(account);
buildMenuBar();
pack();
setVisible(true);
}
And here is the code for the New Employee JMenuItem
with the ActionListener
:
private void buildEmployeeMenu()
{
newEmployee = new JMenuItem("New Employee");
newEmployee.addActionListener(new NewListener());
employeeMenu = new JMenu("Employees");
employeeMenu.add(newEmployee);
}
/**
* Private inner class that handles the event that is generated when the
* user selects New from the file menu.
*/
private class NewListener extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent e) {
name = "";
employeeID = "";
rateOfPay = "";
hoursWorked = "";
JButton Apply = new JButton("Apply");
Apply.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
name = nameField.getText();
employeeID = IDField.getText();
rateOfPay = payRateField.getText();
hoursWorked = hoursField.getText();
setVisible(false);
}
});
setLayout(new GridLayout(4, 4));
add(nameField = new JTextField("Enter the Employee Name"));
add(IDField = new JTextField("Enter the Employee's ID Number"));
add(payRateField = new JTextField("Enter the Employee's Pay Rate"));
add(hoursField = new JTextField("Enter the Employee's Hours Worked"));
add(Apply);
pack();
setVisible(true);
}
}
Example of current running program
I've made it so that the data stored in the textField
components are saved into fields however I haven't been able to successfully create a JButton
in the main program window using the Apply button.
Any help and assistance is appreciated!
Implement a method to add a JButton in the ProgramDisplay class, e.g.
void addButton(String name, String employeeID, String rateOfPay, String hoursWorked) {
add(new JButton(name + ":" + employeeID + ", " + rateOfPay + " - " + hoursWorked); }
pass the ProgramDisplay instance as final parameter to the NewListener, e. g.
private class NewListener extends JFrame implements ActionListener {
final ProgramDisplay pd;
NewListener(ProgramDisplay pd) {
this.pd = pd;
}
.....
and call the addButton method from within your ActionPerformed method:
Apply.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
name = nameField.getText();
employeeID = IDField.getText();
rateOfPay = payRateField.getText();
hoursWorked = hoursField.getText();
setVisible(false);
NewListener.this.pd.addButton(name, employeeID, rateOfPay, hoursWorked);
}
});