button event still works with button disabled - java

private void button_Clicked_download(MouseEvent e) {
button_dl.setEnabled(false);
System.out.println("Button Clicked.");
}
When I click the button the button looks disabled. However the button still executes the code under the MouseEvent and I see "Button Clicked." in the debug console.
How can I make it so if the button is clicked it ignores the code and is indeed disabled?

However the button still executes the code under the MouseEvent and I see "Button Clicked." in the debug console.
This is exactly why you shouldn't use a MouseListener with a JButton but rather an ActionListener. The solution is of course obvious -- to get rid of the MouseListener and instead add an ActionListener to the JButton of interest.

you need to use an ActionListener instead of MouseClickListener.
your button is logically clicked even if it is disabled so click event will execute

There is actually a very simple way of enabling and disabling a button in java which uses a Mouse Listener.
class HoldListen extends MouseAdapter {
#Override
public void mousePressed(MouseEvent e) {
JButton bt = (JButton)e.getSource();
if (!bt.isEnabled()) {
return;
}
// Do code
}
}
I found your question while trying to create something similar and this is how I solved it.
All the MouseListener's methods return void so it works out quite nice. In my situation going back to an ActionListener would have required a lot of extra work while a MouseListener was perfect for the job. Press set a variable which Release undid and another thread used the variable in a ongoing simulation.

Related

How to display Image when button1 is pressed

How can i display and image when button1 is pressed, this is the code im using to check for a button press:
public class clientFrameButtons {
public void frameClient(){........
....
button1.addActionListener(new ActionListener(){ //ActionListener checks for button press
public void actionPerformed(ActionEvent e){
// if button is pressed the following will happen
I've tried implementing an if statement and looked at several sources online but cannot find a answer to fit my needs.
Thanks.
ActionEvent is a logical event those intention is to signal the fact that a button was actioned. So it doesn't provide any information about the user inputs that have lead to the action (it may be a shortcut, a mouse press, your mouse could have been set to left-handed, etc). If you want to care about mouse, then you must catch mouse events, with an appropriate MouseListener.
So your problem can be expressed in two ways:
you want to display an image when a GUI button is actioned then use ActionListener.
you want to make different things depending on the mouse button pressed while over a GUI button, then use MouseListener.

How to check if and which mouse button is pressed in Swing

How can I check if currently any mouse button is pressed and if so, which one it is?
The thing is that I need to use this kind of information in MouseListener.mouseEntered(). I checked MouseEvent but I could not find the method which would help me.
The getButton() method seems to only return value if there has been change in buttons' state.
Is there a way to find this out without manually keeping track of this somehow vie MouseListener.mousePressed()/mouseReleased() methods.
How can I check if currently any mouse button is pressed and if so, which one it is?
Presumably you want to invoke specific code depending on the button pressed so you can do something like:
if (SwingUtilities.isLeftMouseButton(...))
// do something
You could start by looking at How to write a Mouse Listener and the JavaDocs for MouseEvent in particular, the getButton method.
However, there are cross platform considerations that need to taken into consideration, which are overed by SwingUtilities.isLeftMouseButton and equivalent methods...
This will solve your problem
long eventMask = AWTEvent.MOUSE_MOTION_EVENT_MASK + AWTEvent.MOUSE_EVENT_MASK;
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
public void eventDispatched(AWTEvent e) {
System.out.println(e.paramString()+"-"+e.getSource());
}
}, eventMask);
This is a Global Event Listeners.
Get the source and button from AWTEvent and do whatever you want to perform.

Adding close operation to a JButton

I'm having some trouble figuring out how to make a JButton prompt the user to save the contents in a JTextArea before closing the program. So far, I have some code for the close operation for my button, but even that doesn't seem to work... nothing happens when I click it:
public void actionPerformed(ActionEvent event) {
if(event.getSource() == this.Quit)
this.dispose();
....
the rest are more else/if/try statements for other buttons.
I set my class to extend JFrame and implement ActionListener, so my entire program is in one class... probably not a very neat way to code, but I'm finding it easier to stick everything here for now before I distribute some functions into other classes.
Basically, nothing happens, and I don't know how to add the save prompt along with closing it afterwards. Any help would be great!
Is there any way to tie this into a prompt to save before closing?
See Closing an Application.
Note: you can also add the ExitAction to your JButton. Then when you click on the button it will initiate the closing of the window.
Try this:
MyClass.this.setVisible(false);
Also for a hard application exit do:
System.exit(0);
A more easy way to do what you want is this:
class MyClass extends JFrame{
public MyClass(){
JButton myButton;
//... etc
myButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
MyClass.this.setVisible(false);
}
});
}
}

Mouse event handling in Swing

I was wondering, how does a Swing component know where the mouse is and when it is clicked, and how could I use that in my own class, without having to add a new mouse listener every time i want to add an object to a new panel?
EDIT:
Im extending JComponent and I want to get an event method called when the mouse moves
EDIT2:
Got it working now thanks everyone!
Add an actionlistener to your JButton and it will tell you when its been clicked like so:
someButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
//the button was pressed and do your stuff here.
}
}
how does the JButton know where the mouse is and when it clicked
thats what for Listeners are - it listens for a corresponding type of event
just implement ActionListener and register it to its listener by doing this:
jbutton.addActionListener(this);
now when you click the button it will generate an event which will b handled in this part
public void actionPerformed(ActionEvent e){
... // handle event
}
From the comments of the other post, you want to register a MouseListener to you custom component using Component#addMouseListener
You may like to have a read through How to write a mouse listener for more info

Java JButton - MouseMotionListener ( MouseMoved ) MouseOver Effect

I'm trying to do the MouseOver effect like it is known in JavaScript in Java for a JButton. I added a MouseMotionListener and it worked. I did want to set 2 other Buttons visible, if my mouse touches the 1st Button. So that works perfectly.. but I don´t know how to handle if the mouse isn´t over the Button. I want to setVisible the Buttons false after the Mouse left the Button
Heres my code:
mouseover.addMouseMotionListener(new MouseMotionListener() {
public void mouseDragged(MouseEvent arg0) {}
public void mouseMoved(MouseEvent arg0) {
del.setVisible(true);
addone.setVisible(true);
}
mouseover is the Button I want to listen to.
del is another Button wich I want to setVisible
addone also
Sry for my not really awesome english :P
Thank you !
You're looking for a MouseListener, specifically implementing mouseExited.
http://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html
You may want to use MouseAdapter to avoid being forced to implement all of the methods from MouseListener. MouseAdapter is simply a class that implements the mouse listening interfaces.
http://docs.oracle.com/javase/6/docs/api/java/awt/event/MouseAdapter.html
Perhaps you want to check setRolloverIcon(), setRolloverSelectedIcon() methods, instead of using MouseEvent.
Instead of using a MouseMotionListener. Use a MouseListener, this class has two methods called mouseEntered() and mouseExited() these should allow you to make the necessary changes as the mouse comes in and out of the button.
Here is a brief tutorial on MouseListeners

Categories

Resources