I want to repeat the Employment History JPanel(with the contents in grey), by using the ADD button and remove it by using Remove button.This is the GUI
I would also like to know how to store the values of the textfields temporarily. Will an array be helpful?
Here is the code
JLabel lblEmploymentHistory = new JLabel("Employment History:");
lblEmploymentHistory.setBounds(46, 145, 128, 14);
frame.getContentPane().add(lblEmploymentHistory);
JPanel panelemp = new JPanel();
panelemp.setBounds(46, 170, 435, 63);
panelemp.setBackground(Color.GRAY);
frame.getContentPane().add(panelemp);
JLabel lblRoleHeld = new JLabel("Role Held:");
panelemp.add(lblRoleHeld);
txtRole = new JTextField();
txtRole.setText("role");
panelemp.add(txtRole);
txtRole.setColumns(10);
JLabel lblDuration = new JLabel("Duration:");
panelemp.add(lblDuration);
txtDuration = new JTextField();
txtDuration.setText("duration");
panelemp.add(txtDuration);
txtDuration.setColumns(10);
JLabel lblEmployer = new JLabel("Employer:");
panelemp.add(lblEmployer);
txtEmployer = new JTextField();
txtEmployer.setText("employer");
panelemp.add(txtEmployer);
txtEmployer.setColumns(10);
JButton Addnewemphis = new JButton("Add");
Addnewemphis.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
panelemp.add(Addnewemphis);
JButton btnRemove = new JButton("Remove");
btnRemove.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
panelemp.add(btnRemove);
Take a step back first. There is a critical concept you need to learn and understand - Separation of Responsibilities.
You need to separate and isolate your functionality into individual classes, this will make it easier to develop repeated functionality quickly and simply.
You should also be focused on separating the management of your "data" from the "user interface", so that the user interface is a representation of your data and is used to (where applicable) update it.
Based on your current design, there's no way to "select" a employee history panel, so having a "generic" remove action is pointless - which one would you remove? Instead, the action actually belongs with the employee history panel itself, but the responsibility to add and remove these components resides with a seperate controller altogether.
Let's start with a basic concept, you need some data first...
public class EmployeeHistory {
private String role;
private String duration;
private String employer;
public EmployeeHistory() {
}
public EmployeeHistory(String role, String duration, String employer) {
this.role = role;
this.duration = duration;
this.employer = employer;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getDuration() {
return duration;
}
public void setDuration(String duration) {
this.duration = duration;
}
public String getEmployer() {
return employer;
}
public void setEmployer(String employer) {
this.employer = employer;
}
}
Personally, I'd prefer a interface
, possible one for "read-only" and for "read-write" access, but we'll stick with this for brevity.
Next, we need some way to display it...
public class HistoryPane extends JPanel {
private final JTextField txtRole;
private final JTextField txtDuration;
private final JTextField txtEmployer;
private final JButton removeButton;
private EmployeeHistory history;
public HistoryPane(EmployeeHistory history) {
// This is what you should use when you want to populate
// the view or properties of the UI are changed
this.history = history;
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
JLabel lblRoleHeld = new JLabel("Role Held:");
add(lblRoleHeld, gbc);
gbc.gridx++;
txtRole = new JTextField();
txtRole.setText("role");
add(txtRole, gbc);
txtRole.setColumns(10);
gbc.gridx = 0;
gbc.gridy++;
JLabel lblDuration = new JLabel("Duration:");
add(lblDuration, gbc);
gbc.gridx++;
txtDuration = new JTextField();
txtDuration.setText("duration");
add(txtDuration, gbc);
txtDuration.setColumns(10);
gbc.gridx = 0;
gbc.gridy++;
JLabel lblEmployer = new JLabel("Employer:");
add(lblEmployer, gbc);
gbc.gridx++;
txtEmployer = new JTextField();
txtEmployer.setText("employer");
add(txtEmployer, gbc);
txtEmployer.setColumns(10);
gbc.gridx = 0;
gbc.gridy++;
gbc.gridwidth = GridBagConstraints.REMAINDER;
removeButton = new JButton("Remove");
add(removeButton, gbc);
}
public EmployeeHistory getHistory() {
return history;
}
public void addActionListener(ActionListener listener) {
removeButton.addActionListener(listener);
}
public void removeActionListener(ActionListener listener) {
removeButton.removeActionListener(listener);
}
}
nb: I've not bothered to fill out the view with the data, I'm sure you can nut it out
The thing to notice here is that the removeButton
doesn't actually do anything, the responsibility is delegated to some other party
Okay, we can remove, but how do we add? Well, you need another component for that...
public class ActionPane extends JPanel {
private JButton btn;
public ActionPane() {
setLayout(new GridBagLayout());
btn = new JButton("Add");
add(btn);
}
public void addActionListener(ActionListener listener) {
btn.addActionListener(listener);
}
public void removeActionListener(ActionListener listener) {
btn.removeActionListener(listener);
}
}
Again, this doesn't actually do anything, it simply delegates the responsibility to some one else.
nb: This is a basic example, equally, you could pass in a some kind of controller which would act as the delegate, but the outcome is basically the same
Okay, fine, but how does this all work? Well, you just need to plumb all the functionality together...
So this is a possible implementation of both the add and remove functionality
ActionPane actionPane = new ActionPane();
actionPane.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Layout constraints
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
// The actually history data
EmployeeHistory history = new EmployeeHistory();
// This is a model to manage the individual histories, making
// it easier to manage
histories.add(history);
// The history view...
HistoryPane pane = new HistoryPane(history);
// The remove action...
pane.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Remove the action to improve the chances of been
// garbage collected
pane.removeActionListener(this);
// Remove the history from our model
histories.remove(pane.getHistory());
// Remove the view
contentPane.remove(pane);
contentPane.revalidate();
contentPane.repaint();
}
});
// Add the view (this is a little trick ;))
contentPane.add(pane, gbc, contentPane.getComponentCount() - 1);
contentPane.revalidate();
contentPane.repaint();
}
});
Runnable example
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JPanel contentPane;
private List<EmployeeHistory> histories;
public TestPane() {
histories = new ArrayList<>(25);
setLayout(new BorderLayout());
contentPane = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weighty = 1;
contentPane.add(new JPanel(), gbc);
JScrollPane scrollPane = new JScrollPane(contentPane);
add(scrollPane);
ActionPane actionPane = new ActionPane();
actionPane.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
EmployeeHistory history = new EmployeeHistory();
histories.add(history);
HistoryPane pane = new HistoryPane(history);
pane.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
pane.removeActionListener(this);
histories.remove(pane.getHistory());
contentPane.remove(pane);
contentPane.revalidate();
contentPane.repaint();
}
});
contentPane.add(pane, gbc, contentPane.getComponentCount() - 1);
contentPane.revalidate();
contentPane.repaint();
}
});
add(actionPane, BorderLayout.SOUTH);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
}
public class ActionPane extends JPanel {
private JButton btn;
public ActionPane() {
setLayout(new GridBagLayout());
btn = new JButton("Add");
add(btn);
}
public void addActionListener(ActionListener listener) {
btn.addActionListener(listener);
}
public void removeActionListener(ActionListener listener) {
btn.removeActionListener(listener);
}
}
public class EmployeeHistory {
private String role;
private String duration;
private String employer;
public EmployeeHistory() {
}
public EmployeeHistory(String role, String duration, String employer) {
this.role = role;
this.duration = duration;
this.employer = employer;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getDuration() {
return duration;
}
public void setDuration(String duration) {
this.duration = duration;
}
public String getEmployer() {
return employer;
}
public void setEmployer(String employer) {
this.employer = employer;
}
}
public class HistoryPane extends JPanel {
private final JTextField txtRole;
private final JTextField txtDuration;
private final JTextField txtEmployer;
private final JButton removeButton;
private EmployeeHistory history;
public HistoryPane(EmployeeHistory history) {
// This is what you should use when you want to populate
// the view or properties of the UI are changed
this.history = history;
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
JLabel lblRoleHeld = new JLabel("Role Held:");
add(lblRoleHeld, gbc);
gbc.gridx++;
txtRole = new JTextField();
txtRole.setText("role");
add(txtRole, gbc);
txtRole.setColumns(10);
gbc.gridx = 0;
gbc.gridy++;
JLabel lblDuration = new JLabel("Duration:");
add(lblDuration, gbc);
gbc.gridx++;
txtDuration = new JTextField();
txtDuration.setText("duration");
add(txtDuration, gbc);
txtDuration.setColumns(10);
gbc.gridx = 0;
gbc.gridy++;
JLabel lblEmployer = new JLabel("Employer:");
add(lblEmployer, gbc);
gbc.gridx++;
txtEmployer = new JTextField();
txtEmployer.setText("employer");
add(txtEmployer, gbc);
txtEmployer.setColumns(10);
gbc.gridx = 0;
gbc.gridy++;
gbc.gridwidth = GridBagConstraints.REMAINDER;
removeButton = new JButton("Remove");
add(removeButton, gbc);
}
public EmployeeHistory getHistory() {
return history;
}
public void addActionListener(ActionListener listener) {
removeButton.addActionListener(listener);
}
public void removeActionListener(ActionListener listener) {
removeButton.removeActionListener(listener);
}
}
}
Now, having said all that, go and have a read of How to Use Tables and How to Use Lists
Will an array be helpful?
No, not really. This would limit the number of history elements you can display. Instead, I'd use a ArrayList
to manage the instances of the history object, as demonstrated above