Reputation: 303
I imported all the necessary classes and there are no errors. But when I press enter on any of the text field, the prompt window is blank instead of a string that it intend to show. I don't if the problem is with JOptionPane or what. When I run my code for the first time it was working fine on the second run, there is a blank prompt.
public class fish extends JFrame{
private JTextField item1;
private JTextField item2;
private JTextField item3;
private JPasswordField password;
public fish(){
super("Title");
setLayout(new FlowLayout());
item1=new JTextField(10);
add (item1);
item2=new JTextField("Enter Txt here");
add (item2);
item3=new JTextField("Uneditable",20);
item3.setEditable(false);
add (item3);
password = new JPasswordField("MyPass");
add(password);
thehandler handler = new thehandler();
item1.addActionListener(handler);
item3.addActionListener(handler);
item2.addActionListener(handler);
password.addActionListener(handler);
}
private class thehandler implements ActionListener{
public void actionPerformed(ActionEvent event){
String string = "";
if(event.getSource()==item1)
String.format("field1: %s", event.getActionCommand());
else if(event.getSource()==item2)
String.format("field2: %s", event.getActionCommand());
else if(event.getSource()==item3)
String.format("field3: %s", event.getActionCommand());
else if(event.getSource()==password)
String.format("Password Field: %s", event.getActionCommand());
JOptionPane.showMessageDialog(null, string);
}
}
}
public class apples{
public static void main(String args[]){
fish tuna = new fish();
tuna.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tuna.setSize(350,100);
tuna.setVisible(true);
}
}
Upvotes: 1
Views: 152
Reputation: 285405
You're displaying an empty String. Here:
public void actionPerformed(ActionEvent event){
String string = "";
if(event.getSource()==item1)
String.format("field1: %s", event.getActionCommand());
else if(event.getSource()==item2)
String.format("field2: %s", event.getActionCommand());
else if(event.getSource()==item3)
String.format("field3: %s", event.getActionCommand());
else if(event.getSource()==password)
String.format("Password Field: %s", event.getActionCommand());
JOptionPane.showMessageDialog(null, string);
}
Where do you assign anything to the String variable, string
? Also, you never get the text from the JTextField but rather an actionCommand String that was never assigned.
i.e.,
public void actionPerformed(ActionEvent event){
String string = "";
if(event.getSource()==item1) {
string = String.format("field1: %s", item1.getText())
}
else if(event.getSource()==item2) {
string = String.format("field2: %s", item2.getText());
}
else if(event.getSource()==item3) {
string = String.format("field3: %s", item3.getText());
}
else if(event.getSource()==password) {
string = String.format("Password Field: %s", new String(password.getPassword()));
}
JOptionPane.showMessageDialog(null, string);
}
new String(password.getPassword())
, as this makes your program very unsafe as the password will be easy to steal. Upvotes: 3