I am currently working on Java Swing lessons after finishing my first set of lessons in Java. In this lesson, we are working on communication between different components (buttons,toolbars,etc.) that we have been studying. The problem is, "this" is being passed as a method argument for the addActionListener() method. This works, however, I do not completely understand what it is doing. I did some research regarding "this," and found that the most popular usage for the "this" keyword would be in constructors with variables of the same names. I could not find an example that would fit my case. By looking at the code below, I will explain that parts of the code that I understand.
import java.awt.FlowLayout; Implements FlowLayout class
import java.awt.event.ActionEvent; //Imports ActionEvent Class
import java.awt.event.ActionListener; //Imports ActionListener Interface
import javax.swing.JButton; //Imports JButton class
import javax.swing.JPanel; //Imports JPanel class
public class Toolbar extends JPanel implements ActionListener {
// ^ Inherits JPanel & Implements ActionListener Interface
private JButton helloButton; //Creating variable of JButton type
private JButton goodbyeButton; //Creating variable of JButton type
public Toolbar() { //Constructor
helloButton = new JButton("Hello"); //Creates new JButton
goodbyeButton = new JButton("Goodbye"); //Creates new JButton
helloButton.addActionListener(this); //Question 1
goodbyeButton.addActionListener(this); //Question 1
setLayout(new FlowLayout(FlowLayout.LEFT)); //Sets Layout Type to Left
add(helloButton); //Adds button to FlowLayout (Layout Manager) Interface
add(goodbyeButton); //Adds button to FlowLayout (Layout Manager) Interface
}
public void setTextPanel(TextPanel textPanel) {
//No Usage Yet!
}
public void actionPerformed(ActionEvent arg0) { //Unimplemented Method from ActionListener
System.out.println("A button was clicked"); //Prints after button is clicked.
}
}
Question 1: As you can see, after creating two JButtons, we are adding an addActionListener() method in order to see detect if the button has been clicked. If it has been clicked, then it will do any code typed in actionPerformed that is implemented from the ActionListener interface. In previous lessons, we had a different way of making a button respond to a click. It did the same thing, but it looked like this:
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textPanel.appendText("Hello\n");
}
});
In this example, rather than printing to the console to test to see if the actionListener() worked, we just appended the text to a text area from a customized component. In the code directly above this text, we are working with an anonymous class.
So... The question is, what exactly is "this" doing as it is being passed as a method argument in addActionListener()? I know that it is printing "A button was clicked" to the console, but I don't understand the logic behind what "this" is doing to send that the button has been clicked to actionPerformed().
This is what the applications looks like:
This is the application in motion, and the buttons have already printed to the console after being clicked. I thought it might give you a better idea of what I am working on. I hope somebody can shed some light on this, and explain how "this" is working in this context. Thank you!
this represents the current instance of your class.
Toolbar class implements ActionListener interface, that means it provides an implementation of method actionPerformed.
So, helloButton.addActionListener(this); becomes possible (try to remove implements ActionListener from class declaration, the code won't compile).
Saying this, Toolbar instances can be considered as ActionListener objects and can be passed to button.addActionListener.
When using
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
}
}
you create a new implementation of the ActionListener interface in the fly. This kind of implementation is named anonymous.
The both solutions are valid. Prefer the anonymous solution if the code in actionPerformed cannot be used from another place.
this is just an instance of your Toolbar class. It is used inside instance methods to represent the current instance. Essentially you can imagine your class has another private member variable named this, that points to a Toolbar object.
Since Toolbar implements ActionListener you are assigning the newly instantiated Toolbar object as a listener to the buttons (meaning the Toolbar.actionPerformed() method will be called when the buttons are clicked.)
By implementing ActionListener and then passing this to addActionListener method you are asking to be informed of any actions that are performed through a call to your actionPerformed method.
Or, more simply, you are giving the button your phone number and asking it to call you whenever something happens.
In the earlier lessons you were stating what should happen - print this text or add some text to the textPanel. To do this you were making an ActionListener on-the-fly. Now you implement ActionListener yourself you can request a callback to you anstead of making a listener one on-the-fly.
The addActionListener method for a swing component takes an argument of type ActionListener. A class that implements an ActionListener contains code that specifies what should be done when someone interacts with a swing component.
When you pass this to the addActionListener method, you are passing a reference to the current object that is being instantiated. As it happens, the current object being instantiated is also an ActionListener (as it implements ActionListener) and can therefore be passsed to the addActionListener method.
When you interact with the JButton in the GUI, the actionPerformed method of Toolbar class will be called since that is the ActionListener that you registered the JButton with.
These two examples would do the same thing (assuming all imports are there, and that capitalization would be correct):
public static void main(String args[]){
myFrame frame = new myFrame();
myFrame.addActionListener(new myListener);
}
public myFrame extends JFrame{
public myFrame(){
super("myFrame");
}
}
public myListener implements ActionListener{
public void ActionPerformed(ActionEvent e){
//Do Stuff
}
}
and
public static void main(String args[]){
myFrame frame = new myFrame();
}
public myFrame extends JFrame implements ActionListener{
public myFrame(){
super("myFrame");
this.add(this);
}
public void ActionPerformed(ActionEvent e){
//Do Stuff
}
}
The advantage of having the ActionListener be this is that it would have more direct access to fields and methods, if you want to make the modifier on the methods private.
However, if you want to avoid if/else if monstrosities for handling multiple buttons, I recommend either using a separate ActionListener(and providing a means to change what needs to be changed), or taking a look at anonymous classes and lambda expressions.
Often with object-oriented coding, it's worth role-playing the part of an object of the class you're coding. If you do that, "this" means "me".
And because Java is pass-by-reference, "this" is an arrow pointing to "me".
public class Foo implements ActionListener {
public void actionPerformed(ActionEvent e) {
// do something useful with e
}
}
Here we've written a class Foo. Since we've said it implements ActionListener, it must have an actionPerformed() method. Anything can call that:
ActionListener listener = new Foo(...);
ActionEvent event = ...;
foo.actionPerformed(event);
And we can create a Foo and give it to something that generates events:
ActionListener listener = new Foo(...);
button.addListener(listener);
If we role-play the object, you can think of this last line as "Hey button! Tell listener whenever an action happens.".
Now, what if we want the listener to take control of who tells it what on its own? "Hey button, tell me whenever an action happens".
public class Foo implements ActionListener {
public attachToButton(JButton button) {
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
// do something useful with e
}
}
It may help to imagine what the button's addActionListener code might look like:
public class JButton { // not the real JButton code, but it will be similar
private List<ActionListener> actionListeners = new ArrayList<ActionListener>();
public void addActionListener(ActionListener a) {
actionListeners.add(a);
}
// called internally when an event happens
private void onEvent(ActionEvent e) {
for(ActionListener listener : actionListeners) {
listener.actionPerformed(e);
}
}
}
So, if you're the listener that called addActionListener(this), then that List contains a reference that points right back at you, which the button uses to tap you on the shoulder every time an action occurs.
Related
Can you make a button in a jframe generate its Events>Action>actionPerformed code into another class besides the jframe class. If you cant, can you manually make the code in the different class to make the button do something.
Yes, you can.
Basically, you use myJbutton.setActionListener(new MyButtonActionListener()).
And you have a class MyButtonActionListener implements ActionListener. The method actionPerformed will be called automatically.
EDIT: Added code below
public class MyButtonActionListener implements ActionListener {
#Override
public void actionPerformed(EventListener e){
// Do stuff
}
}
And you setup your button:
JButton myButton = new JButton("mybutton");
myButton.setActionListener(new MyButtonActionListener());
Im having problem with this code , it doesn't compile . Could you help me ?
I need to close the JFrame when i click the button
public class SlotMachine extends JFrame
{
/*
*
*/
JButton btnExit = new JButton("Exit");
btnExit.addMouseListener(new MouseAdapter()
{
#Override
public void mouseClicked(MouseEvent arg0)
{
this.dispose();
}
});
}
The error is = The method dispose() is undefined for the type new MouseAdapter(){}
I dont know how to get the SlotMachine object from the mouseClicked method
You're calling this.dispose(); the key here being that this refers to the inner class, the MouseListener, and MouseListener doesn't have a dispose() method.
Solution: get rid of this, and it should work, since the compiler will then look into the outer class if the inner class does not contain the method. Alternatively you could specify which this you mean: SlotMachine.this.dispose(); will tell the compiler to call the method of the outer SlotMachine class.
Use an ActionListener on a JButton for several reasons:
Default behavior of buttons is to be activated by spacebar press if the button has focus. This will not work for a MouseListener.
Also an expected behavior is that if the button is disabled via setEnabled(false), then pressing it should not cause the action to be fired. This does not work with MouseListener but does with ActionListener.
You can easily share the ActionListener (or better yet, an AbstractAction) with other components including JMenuItems.
private JButton jBtnDrawCircle = new JButton("Circle");
private JButton jBtnDrawSquare = new JButton("Square");
private JButton jBtnDrawTriangle = new JButton("Triangle");
private JButton jBtnSelection = new JButton("Selection");
How do I add action listeners to these buttons, so that from a main method I can call actionperformed on them, so when they are clicked I can call them in my program?
Two ways:
1. Implement ActionListener in your class, then use jBtnSelection.addActionListener(this); Later, you'll have to define a menthod, public void actionPerformed(ActionEvent e). However, doing this for multiple buttons can be confusing, because the actionPerformed method will have to check the source of each event (e.getSource()) to see which button it came from.
2. Use anonymous inner classes:
jBtnSelection.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
selectionButtonPressed();
}
} );
Later, you'll have to define selectionButtonPressed().
This works better when you have multiple buttons, because your calls to individual methods for handling the actions are right next to the definition of the button.
2, Updated. Since Java 8 introduced lambda expressions, you can say essentially the same thing as #2 but use fewer characters:
jBtnSelection.addActionListener(e -> selectionButtonPressed());
In this case, e is the ActionEvent. This works because the ActionListener interface has only one method, actionPerformed(ActionEvent e).
The second method also allows you to call the selectionButtonPressed method directly. In this case, you could call selectionButtonPressed() if some other action happens, too - like, when a timer goes off or something (but in this case, your method would be named something different, maybe selectionChanged()).
Your best bet is to review the Java Swing tutorials, specifically the tutorial on Buttons.
The short code snippet is:
jBtnDrawCircle.addActionListener( /*class that implements ActionListener*/ );
I don't know if this works but I made the variable names
public abstract class beep implements ActionListener {
public static void main(String[] args) {
JFrame f = new JFrame("beeper");
JButton button = new JButton("Beep me");
f.setVisible(true);
f.setSize(300, 200);
f.add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Insert code here
}
});
}
}
To add an action listener, you just call addActionListener from Abstract Button.
I have been working with java buttons, and I have created a button ,but when i click the button, I want the shape of the object to change. This is the code I've worked on
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class shapes {
public static void main(String[] a) {
JFrame f = new JFrame("Change shapes");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton b = new JButton("Shapes Change");
f.getContentPane().add(b);
f.pack();
f.setVisible(true);
}
Public void paint (Graphics g)
{
//no clue what to do here
}
private static abstract class MyButton extends JButton implements ActionListener {
MyButton()
{
addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b)
{
//no clue what to do here
}
}
}
}
At first, There is a shape created, once the button is clicked it I want to change it to another shape.
There really should be any need to subclass JButton. If you want to customise the button, you could use the Action API instead, see How to Use Actions.
To perform custom painting your should extend a Swing component like JComponent or JPanel and override the paintComponent method...
See Performing Custom Painting for more details.
You would then need to provide some method which you could call to tell the component that the shape should change to how the shape should be changed.
You would then provide a means for your buttons ActionListener to reference the instance of the paint panel and call these methods...
You simply add an ActionListener to the button:
b.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// do some action
}
});
A couple other things to note:
You should not run Swing methods outside the Event Dispatch Thread (EDT) or you may run into unpredictable errors.
Java naming conventions specify that class names should be capitalized. In your code, you named the class "shapes" but it is more proper to name it "Shapes".
First declare and define methods for drawing objects. For example drawSquare (), drawCircle () which draws required shapes. Define radio button or something similar to that to get the users choice (to know which object has to be drawn) . In actionPerformed () check which radiobutton is selected and call the appropriate method for drawing objects and call the repaint () for updating in user interface
I have a main class in a program that launches another class that handles all the GUI stuff.
In the GUI, i have a button that i need to attach an ActionListener to.
The only problem is, the code to be executed needs to reside within the main class.
How can i get the ActionPerformed() method to execute in the main class when a button is clicked elsewhere?
Make your controller ("main" class) implement the ActionListener interface, then pass a reference to the view class:
public class View extends JFrame {
public View(final ActionListener listener) {
JButton button = new JButton("click me");
button.addActionListener(listener);
button.setActionCommand("do_stuff");
getContentPane().add(button);
pack();
setVisible(true);
}
}
public class Control implements ActionListener {
public Control() {
new View(this);
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("do_stuff")) {
// respond to button click
}
}
}
It can also be done with Actions, but that's more useful where you want one piece of code to respond to many buttons.
Implement an anonymous inner class as ActionListener on the button, then call the method on your main class. This creates less dependencies and avoids the tag & switch style programming that implementing the ActionListener interface on a main class tends to promote.
In either case it will create a cycle in your dependency graph: the main class will know about the button and the button will need to call the main class. This might not be a good idea since it will make it hard to compose things in any other way. But without further information it is hard to judge the situation or recommend anything concrete.
Implement ActionListener in your main class and add the main class instance as a listener on your GUI button.