New Java user ActionListener question
Hello all,
I'm a new Java user and I'm trying out the ActionListener feature but I am not sure what is the best way to implement it. Below is my code where I've been testing out different Java features.
Based on my code below, what is the best way to add an ActionListener to the OK Button and the Close Button? Perhaps the OK Button has a MessageBox that says "OK Button Pressed" and the Close Button closes the application.
Thanks for the help!
VBGuy
Code Java:
package test;
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(String[] args) {
//Hello();
//Calc();
ShowFrame();
}
public static void Hello() {
System.out.println("Hello World");
}
public static void Calc() {
int a, b, c;
a = 1;
b = 1;
c = a + b;
System.out.println(c);
}
public static void ShowFrame() {
JFrame MyWindow = new JFrame("Hello World");
MyWindow.setSize(800, 600);
MyWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyWindow.setLayout(null);
//Text Field
JTextField bbox = new JTextField(16);
bbox.setBounds(10, 10, 100, 25);
//Add Buttons
JButton CloseButton = new JButton("Close");
CloseButton.setBounds(10, 100, 90, 40);
JButton OkButton = new JButton("Ok");
OkButton.setBounds(100, 100, 90, 40);
//Add to Frame
MyWindow.add(CloseButton);
MyWindow.add(bbox);
MyWindow.add(OkButton);
//Set Visible
MyWindow.setVisible(true);
bbox.setVisible(true);
CloseButton.setVisible(true);
OkButton.setVisible(true);
//Set Action Listener
CloseButton.addActionListener(null);
}
}
Re: New Java user ActionListener question
Quote:
what is the best way to add an ActionListener
The only way I know is to use the addActionListener() method.
Re: New Java user ActionListener question
You must implement the ActionListener interface by creating a new class or have your main class implement it for you. See
How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
Re: New Java user ActionListener question
Please surround your code snippets with [highlight=Java]CODE goes here[/highlight]. It makes reading the code much easier.
Re: New Java user ActionListener question
Or you can simply create an anonymous class when adding the listener.
Example:
Code Java:
//Set Action Listener
CloseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
//actions perform when the CloseButton is clicked
}
});
The other way by creating inner class would look like this:
Code Java:
public class Main {
...
TheHandler handler = new TheHandler();
CloseButton.addActionListener(handler);
...
//////// private inner class ////////
private class TheHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
//actions perform when the CloseButton is clicked
}
}
}
Re: New Java user ActionListener question
I think the inner class is the best method by my opinion. It means you can create 1 actionListener that takes account of all possible actions and whatnot. By doing the other way, you have to create a new actionListener that is specific to that object. Here is a little bit of helper code for my perferred method:
Code java:
public class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if(source instanceof JButton)
{
if(source == fileChooserButton)
{...}
else if(source == runButton)
{...}
}
else if(source instanceOf JComboBox)
{
if(source == whatever)
{...}
}
}
}
And to assign the actionListener, you do this:
Code java:
ActionListener listener = new ButtonListener();
fileChooserButton.addActionListener(listener);
runButton.addActionListener(listener);
whatever.addActionListener(listener);
As long as you include the instanceOf comparisons and the object comparisons in the ActionListener, the listener will run fast and accurately. You can do it without them, but including them means including less if/else statements.
Re: New Java user ActionListener question
Quote:
create a new actionListener that is specific to that object
That can make for easier to maintain code that will have fewer bugs and cause fewer problems.
Having it all in one method multiplies the chances of coding/logic problems.
Re: New Java user ActionListener question
Try this:
Code Java:
package test;
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(String[] args) {
//Hello();
//Calc();
ShowFrame();
}
public static void Hello() {
System.out.println("Hello World");
}
public static void Calc() {
int a, b, c;
a = 1;
b = 1;
c = a + b;
System.out.println(c);
}
public static void ShowFrame() {
JFrame MyWindow = new JFrame("Hello World");
MyWindow.setSize(800, 600); // kinda big isn't it?
MyWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyWindow.setLayout(null);
//Text Field
JTextField bbox = new JTextField(16);
bbox.setBounds(10, 10, 100, 25);
//Add Buttons
JButton CloseButton = new JButton("Close");
CloseButton.setBounds(10, 100, 90, 40);
JButton OkButton = new JButton("Ok");
OkButton.setBounds(100, 100, 90, 40);
//Add to Frame
MyWindow.add(CloseButton);
MyWindow.add(bbox);
MyWindow.add(OkButton);
//Set Visible
MyWindow.setVisible(true);
bbox.setVisible(true);
CloseButton.setVisible(true);
OkButton.setVisible(true);
//Set Action Listener
CloseButton.addActionListener(this);
OkButton.addActionListener(this);
}
public void actionPerformed (ActionEvent e)
{
if (e.getActionCommand().equals("Close")
{
System.exit(0); // I'm assuming that's what you want by close
}
else if (e.getActionCommand().equals("Ok")
{
// code for whatever ok button is supposed to do
}
}
}
[/QUOTE]