The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
January 2002

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Passing variables in Swing

Posted by KABOOM on January 18, 2002 at 1:10 AM

I've got a bit of a dilemma. I'm just trying to make a basic login but the problem is, I have this "OK" button coded as:

okb.addActionListener(this);

This goes to a action performed method. Great, nothing abnormal there.
public void actionPerformed(ActionEvent pressbtn)
{
// Do stuff
}

The problem is, I want to access the data in the username and password fields but I can't unless I pass them to the actionPerformed method. If I do that, the compiler yells at me.
So, I tried to declare and save them as private variables but nothing gets saved until the user clicks the button and then i've lost the value for hte username/password.

Can anyone help me out and solve this little dilemma? I've attached my code and the relevant areas below.

Look for ****************************************
Thanks,
Mike

public class NamePass extends JFrame implements ActionListener
{
************* MY PRIVATE VAR STRATEGY WHICH DOESN'T WORK******
private JTextField name;
private JPasswordField pass;


public NamePass()
{
super("Username and Password");
setSize(290, 110);
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();


// TEXT: NAME
JPanel pane = new JPanel();
pane.setLayout(gridbag); // Name label
buildConstraints(constraints, 0, 0, 1, 1, 10, 40);
constraints.fill = GridBagConstraints.NONE; // Stretch component
constraints.anchor = GridBagConstraints.EAST; // Align
JLabel label1 = new JLabel("Name:", JLabel.LEFT); //Align
gridbag.setConstraints(label1, constraints); // Apply constraints to button
pane.add(label1); // Add Name text field


// TEXTFIELD: NAME
buildConstraints(constraints, 1, 0, 1, 1, 90, 0);
constraints.fill = GridBagConstraints.HORIZONTAL;
JTextField tfname = new JTextField();
gridbag.setConstraints(tfname, constraints);
pane.add(tfname); // password label


// TEXT: PASSWORD
buildConstraints(constraints, 0, 1, 1, 1, 0, 40);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.EAST;
JLabel label2 = new JLabel("Password:", JLabel.LEFT);
gridbag.setConstraints(label2, constraints);
pane.add(label2); // password text field


// TEXTFIELD: PASSWORD
buildConstraints(constraints, 1, 1, 1, 1, 0, 0);
constraints.fill = GridBagConstraints.HORIZONTAL;
JPasswordField tfpass = new JPasswordField();
tfpass.setEchoChar('*');
gridbag.setConstraints(tfpass, constraints);
pane.add(tfpass); // OK Button


// BUTTON: OK
buildConstraints(constraints, 0, 2, 2, 1, 0, 20);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.CENTER;
JButton okb = new JButton("OK");
******** I WANT TO PASS MY PASSWORD (tfpass) and USERNAME (tfuser) *****************************
okb.addActionListener(this); // Will check with datfile
************************************************************

gridbag.setConstraints(okb, constraints);
pane.add(okb); // Content Pane

setContentPane(pane);
}


public void actionPerformed(ActionEvent pressbtn)
{
**********************************************
//Want to access pass and name private vars. How do I get to them
**********************************************
}

public static void main(String[] arguments)
{
NamePass frame = new NamePass();
ExitWindow exit = new ExitWindow();
frame.addWindowListener(exit);
frame.show();
}
}




Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us