Code Error on Event Handler

0 Matic Kržičnik · January 27, 2015
Hello can any one halp me with this error. If i use fixer, program dont work when i press enter. ty
/images/forum/upload/2015-01-27/faae72ec643ad160078615f1080768b3.jpg

Post a Reply

Replies

Oldest  Newest  Rating
+1 Nicholas Eason · January 27, 2015
Basically, when extending the class ActionListener, you need to use the actionPerformed() method. You didn't do that.

public void actionPerformed(ActionEvent e){
//Whatever you need here
}


Also, why not just have the JFrame class you're using implement ActionListener instead? It's the same situation.
+1 Branislav Lazic · January 27, 2015
Or pass lambda expression in your addActionListener method. Lambdas are introduced as a replacement for anonymous inner classes that have single abstract method.


passwordField.addActionListener(ae -> { // do stuff});


"ae" represents ActionEvent parameter (parameters that your method takes in single abstract method interface (ActionListener is SAM in this case)). More readable version:


passwordField.addActionListener((ActionEvent ae) -> { // do stuff});
+1 Jasmin Bakalović · January 28, 2015
You have a semicolon ";" in your if statement. You need to delete.

Remember, if you want to use if statement without curly braces then:

if (condition)

    System.out.print("Text"); // This line of code will be executed
    System.out.print("Text 2"); // This line of code will be also executed but its not linked with this if stement

Otherwise, if you use in your if statement curly braces then:


if (condition) {

    System.out.print("Text"); // This line of code will be executed
    System.out.print("Text 2"); // This line of code will be also executed and now its linked with this if statement
}
+1 Jasmin Bakalović · January 28, 2015
I don't know how to put lambda expression. I hear for that for the first time.

If you want to know more about that here's video:

0 Matic Kržičnik · January 27, 2015

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;

public class importEventJFrames extends JFrame{
private JTextField item1;
private JTextField item2;
private JTextField item3;
private JPasswordField passwordField;

public importEventJFrames (){
super("This is TITLE");
setLayout(new FlowLayout());

item1=new JTextField (20);
add(item1);

item2=new JTextField ("enter text here");
add(item2);

item3=new JTextField ("uneditabale",20);
item3.setEditable(false);
add(item3);

passwordField=new JPasswordField("8888",20);
add(passwordField);

thehandler handler = new thehandler ();
item1.addActionListener(handler);
item2.addActionListener(handler);
item3.addActionListener(handler);
passwordField.addActionListener(handler);
}
private class thehandler implements ActionListener{
@SuppressWarnings("unused")
public void actionPreformed(ActionEvent event){

String string="";

if(event.getSource()==item1);
string=String.format("fild1 : %s", event.getActionCommand());
if(event.getSource()==item2);
string=String.format("fild2 : %s", event.getActionCommand());
if(event.getSource()==item3);
string=String.format("fild3 : %s", event.getActionCommand());
if(event.getSource()==passwordField);
string=String.format("fild Password : %s", event.getActionCommand());


JOptionPane.showMessageDialog(null, string);
}
}
}
0 Matic Kržičnik · January 27, 2015
Ty a lot to all.
0 Matic Kržičnik · January 28, 2015
Branislav can you show me how to put thet new coud in to my. I try a bit but i dont have idea what i am doing.



Question number 2: 
if (event.getSource()==item1);
string=String.format("fild1 : %s", event.getActionCommand());
else if (event.getSource()==item2);
string=String.format("fild2 : %s", event.getActionCommand());
else if(event.getSource()==item3);
string=String.format("fild3 : %s", event.getActionCommand());
else if(event.getSource()==passwordField);
string=String.format("fild Password : %s", event.getActionCommand());


Why i get error on else if ->  Multiple markers at this line.
0 Matic Kržičnik · January 28, 2015
ty do you know how to put  lambda expression in to my code?
  • 1

Java / Android Development

107,116 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator