Greetings,
I am developping a gui application. In my toolbar I would like to have a menu button which opens a frame which prompts user for various "account settings" values and then return them to the main window.
What is the best way to approach this?
I have the following code, but my getter methods are always returning null for some reason
Thanks.
// SAMPLE FROM MAIN CLASS .......
private String username = "";
private String password = "";
JMenuItem miAccountSettings = new JMenuItem("Account Info");
miAccountSettings.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
AccountSettings accountSettings = new AccountSettings();
username = accountSettings.getUsername();
password = accountSettings.getPassword();
}
});
optionsMenu.add(miAccountSettings);
// AccountSettings CLASS
public class AccountSettings implements ActionListener {
private String username;
private String password;
private JTextField txtUser;
private JPasswordField txtPass;
private JButton btnSave;
private JButton btnCancel;
private JFrame f;
public AccountSettings() {
f = new JFrame();
f.setTitle("Account Info");
Toolkit kit = f.getToolkit();
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
Insets in = kit.getScreenInsets(gs[0].getDefaultConfiguration());
Dimension d = kit.getScreenSize();
int max_width = (d.width - in.left - in.right);
int max_height = (d.height - in.top - in.bottom);
f.setSize(Math.min(max_width, 400), Math.min(max_height, 250));
f.setLocation((int) (max_width - f.getWidth()) / 2, (int) (max_height - f.getHeight() ) / 2);
f.setResizable(false);
f.setVisible(true);
GridLayout fLayout1 = new GridLayout(2, 1);
fLayout1.setColumns(1);
fLayout1.setHgap(5);
fLayout1.setVgap(5);
fLayout1.setRows(2);
f.getContentPane().setLayout(fLayout1);
JPanel rData = new JPanel();
GridBagLayout rDataLayout = new GridBagLayout();
rDataLayout.rowWeights = new double[] {0.1, 0.1, 0.1, 0.1};
rDataLayout.rowHeights = new int[] {7, 7, 7, 7};
rDataLayout.columnWeights = new double[] {0.1, 0.1, 0.1, 0.1};
rDataLayout.columnWidths = new int[] {7, 7, 7, 7};
rData.setLayout(rDataLayout);
JLabel lblUser = new JLabel();
lblUser.setText("Username: ");
rData.add(lblUser, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
txtUser = new JTextField(10);
txtUser.setBorder(BorderFactory.createLoweredBevel Border());
txtUser.setHorizontalAlignment(JTextField.CENTER);
rData.add(txtUser, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
JLabel lblPass = new JLabel();
lblPass.setText("Password: ");
rData.add(lblPass, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
txtPass = new JPasswordField(10);
txtPass.setBorder(BorderFactory.createLoweredBevel Border());
txtPass.setHorizontalAlignment(JTextField.CENTER);
rData.add(txtPass, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
f.getContentPane().add(rData);
JPanel buttonPanel = new JPanel();
btnSave = new JButton();
btnSave.setText("Save");
btnSave.addActionListener(this);
buttonPanel.add(btnSave);
btnCancel = new JButton();
btnCancel.setText("Cancel");
btnCancel.addActionListener(this);
buttonPanel.add(btnCancel);
f.getContentPane().add(buttonPanel);
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnSave) {
username = txtUser.getText();
password = new String(txtPass.getPassword());
System.out.println("username: " + username);
System.out.println("password: " + password);
f.setVisible(false);
f.dispose();
}
if (e.getSource() == btnCancel) {
f.setVisible(false);
f.dispose();
}
}
}

