I have an Glob
object which represents a glob of data. It has several parameters like size, the date it was created, who created it, and so on. I'm building a GUI to modify these Globs
of data, and in my GUI is a JTextField
for each of these parameters. Each JTextField
has a FocusListener
attached to it, which looks like:
public class GlobUpdaterGUI {
HashMap<String, Glob> globs;
// I know it's not great style to call main like this, but
// it makes this snippet runnable.
public static void main(String[] args) {
new GlobUpdaterGUI();
}
public GlobUpdaterGUI() {
initialize();
}
public void initialize() {
JFrame frame = new JFrame("Glob Updater");
frame.setMinimumSize(new Dimension(250,150));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(4, 2));
JTextField globNumber = new JTextField(10);
JTextField dateTextField = new JTextField(10);
dateTextField.addFocusListener(new FocusListener() {
@Override public void focusGained(FocusEvent arg0) {}
@Override
public void focusLost(FocusEvent e) {
/* "globNumber" lets the user select a glob from "globs". */
Glob glob = globs.get(globNumber.getText());
glob.date = dateTextField.getText();
}
});
JTextField sizeTextField = new JTextField(10);
sizeTextField.addFocusListener(new FocusListener() {
@Override public void focusGained(FocusEvent arg0) {}
@Override
public void focusLost(FocusEvent e) {
Glob glob = globs.get(globNumber.getText());
glob.size = dateTextField.getText();
}
});
JTextField creatorTextField = new JTextField(10);
creatorTextField.addFocusListener(new FocusListener() {
@Override public void focusGained(FocusEvent arg0) {}
@Override
public void focusLost(FocusEvent e) {
Glob glob = globs.get(globNumber.getText());
glob.creator = dateTextField.getText();
}
});
panel.add(new JLabel("Glob Number:"));
panel.add(globNumber);
panel.add(new JLabel("Glob Date:"));
panel.add(dateTextField);
panel.add(new JLabel("Glob Size:"));
panel.add(sizeTextField);
panel.add(new JLabel("Glob Creator:"));
panel.add(creatorTextField);
frame.add(panel);
frame.setVisible(true);
}
}
My problem is that every FocusListener block is nearly identical to the others, but for a specific parameter.
This practice doesn't support readability, writeability, maintainability, or really any style guideline. Though there are only three blocks now, which isn't too bad, a Glob
is going to end up with about a dozen parameters, so I'd like to solve this problem before heading too far down the wrong path.
I'd much rather create a new class which implements FocusListener
, which takes in a JTextField
(and/or whatever else it may need) and modifies its respective Glob
parameter, but I don't know the proper practice if I need to modify a different variable in each one. Is there a way to generalize a Listener
so that I don't need nearly identical code littered throughout my project?
UpdaterGUI
to only write values when I "commit" them, instead of on an unfocus event. That makes the whole mess of Focus listeners unnecessary... \$\endgroup\$