Hey,
I'm trying to learn Java (The GUI part right now), but my code won't do as intetden.
What it should do:
When I press enter in a field, then show the dialog (See fat text in code (Can't use FAT in code, so look at the [/B])
My Problem
It will only show the message in the password-part (Which I think is stupid)
Code:
public class EHTwo extends JFrame{ private JTextField itemOne; private JTextField itemTwo; private JTextField itemThree; private JPasswordField passwordField; public EHTwo(){ super("The Title"); setLayout(new FlowLayout()); //10 is the max length. itemOne = new JTextField(10); //This is like a PROMT itemTwo = new JTextField("TRY ENTER!"); itemThree = new JTextField("Uneditebale", 20); //This makes sure that the user can't edit it. itemThree.setEditable(false); //mypass is the default password passwordField = new JPasswordField("mypass"); add(itemOne); add(itemTwo); add(itemThree); add(passwordField); TheHandler handler = new TheHandler(); itemOne.addActionListener(handler); itemTwo.addActionListener(handler); itemThree.addActionListener(handler); passwordField.addActionListener(handler); } //When a class is inside another class, then it will INHERITANCE all the stuff form the first. private class TheHandler implements ActionListener{ //This has to be name actionPerformed. (ActionEvent) is the type. event is the name. public void actionPerformed(ActionEvent event){ //This is the final string we output. String string = ""; [B]//This is to check which box is clicked.[/B] if(event.getSource()==itemOne){ //This is going to set the string. string=String.format("Field One %s", event.getActionCommand()); //event.getActionCommand() means that it is gonna get the information form the "box" /point /object it's at. } else if(event.getSource()==itemTwo){ string=String.format("Field Two %s", event.getActionCommand()); } else if(event.getSource()==itemThree){ string=String.format("Field Three %s", event.getActionCommand()); } else if(event.getSource()==passwordField){ string=String.format("Password Field is: %s", event.getActionCommand()); JOptionPane.showMessageDialog(null, string); } } }
Thanks, I hope that you can read the code, if not then message me