• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

actions performed

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
 
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
}

}
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you also make class ButtonTest implement java.awt.event.ActionListener to get it working.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
go for JOptionPane for easy and fancy display !!
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
James Jiang
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes,Rob.It is more reasonable in your way. I learn it.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by James Jiang:
Yes,Rob.It is more reasonable in your way. I learn it.



 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Posts: 63
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
  •  
    reply
      Bookmark Topic Watch Topic
    • New Topic