Skip to content
Advertisement

How to make multiple buttons work individually in Swing?

I am trying to develop an application and I have decided to learn Swing to create the GUI. I have never learnt this before, it seems quite simple but I haven’t gotten my head around ActionListener.

I am trying to make the buttons respond by producing some debug text in the console.

Could someone take a look and point out the obvious.

JavaScript

Thanks in advance

Advertisement

Answer

In Swing “listeners” are based on a simple concept known as the “observer pattern”. On object registered interest in been notified about by another object when something their inserted in happens.

In your case, when the button is “actioned”.

But, before you solve that problem, you need to fix one other.

You’re shadowing your variables…

JavaScript

You’ve declared b, b1, b2, b3 and b4 as properties of the MainMenu, but in the constructor have redeclared them as local variables, whose context is only available within the constructor. This means, that if any other method tries to references the properties, they will find them to be null

Now we’ve spotted that problem, we can correct it and, add the magic sauce to make your code work…

JavaScript

Here, we’re just saying, “button, tell me when the you’re actioned”

Example…

JavaScript

You should also make sure you take the time to read through How to Use Actions and Laying Out Components Within a Container, which is going to save you a lot of hassle in the future.

Expansion …

Like most things, there’s more then one to use a ActionListener.

The “problem” with the approach you’ve started with (and it’s not bad), is that the actionPerformed method is public, so any one can call it. This might not be a bad idea, but it “leaks” implementation detail which might better constrained internally.

Another “issue” is ALL the buttons use the same ActionListener. In generally, that’s not a “bad” thing, but it can quick make your code very complex, difficult to understand and to maintain.

Before addressing those issues, I’ll address the issue of using a object reference to determine what was triggered (as we’ve done above). Again, this is not a “bad” thing, but it begins to limit it’s re-use, as you could have a button, toolbar button and menu button all wanting to do the same thing, wouldn’t it be nice to re-use as much functionality as possible?

The “action command”

A simple way to make a ActionListener more re-usable, is to make use of the actionCommand property. This allows you to specify a String which can be used to identify a given action, but which might be trigged by the user in a number of different ways.

By default, a JButton‘s actionCommand is set to the buttons text, but you can specify your to make it easier.

So, starting with…

JavaScript

And then use something like…

JavaScript

to determine when the action is trigged. The point to this is, to decouple the logic. This implementation is no longer reliant on (coupled to) the instance of b! Sweet!

Anonymous classes

Another approach might to make use of “anonymous classes” (spooky). This is a neat feature which allows you to create a inline (someone will point to me that’s wrong term to use 😝) instance of an interface, for example…

JavaScript

Now, when the actionPerformed method is called, you are guaranteed who actually triggered it! No need to actionCommand or referencing checking.

See Anonymous Classes for more details.

There are a bunch of other ways you can make this work, but I think I might have scared you enough for one question 😉

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement