Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-27-2008, 07:28 PM
Member
 
Join Date: Oct 2008
Posts: 5
Rep Power: 0
3speed is on a distinguished road
Default 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();
}
}
}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 10-27-2008, 07:55 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
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();
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-27-2008, 08:06 PM
Member
 
Join Date: Oct 2008
Posts: 5
Rep Power: 0
3speed is on a distinguished road
Default Re:
Thanks Norm,

I will give it try.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 10-27-2008, 08:09 PM
Member
 
Join Date: Oct 2008
Posts: 5
Rep Power: 0
3speed is on a distinguished road
Default
would the JDialog preserve the look and feel? or use the system default?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 10-27-2008, 08:23 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
Since its Swing, I think it would be tailorable.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 10-27-2008, 09:31 PM
Member
 
Join Date: Oct 2008
Posts: 5
Rep Power: 0
3speed is on a distinguished road
Default
Originally Posted by Norm View Post
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?
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 10-27-2008, 09:59 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
Sorry, not very good at GUI.
Read the API or wait for a GUI guru
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 10-31-2008, 12:30 PM
Member
 
Join Date: Oct 2008
Posts: 1
Rep Power: 0
csumanbabu is on a distinguished road
Default
yes actually i dont know
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 10-31-2008, 12:53 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,513
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
What you mean, what's your question?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
JVm Heap memory settings nagesh Advanced Java 2 09-17-2009 06:47 PM
Graphic settings being overridden? sjchase New To Java 0 01-17-2008 12:27 AM
Saving application settings mwildam New To Java 0 11-16-2007 03:30 PM
JVM Heap memory settings nagesh New To Java 1 08-11-2007 11:17 PM
Detecting Browser Settings arupranjans JavaServer Pages (JSP) and JSTL 0 07-31-2007 03:31 PM


All times are GMT +2. The time now is 03:40 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org