Two Laptop Bag*
The moose likes Swing / AWT / SWT and the fly likes actions performed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "actions performed" Watch "actions performed" New topic
Author

actions performed

sandeep Talari
Ranch Hand

Joined: Dec 24, 2007
Posts: 63
hello, i had a small view like panel,label , checkbox, button etc...
now i want add some action to some button,like if press ?button it should display some message about it .. how i can do that please can anyone give steps.. that i can do
Mycode:
import java.awt.*;
class ButtonTest extends Frame
{
Button b = new Button("Start Game");
Button b2 = new Button("quit Game");

ButtonTest(String s)
{
super(s);
setLayout(new FlowLayout());
add(b);
add(b2);
}
public static void main(String [] args)
{
ButtonTest bt = new ButtonTest("so many buttons");
bt.setSize(500,500);
bt.setVisible(true);
}
}
Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
b.addActionListener(this);
b2.addActionListener(this);

public void actionPerformed(ActionEvent e){
Object source = e.getSource();

if (source == b) {
//Do what ever you want to
//To display messages
String message = "Hi button b is selected!" JOptionPane pane = new JOptionPane(message);
JDialog dialog = pane.createDialog(new JFrame(), "Dilaog");
dialog.show();

}
if (source == b2) {
//Do what ever you want to
}

}
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Originally posted by Gopu Akraju:
b.addActionListener(this);
b2.addActionListener(this);
addActionListener(this)?



I know you will find it in some books and the Java Tutorial, but addActionListener(this) is almost always a breach of the customs of object-oriented programming. You can see from all the if statements that method will become unwieldy and difficult to maintain.

Last time I wrote on this subject appears to be here; follow the link to earlier discussions. Much more detail about Campbell's technique for writing an anonmyous inner class in the old discussions, eg here.

You need something like this:
James Jiang
Greenhorn

Joined: Dec 13, 2007
Posts: 24
you also make class ButtonTest implement java.awt.event.ActionListener to get it working.
wm jaban
Greenhorn

Joined: Apr 30, 2008
Posts: 8
go for JOptionPane for easy and fancy display !!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Originally posted by James Jiang:
you also make class ButtonTest implement java.awt.event.ActionListener to get it working.

That's just what Campbell discourages, and I agree with him.

In fact, I support the use of anonymous listeners just like Campbell has shown. However, I tend to use Action instead of ActionListener, and create my buttons with an Action object instead. That allows me to use just a single Action object for multiple controls, yet control them all (e.g. disable them) by just controlling the Action.

An example of a button created with an Action:

Now besides having both buttons, with mnemonics for Alt+S and Alt+Q respectively, there are menu items as well with the same mnemonics. Calling "a.setEnabled(false)" will disable both the button and the menu item.

Note: this only works with Swing. With AWT, use Campbell's approach (although Action extends ActionListener so you can use them that way too).
[ April 30, 2008: Message edited by: Rob Prime ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
James Jiang
Greenhorn

Joined: Dec 13, 2007
Posts: 24
Yes,Rob.It is more reasonable in your way. I learn it.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Originally posted by James Jiang:
Yes,Rob.It is more reasonable in your way. I learn it.


Christopher Young
Ranch Hand

Joined: Nov 02, 2007
Posts: 63
Hmm. I'll have to try the anonymous listener class sometime.

Looks very clean and easy to maintain.

I think the books just say use obj.addActionListener(this) to make things simpler for the ones who haven't coded for awhile.

The C++ book i have suggests laying dense code out to be less dense to make it more readable. Which I can understand for a novice but to build quicker making code denser is a good thing IMO.

Though I'm not a pro or anything.


Technology can never substitute for knowledge.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
I have a little rule of thumb:
  • Anything likely to be used again in other apps: public class implementing the Listener interface.
  • Anything used several times in the same JFrame: private class.
  • Anything used once: not used in two places and nothing similar used anywhere else: anonymous class.
  • You will see Rob Prime has offered an alternative with Actions, and you could also use an Adapter class.

    Yes, some books use addActionListener(this) because it looks easier and it takes up less paper, just as they put everything in the main() method. Quicker to learn the syntax, but you don't learn maintainable programming.

    And beware of making your code dense; it was fashionable when C was King, but remember somebody else will have to read it in 6 months. Laying code out with lots of whitespace helps no end.
    Christopher Young
    Ranch Hand

    Joined: Nov 02, 2007
    Posts: 63
    would it be useful to put comments in dense pieces so someone picking it up later on can get a grasp of what one line is doing for 4-5 chunks later on down the road?

    Also with the anonymous actionListener, I like it. Except it takes up a lot more bytes than using one big actionListener (But I think with todays computers we can spare this).

    I think as a newbie its more important to understand the syntax than it is to write maintainable systems. That comes later when one does more and more research into the field and flounders on a few things (Hopefully in his basement and not at his work).

    Would an experienced programmer have a hard time understanding dense pieces of code if he knows his way around the language? It's never been much of a problem to me so far, but I haven't looked at other people's code so much and most of what I do so far is call stuff from the language than stuff I've made myself.

    I ask this because it seems to me a programmer shouldn't be too concerned about dense code. But I'm also be wary of saying such things as truth because I'm not too experienced (8 months or so, I don't think I'm doing too bad for myself knowledge wise for the amount of time I've put in) and maybe with bigger projects its an issue (The biggest project I've written is really under 1000 lines, and some of it is bulk I kluged up to keep working and stuff)
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38075
        
      22
    Yes, put comments in so people can understand the code. But . . .. . . is standard code to most experienced programmers, and doesn't need explanation. It does actually contain two comments. A Javadoc comment on the actionPerformed method would probably be useful.
    Look at this snippetHere a comment to explain where the 2 comes from and where the - 1 comes from are probably useful. Something like
    /* Iterate along half the array */ and
    /* If i = 0, last element = array.length - 1 */
    would probably be useful.

    No, you should not consider learning the syntax more important that maintainable code; writing correct syntax is the beginnings of maintainable code. Get your code nice and simple and three benefits will accrue:

  • Easily maintainable code.
  • The compiler will better be able to optimise the code.
  • Better marks when you hand in your assignments.
  •  
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: actions performed
     
    Similar Threads
    firing an event when it has not been fired
    static variable vs final variable!
    Strange BorderLayout !!
    Please Help me...!!!
    Weird System Look and Feel behaviour