Results 1 to 9 of 9
  1. #1
    hitmen is offline Member
    Join Date
    Oct 2008
    Posts
    58
    Rep Power
    0

    Default Implementing ActionListener for "nameless " button

    I have declared a nameless button. How am I supposed to add an actionlistener to the button which has no name. I only attached an ImageIcon to it.

    Any ideas?

    e.getSource()= ??
    e.getActionCommand() == ???

  2. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    nameless button? You mean a button with no text on it? A nameless button... if you just use (new Button()), how would you want to change the 'settings' of that button?

    Explain yourself.
    I die a little on the inside...
    Every time I get shot.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    If you construct a JButton without retaining a reference to it, you have no way of adding an ActionListener, so the question is moot.

    Of course, that's assuming that's what you mean by "nameless".

    To get better help sooner, post a SSCCE* that demonstrates your problem.
    * SSCCE : Java Glossary

    Also read How To Ask Questions The Smart Way

    db

  4. #4
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Java Code:
    JButton b=new JButton();
    b.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
             //add you preferred action here
         }
    });

  5. #5
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    ... nameless button ...
    Not local serjant. Of course, if you replace 'b' with '(new JButton())' it'll work fine, but what would be the use of that? You can't set the position and such.
    I die a little on the inside...
    Every time I get shot.

  6. #6
    hitmen is offline Member
    Join Date
    Oct 2008
    Posts
    58
    Rep Power
    0

    Default

    Let me explain lah
    My friend used the above code

    JButton [] jb = new JButton[2];
    jb[0] = new JButton("Start");
    jb[0] = new JButton("Stop");
    then
    if (e.getActionCommand() == "Start ") ...

    The problem is that I am supposed to design the buttons for him. However, if I code this way jb[0] = new JButton(null, image);
    then what do I put for
    e.getActionCommand() == ?????
    e.getSource() == ??????:mad:

  7. #7
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Java Code:
    JButton b=new JButton(null, image);
    b.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e){
             //add you preferred action here
         }
    });

  8. #8
    hitmen is offline Member
    Join Date
    Oct 2008
    Posts
    58
    Rep Power
    0

    Default

    No!
    You dont understand.

    e.getSource() == jb[0] is an error
    e.getActionCommand() == jb[0] is also an error.

    What should I do?

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    What should I do?
    For one start explaining things a whole lot better than you're doing. I recommend you start from the beginning and this time provide all the detail necessary because you're currently you're making us try to guess what you're not telling us, so of course you'll not get good answers.

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 05:49 AM
  2. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 04:03 AM
  3. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  4. Replies: 10
    Last Post: 12-30-2008, 04:46 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •