Hi everyone.
<BR>
I have made repeated attempts to solve this but to no avail. When I click an entry in the JComboBox, the program prints the name *twice*. Why is that? The goal of this project is to create a student rolodex that will display a textarea containing a students personal info ( name, phone number, etc. obtained from a file ) when their name is selected from the JComboBox.
( code below ).
If someone could either point me in the right direction or provide a small code sample, I would be most appreciative.
Thanks,
-CaitlinG.
<code>
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.io.*;
public class test36 extends JFrame implements ActionListener,
ItemListener
{
private JTextField fname,
lname;
private TextField myPass;
private JButton login,
quit,
edit,
changePass;
private ImageIcon[] images = new ImageIcon[10];
private JComboBox myCombo;
private JFrame newFrame;
private JPanel newPanel;
private JLabel newLabel;
int numTries = 0;
private
String password;
private String oldPass;
private String newPass;
private String[] students = { "Students", "Caitlin Gibbons",
"Charles Kraver", "Carole Kowitt",
"Linda Donner", "James Trimble",
"Spencer Lyman", "Lena Rapp",
"Anne Garney" };
BufferedReader myReader = null;
PrintWriter myWriter = null;
public static void main( String args[] )
{
test36 myTest = new test36();
myTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myTest.pack();
myTest.setVisible(true);
}
public test36()
{
newFrame = new JFrame();
newPanel = new JPanel();
setTitle("User Interface Test");
JPanel myPanels = new JPanel();
myPanels.setLayout( new GridLayout( 4, 1 ));
myPanels.add( new JLabel("First Name:"));
myPanels.add( new JLabel("Last Name:"));
myPanels.add( new JLabel("Password:"));
myPanels.setBackground( Color.yellow );
JPanel myTextFields = new JPanel();
myTextFields.setLayout( new GridLayout( 4, 1 ));
myTextFields.add( fname = new JTextField(15));
myTextFields.add( lname = new JTextField(15));
myTextFields.add( myPass = new TextField(10));
myPass.setEchoChar('*');
myTextFields.setBackground( Color.yellow );
JPanel p1 = new JPanel();
p1.setLayout(new BorderLayout());
p1.add( myPanels, BorderLayout.WEST );
p1.add( myTextFields, BorderLayout.CENTER );
p1.setBackground( Color.yellow );
JPanel p2 = new JPanel();
p2.setLayout( new FlowLayout(FlowLayout.LEFT));
p2.add( quit = new JButton("quit"));
quit.setToolTipText("Exits the application");
quit.setMnemonic('q');
p2.add( login = new JButton("login"));
login.setToolTipText("Submit user authentication info");
login.setMnemonic('l');
p2.add( edit = new JButton("edit"));
edit.setToolTipText("Edit list of students");
edit.setMnemonic('d');
edit.setVisible(false);
p2.add( changePass = new JButton("change password"));
changePass.setToolTipText("Change current password");
changePass.setMnemonic('c');
changePass.setVisible(false);
p2.setBackground( Color.yellow );
JPanel p3 = new JPanel();
p3.setBackground( Color.yellow );
p3.setLayout( new GridLayout( 4, 1));
p3.add(myCombo = new JComboBox( students ));
myCombo.setVisible(false);
p3.setBackground( Color.yellow );
images[0] = new ImageIcon("Glenda.jpg");
getContentPane().setLayout( new BorderLayout());
getContentPane().add(p1, BorderLayout.WEST);
getContentPane().add(p2, BorderLayout.SOUTH);
getContentPane().add(p3, BorderLayout.EAST);
login.addActionListener( this );
quit.addActionListener( this );
changePass.addActionListener( this );
myCombo.addItemListener( this );
try
{
myReader = new BufferedReader( new FileReader("pass.txt"));
password = myReader.readLine();
myReader.close();
}
catch( IOException e )
{
}
}
public void setPass( String newPassword )
{
password = newPassword;
try
{
myWriter = new PrintWriter( new FileOutputStream("pass.txt"));
myWriter.println(password);
myWriter.close();
}
catch( IOException e )
{
}
}
public String getPass()
{
return password;
}
public void itemStateChanged( ItemEvent e )
{
int index = myCombo.getSelectedIndex();
if( index == 1 )
System.out.println("I see Caitlin!\n");
//newLabel.setIcon( images[index] );
//newFrame.getContentPane().add( newPanel );
//newFrame.pack();
//newFrame.show();
}
public void actionPerformed( ActionEvent e )
{
String actionCommand = e.getActionCommand();
if( e.getSource() instanceof JButton )
{
if( "login".equals( actionCommand ))
{
if( fname.getText().equals("kim") &&
lname.getText().equals("milton") &&
myPass.getText().equals(password) )
{
edit.setVisible(true);
myCombo.setVisible(true);
changePass.setVisible(true);
//JOptionPane.showMessageDialog(null, "Thanks" );
}
else
{
JOptionPane.showMessageDialog(null, "Incorrect login" );
numTries++;
if( numTries >= 3 )
{
JOptionPane.showMessageDialog( null, "Authorization failure\n\n" +
"Exiting application..." );
System.exit(0);
}
}
}
if( "quit".equals( actionCommand ))
System.exit(0);
if( "change password".equals( actionCommand ))
{
oldPass = JOptionPane.showInputDialog("Enter old password: ");
if( !(oldPass.equals( password )))
{
JOptionPane.showMessageDialog( null, "Incorrect password" );
System.exit(0);
}
else
{
newPass = JOptionPane.showInputDialog("Enter new password: ");
setPass( newPass );
if( getPass().equals( newPass ))
JOptionPane.showMessageDialog( null, "Password update successful" );
}
}
}
}
}
</code>