Results 1 to 9 of 9

Thread: User settings

  1. #1
    3speed is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    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();
    }
    }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default

    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();

  3. #3
    3speed is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default Re:

    Thanks Norm,

    I will give it try.

  4. #4
    3speed is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default

    would the JDialog preserve the look and feel? or use the system default?

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default

    Since its Swing, I think it would be tailorable.

  6. #6
    3speed is offline Member
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Default

    Quote 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?

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default

    Sorry, not very good at GUI.
    Read the API or wait for a GUI guru

  8. #8
    csumanbabu is offline Member
    Join Date
    Oct 2008
    Posts
    1
    Rep Power
    0

    Default

    yes actually i dont know

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. JVm Heap memory settings
    By nagesh in forum Advanced Java
    Replies: 2
    Last Post: 09-17-2009, 05:47 PM
  2. Graphic settings being overridden?
    By sjchase in forum New To Java
    Replies: 0
    Last Post: 01-16-2008, 11:27 PM
  3. Saving application settings
    By mwildam in forum New To Java
    Replies: 0
    Last Post: 11-16-2007, 02:30 PM
  4. JVM Heap memory settings
    By nagesh in forum New To Java
    Replies: 1
    Last Post: 08-11-2007, 10:17 PM
  5. Detecting Browser Settings
    By arupranjans in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 07-31-2007, 02:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •