I am doing a calculator app with swing. I have changed the background color of JButton but when clicked its color changes to a blueish color. Can I set a color manually on mouse over and on click. like the windows 10 default calculator?
Try using this:
buttonName.setFocusPainted(false);
This will get rid of the default border when you click on a button.
To make the button do stuff ie change it's background you can implement the MouseListener interface for it, but I think the best thing for you would be to read about event handling in swing :)
Here's a link
You need to first detect mouse motion over the button using an Mouse listener .You can use an implemented version of it called the MouseAdapter
button.addMouseMotionListener(new MouseAdapter()//To
detect mouse motion
{
#override
public void mouseEntered(MouseEvent m)//called when mouse
enters first time into the button
{
button.setBackground(Color.blue);
}
#override
public void mouseClicked(MouseEvent m)//called when mouse is clicked[pressed and then released]
{
button.setBackground(//whatever you want);
}
});
Note for mouse clicks you can just change the color inside your action listener
Related
I want to get mouse event while mouse click is held down but is not moving.
mouseDragged only works while the mouse is moving, but how can I get the event while mouse is not moving.
I'm using java.awt.event.MouseListener
Thank you.
There is a full javadoc for AWT (also for a lot of other api): https://docs.oracle.com/javase/7/docs/api/java/awt/event/MouseListener.html
void mousePressed(MouseEvent e)
Invoked when a mouse button has been pressed on a component.
void mouseReleased(MouseEvent e)
Invoked when a mouse button has been released on a component.
The mouse doesn't detect any events while mouse click is held down and the mouse is in place
So I have a Java Swing program and I want to be able detect mouse clicks.
addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
update(evt); //another method in the program
}
});
The code works if I click on the window's side or places where there isn't an object, but does not work when I click on objects in the JFrame, such as my JTable or my text field.
Please help me how to have the MouseListener work on objects inside the JFrame as well.
When you click on a textfield, the textfield gains focus. That means your frame loses ownership of focus, and since your listener is most likely added to your frame, your listener stops working right when frame isnt in focus. Add your listener to all compoments, or use Key Binding
Right now I have:
panel.getZoomButton().addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ACtionEvent e)
{
zoom();
}
}
This is called every time the zoom button is pressed. How can I change it so that zoom() will be continuously called if the zoom button is being held down?
You will need to use a MouseListener and override the mousePressed() method. There you can use a Timer or something similar to measure the time, the button has been pressed, in order to calculate your zoom.
Perhaps this question helps you with that: Java MouseEvent, check if pressed down
use Swing Action (most scallable abstraction) instead of ActionListener, there you can to set isEnabled, to switch to false value until all events are done
or add Swing Timer for reset Boolean to true value
there is possible to setMultiClickThreshhold(long threshhold), but is aplicable only for MouseEvents, this methods doesn't react to KeyBindings (ENTER and TAB) firing from Keyboard
Hi and thanks in advance!
I've been working on a game project and no I'm looking into making a basic GUI. To put it short this is what I'm trying to achieve:
The primary component is a J(Scroll)panel that houses moving in-game objects. I want the players to be able to hover the mouse over an object and get some kind of information popup that is tied to the location of the object hovered over. Secondly I want the user to be able to click on an object with the left mouse button in order to "select" the object and also to be able click with the right mouse button to open a popup menu next to the object.
You should add a mouse listener to your panel. Like this:
panel.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
System.out.println(me);
}
});
I use a mouse move listener to handle mouse moves on a canvas in SWT.
But I'd like to have two behaviours, one when the mouse is moving into the canvas area and another one when the mouse is quiting the area.
In fact what I really like to do is to change the cursor icon when the mouse is in the area and restore it when it is out. But I can only capture events when the mouse is in the area.
Is there a specific listener to handle the Mouse Out event on a Canvas?
Thank you.
You are looking for a MouseTrackListener, it has methods for entering, exiting and hovering an element. See http://help.eclipse.org/indigo/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/swt/events/MouseTrackListener.html
This is what you're looking for:
public void mouseExited(MouseEvent e) {
saySomething("Mouse exited", e);
}
Follow this link:
Oracle MouseListeners