
10-27-2008, 07:28 PM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 5
Rep Power: 0
|
|
User settings
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();
}
}
}
|
|

10-27-2008, 07:55 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
|
|
|
Quote:
|
|
frame which prompts user for various "account settings" values and then return them to the main window.
|
Use a custom dialog that gets the values, builds an object with those values and returns that object to the caller.
Usage:
CustomDialog cd = new CustomDialog(...);
ValueObj valObj = cd.getValues();
|
|

10-27-2008, 08:06 PM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 5
Rep Power: 0
|
|
Re:
Thanks Norm,
I will give it try.
|
|

10-27-2008, 08:09 PM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 5
Rep Power: 0
|
|
|
would the JDialog preserve the look and feel? or use the system default?
|
|

10-27-2008, 08:23 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
|
|
|
Since its Swing, I think it would be tailorable.
|
|

10-27-2008, 09:31 PM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 5
Rep Power: 0
|
|
Originally Posted by Norm
|
|
Since its Swing, I think it would be tailorable.
|
Hi Norm.... try as you suggested and is working. The only issue now is that JDialog does not keep the look and feel.
how can we set the look and feel for a JDialog?
|
|

10-27-2008, 09:59 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
|
|
|
Sorry, not very good at GUI.
Read the API or wait for a GUI guru
|
|

10-31-2008, 12:30 PM
|
|
Member
|
|
Join Date: Oct 2008
Posts: 1
Rep Power: 0
|
|
|
yes actually i dont know
|
|

10-31-2008, 12:53 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,513
Rep Power: 11
|
|
|
What you mean, what's your question?
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +2. The time now is 03:40 PM.
|
|