Java: Event Handling
Ok, so in this lesson we are going to do some event handling, this is some pretty difficult stuff so if you don't understand it at the end, try rereading the lesson.
Ok so firstly, lets create a new class and call it App(.java).
Ok. So now we've got a class lets get it setup. We must firstly import all of the things we will need:
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;
Ok, lets explain these:
FlowLayout was explained in previous lessons.
ActionListener simply allows us to listen or detect actions by the user.
ActionEvent simple lets us handle events.
JFrame we have discussed in previous lessons.
JTextField and JPasswordField are pretty self-explanatory.
And JOptionPane we have discussed in previous lessons.
Now lets make App inherit from JFrame:
public class App extends JFrame{
Now lets create the variables for the items we are going to put on the form:
private JTextField ItemOne; private JTextField ItemTwo; private JTextField ItemThree; private JPasswordField Pass;
Now we can create our class constructor, add a title, set the layout and all of that stuff:
public App(){
super("The Title!");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300, 200);
setLayout(new FlowLayout());
Next lets set ItemOne:
ItemOne = new JTextField(9); add(ItemOne);
All this does is create a new textfield and make it have a length of 9 characters and then adds it to the form.
Now for Items Two and Three:
ItemTwo = new JTextField("You Cant Edit Me!", 9);
ItemTwo.setEditable(false);
add(ItemTwo);
ItemThree = new JTextField("Default Text", 15);
add(ItemThree);
As you can see we have used some new stuff here, but its all pretty self-explanitory..
Now just for our password field:
Pass = new JPasswordField(); add(Pass);
Ok.. Now we can use some action listeners and stuff to do some real event handling.
For this addition to functionality we are going to create another class, this one is going to be called Handler; However firstly lets do some more stuff in the App class while we are here. Lets create our handler object:
Handler handler = new Handler();
And now we have to apply an ActionListener to each item on the form, (using the new class object(this will become clear in a second when we create the Handler class)):
ItemOne.addActionListener(handler); ItemTwo.addActionListener(handler); ItemThree.addActionListener(handler); Pass.addActionListener(handler);
Ok, so now we just need to set the thing to visible, and then the closing bracket for the constructor:
setVisible(true); }
Now we are going to create Handler, but in a slightly different way that usual, as we are going to do it inside the App class (but outside the constructor obviously), so after the closing curly brackets of your constructor, create the ner class like this:
private class Handler implements ActionListener{
}
All this means is the class Handler gets all the stuff from App (because its in App), and it deals with ActionListener stuff.
Ok now all we want in our new class is stuff to manage what happens when a certain action is performed, so lets create an actionPerformed function:
public void actionPerformed(ActionEvent event){
}
You must call it this, and it must accept an ActionEvent as its arguments.So lets start creating what happens when an action is performed.
To do this we can get the source of the event, and then act appropriately. Lets write some stuff into our function:
String MyString = "";
if(event.getSource()==ItemOne){
MyString = String.format("ItemOne:%s", event.getActionCommand());
} else if(event.getSource()==ItemTwo){
MyString = String.format("ItemTwo:%s", event.getActionCommand());
} else if(event.getSource()==ItemThree){
MyString = String.format("ItemThree:%s", event.getActionCommand());
}else if(event.getSource()==Pass){
MyString = String.format("Password:%s", event.getActionCommand());
}
You should understand most of this, the only bit you wont understand is:
event.getActionCommand()
This simply gets whats in the textboxes.
Now lets create a pop-up to display 'MyString':
JOptionPane.showMessageDialog(null, MyString);
We are now done with all this class stuff, we can go back to main and call all of this stuff with a simple object:
App Object = new App();
We can now run this application successfully! (for those who didn't know a textfield's action by default is pressing the enter button on your keyboard)
The full code for this lesson:
App.java:
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 App extends JFrame{
private JTextField ItemOne;
private JTextField ItemTwo;
private JTextField ItemThree;
private JPasswordField Pass;
public App(){
super("The Title!");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300, 200);
setLayout(new FlowLayout());
ItemOne = new JTextField(9);
add(ItemOne);
ItemTwo = new JTextField("You Cant Edit Me!", 9);
ItemTwo.setEditable(false);
add(ItemTwo);
ItemThree = new JTextField("Default Text", 10);
add(ItemThree);
Pass = new JPasswordField(10);
add(Pass);
Handler handler = new Handler();
ItemOne.addActionListener(handler);
ItemTwo.addActionListener(handler);
ItemThree.addActionListener(handler);
Pass.addActionListener(handler);
setVisible(true);
}
private class Handler implements ActionListener{
public void actionPerformed(ActionEvent event){
String MyString = "";
if(event.getSource()==ItemOne){
MyString = String.format("ItemOne:%s", event.getActionCommand());
} else if(event.getSource()==ItemTwo){
MyString = String.format("ItemTwo:%s", event.getActionCommand());
} else if(event.getSource()==ItemThree){
MyString = String.format("ItemThree:%s", event.getActionCommand());
}else if(event.getSource()==Pass){
MyString = String.format("Password:%s", event.getActionCommand());
}
JOptionPane.showMessageDialog(null, MyString);
}
}
}
Main.java:
public class Main {
public static void main(String[] args) {
App Object = new App();
}
}
Back to Java

