blah123123 0 Newbie Poster

Im trying to make an employee database where the user can add, delete, search for, view all, edit an employee, and quit. After the user clicks the add or delete button, the program is supposed to allow them to input the names and other things nd when they press enter it should add or delete an employee. Same for search and edit and view all is supposed to output all the names of the employees. Quit is just supposed to exit the program. I was able to create a fully functioning program without GUI but I was told to use GUI and I cannot create the program. Any help is appreciated.

Here is what i have done so far:
(The lines that are commented out are trial and error. I used them but I had some problems so I tried to make the program without them but I didnt want to completely take them out)

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class EmployeeButtonstest implements ActionListener{
private JButton addButton;
private JButton deleteButton;
private JButton editButton;
private JButton searchButton;
private JButton viewButton;
private JButton quitButton;

JTextField fnameField = new JTextField (10);
JTextField lnameField = new JTextField (10);
JTextField phonenumField = new JTextField (20);
JTextField addressField = new JTextField (20);

JFrame frame;
JPanel panel;

public EmployeeButtonstest() {
//super ("ICT Employee Database");
frame = new JFrame("Test");
//frame.setLayout(new Layout(FlowLayout));
panel = new JPanel();
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(800,300);

}

public void make(){

addButton = new JButton("Add Employee");
panel.add(addButton);

deleteButton = new JButton("Delete Employee");
panel.add(deleteButton);

editButton = new JButton("Edit Employee");
panel.add(editButton);

searchButton = new JButton("Search Employee");
panel.add(searchButton);

viewButton = new JButton("View Employees");
panel.add(viewButton);

quitButton = new JButton("Quit");
panel.add(quitButton);

addButton.addActionListener(this);
deleteButton.addActionListener(this);
editButton.addActionListener(this);
searchButton.addActionListener(this);
frame.add(panel);
}


public void actionPerformed(ActionEvent event) {

if (event.getSource() == addButton)
{
panel.revalidate();
panel.removeAll();

//add text fields
panel.add(fnameField);
panel.add(lnameField);
panel.add(phonenumField);
panel.add(addressField);

panel.repaint();
}
else if(event.getSource() == deleteButton)
{
//string = String.format("Delete Employee: %s", event.getActionCommand());

panel.revalidate();
panel.removeAll();
panel.add(fnameField);
panel.add(lnameField);

panel.repaint();
}
else if(event.getSource() == editButton)
{
//string = String.format("Edit Employee: %s", event.getActionCommand());

panel.revalidate();
panel.removeAll();
panel.add(fnameField);
panel.add(lnameField);

panel.repaint();

}
else if(event.getSource() == searchButton)
{
//string = String.format("Search Employee: %s", event.getActionCommand());

panel.revalidate();
panel.removeAll();
panel.add(fnameField);
panel.add(lnameField);

panel.repaint();
}

// JOptionPane.showMessageDialog(null, string);
}
}

Dani AI

Generated

For : quick diagnosis and a practical plan to get the GUI working and predictable.

The posted code shows the right idea (buttons, text fields, a panel) but a few common Swing pitfalls are preventing the behavior expected when pressing Enter or switching modes. Key issues are GUI creation ordering and thread-safety, missing listeners/wiring for some buttons, and relying on Enter without a clear "submit" action or default button. A cleaner approach is to separate the model (an Employee class + List) from the view and controller, and to use a small view-switching strategy (CardLayout or modal dialogs) instead of removing/adding components in-place repeatedly.

Concrete workflow to follow

  • Keep a List<Employee> as the data model and write small helper methods: addEmployee(), findEmployee(), updateEmployee(), removeEmployee(). Call these from action handlers.
  • Make the top row a control panel of buttons and the center a CardLayout that hosts: (a) an input form panel, (b) a table/list panel for "view all", (c) an empty/info panel. Switch cards on button presses.
  • Use a JTable (DefaultTableModel or a custom AbstractTableModel) for View All — it updates easily when the model changes.
  • For form submission, add an explicit Submit/Cancel pair. To allow Enter to trigger submission, set a default button on the root pane rather than relying on text-field ActionListeners.

Small, high-impact fixes (examples)

  • Create and show the GUI on the Event Dispatch Thread.
  • Ensure the UI is fully constructed (components added to the frame) before making it visible, or call revalidate/pack after changes.
  • Wire listeners for all buttons (including View and Quit) and clear form fields after submit.
  • Consider assigning a unique ID to employees so delete/edit operations are unambiguous.

Helpful references: Oracle’s Swing tutorial covers UI basics and concurrency (Creating a GUI With Swing and Concurrency in Swing). These explain EDT rules, card layouts, and table models that make the recommended refactor straightforward.

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.