Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / September 2004

Tip: Looking for answers? Try searching our database.

JButton.setEnable(false) doesn't disable the Listener (Linux)

Thread view: 
Theodosis Ekizoglos - 28 Sep 2004 16:28 GMT
I  assign an ActionListener or a MouseListener to a JButton and later
in the programm I disable it (bt.setEnable(false)).

It becomes gray but I can click on the (J)Button.

That happends on Linux

In Windows, when I assign an ActionListener I can't click on the
Button.

Does someone know how to solve this? I expect, when I disabe the
Button then it will not hear the mouse anymore until I enable it
(bt.setEnabled(true))
Andrew Thompson - 28 Sep 2004 17:05 GMT
> I  assign an ActionListener or a MouseListener to a JButton and later
> in the programm I disable it (bt.setEnable(false)).
[quoted text clipped - 7 lines]
>
> Does someone know how to solve this?

Since you did not make the problem clear, it
is not easy to guess what the solution is.

How about you state the following.

The example I have is..
a) <http://www.physci.org/codes/sscce.jsp>
The behaviour I expect/want is..
b) ...
The behaviour I get is
c) ...

>..I expect, when I disabe the
> Button then it will not hear the mouse anymore until I enable it
> (bt.setEnabled(true))

So what does your a) reveal?

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.lensescapes.com/  Images that escape the mundane

Babu Kalakrishnan - 28 Sep 2004 17:20 GMT
> I  assign an ActionListener or a MouseListener to a JButton and later
> in the programm I disable it (bt.setEnable(false)).
[quoted text clipped - 9 lines]
> Button then it will not hear the mouse anymore until I enable it
> (bt.setEnabled(true))

The API documentation of setEnabled(boolean) specifically mentions this:

<Quote>

Note: Disabling a lightweight component does not prevent it from
receiving MouseEvents.

</Quote>

I'm not sure why this distinction has been made, but looks like it was
done on purpose.

You could try overriding the setEnabled(boolean) method and calling
enableEvents / disableEvents methods with the appropriate mask.
(Warning:  I've never tried it myself - so no guarantees of what side
effects it can cause :-))

BK
Andrew Thompson - 28 Sep 2004 20:21 GMT
> <Quote>
> Note: Disabling a lightweight component does not prevent it from
[quoted text clipped - 3 lines]
> I'm not sure why this distinction has been made, but looks like it was
> done on purpose.

Huh!  I initially tried it with a Frame/Button and got
negative results (no events detected).  When I read that
I tried an alternate example.

<sscce>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/** Demonstrates that the mouse event on the disabled button
is delayed by 'doClick' ms even though no action event is fired. */
public class DisabledTest extends JFrame implements ActionListener {

  JButton button;

  static int xLoc = 1, yLoc=1;

  DisabledTest() {
     this(false);
  }

  DisabledTest(boolean enabled) {
     setLayout(new BorderLayout());
     button = new JButton("OK");
     button.addActionListener(this);
     button.addMouseListener( new MouseAdapter() {
        public void mouseEntered(MouseEvent me) {
           System.out.println( "Enter: " + me.getPoint() );
           button.doClick(3000);
        }
        public void mouseExited(MouseEvent me) {
           System.out.println( "Exit: " + me.getPoint() );
        }
     } );
     add(button, BorderLayout.CENTER);
     button.setEnabled(enabled);
     setLocation( xLoc++*50, yLoc++*50 );

     pack();
     setSize(getPreferredSize());
  }

  public void actionPerformed(ActionEvent ae) {
     System.out.println( ae.getActionCommand() );
  }

  public static void main(String args[]) {
     new DisabledTest().setVisible(true);
     new DisabledTest(true).setVisible(true);
  }
}
</sscce>

I find it intriguing that the (exit) MouseEvent is delayed
despite the ActionEvent never firing in the 'disabled'
example.

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.lensescapes.com/  Images that escape the mundane

Theodosis Ekizoglos - 29 Sep 2004 14:54 GMT
Andrew, you proved with your code what I wrote:
jbutton.setEbabled(false) doesn't prevent from mouse events.
I tried also with jbutton.disableEvents( ) and that also
doesn't work.

I overriden the setEnabled() method of JButton class
and now it works. I control  in the body of mousePressed( )function if
the button is enabled and if not, mousePressed does nothing.

I don't know if it is a very profecional way but it works.

thank you for support


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.