Results 1 to 9 of 9
  1. #1
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default ActionListner Problem

    whats wrong with this code ...... it always shows button 1 is selected
    {
    JButton b1 = new JButton("Button 1");
    JButton b2 = new JButton("Button 2");
    JPanel p = new JPanel();

    public Frame() {
    p.add(b1);
    p.add(b2);
    add(p);
    b1.addActionListener(this);
    b2.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {

    if(b1.isEnabled()){
    JOptionPane.showMessageDialog(null, "b1 selected");
    }else{
    if(b2.isEnabled()){
    JOptionPane.showMessageDialog(null, "b2 selected");
    }
    }
    }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default Re: ActionListner Problem

    Being selected and being enabled are two different things. If a button is capable of being selected, it will always be enabled. Hence the second part of the if construct will never get executed.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: ActionListner Problem

    so how can i modify this code ?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default Re: ActionListner Problem

    Check the JButton API (actually AbstractButton). What you want is isSelected(), not isEnabled().

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: ActionListner Problem

    It does not work with isSelected() .... that's why I tried isEnabled() ...

  6. #6
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: ActionListner Problem

    OK jim thanks .. I did it with ...
    {
    public void actionPerformed(ActionEvent e) {

    if(e.getSource() == b1){
    JOptionPane.showMessageDialog(null, "b1 selected");
    }else{
    if(e.getSource() == b2){
    JOptionPane.showMessageDialog(null, "b2 selected");
    }
    }
    }
    }

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: ActionListner Problem

    A JButton can't be really selected, compare it with a doorbell switch; use a JToggleButton instead (compare that one with a light switch). Also read the API documentation for the AbstractButton, JButton and JToggleButton classes.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: ActionListner Problem

    thanks JOSH

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default Re: ActionListner Problem

    Well nuts!! I hate giving bad advice. I checked the API and the source but didn't try an example. I saw no other method which would serve as an indicator. I clearly haven't used buttons in a while.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Button and ActionListner problems
    By reader34 in forum Java Applets
    Replies: 2
    Last Post: 04-28-2014, 11:52 AM
  2. Replies: 0
    Last Post: 11-07-2012, 12:44 PM
  3. Small problem with problem with Java, C++ parse program.
    By dragstang86 in forum New To Java
    Replies: 4
    Last Post: 10-30-2011, 03:43 AM
  4. Replies: 9
    Last Post: 09-21-2010, 04:15 PM
  5. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 06:12 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
  •