How to change JButton background colour while it is pressed? - java

I am trying to override the default background colour of the JButton while it is pressed; the background colour must stay as long as the button is still pressed.
I tried this code:
button.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent evt) {
button.setBackground(Color.BLUE); // applied after release!!
System.out.println("pressed"); // applied on pressed - OK.
}
});}
mousePressed here doesn't achieve my requirement!!
This line is never invoked: button.setBackground(Color.BLUE);. But this line is invoked: System.out.println("pressed");.
However this line button.setBackground(Color.BLUE); is invoked after releasing the button - Not while pressed!!!
How to achieve my requirement?

Before your mouse-adapter part of code, make a Color object as:
final Color col = button.getBackground();
Then, in your mouse adapter, add this method:
public void mouseReleased(MouseEvent e)
{
button.setBackground(col);
}
This makes your total code to :
final Color col = b.getBackground();
button.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e)
{
b.setBackground(Color.BLUE);
}
public void mouseReleased(MouseEvent e)
{
b.setBackground(g);
}
});
So, when you press, the color changes to blue, and then when you release the mouse, it changes back to original. This works fine on my computer.

Related

How to change a JButton color when I hover over it but change it permanently to something else even if I hover afterwards when I click it?

I want to have a JButton on which when I hover over it, it should turn green and when the mouse exits it should go back to default but when I click on it should turn yellow and stay yellow whether or not I hover over it. Thanks.
I have already tried the mouselistener method.
public void mouseEntered(MouseEvent evt) {
bakery.setBackground(Color.GREEN);
}
public void mouseExited(MouseEvent evt){
bakery.setBackground(UIManager.getColor("control"));
}
public void mousePressed(MouseEvent evt){
bakery.setBackground(Color.YELLOW);
}
});
I expected that once I click it should stay yellow but it seems so when I exit the button area it goes back to default and when I hover again it gets green again. Which makes sense according to the mouselistener but I have no idea how to get the result I actually desire.
Sounds like you want the button to stay yellow, until clicked again?
Try this:
public void mouseEntered(MouseEvent e) {
if (bakery.getBackground() != Color.YELLOW) { // skip if already yellow
bakery.setBackground(Color.GREEN);
}
}
public void mouseExited(MouseEvent e) {
if (bakery.getBackground() != Color.YELLOW) { // skip if already yellow
bakery.setBackground(UIManager.getColor("control"));
}
}
public void mousePressed(MouseEvent e) {
if (bakery.getBackground() != Color.YELLOW) {
// The first click will set yellow
bakery.setBackground(Color.YELLOW);
} else {
// A second click clears the yellow.
bakery.setBackground(UIManager.getColor("control"));
}
}

How to assign a method to a button when pressed or released with ActionListener in Java?

I am new to coding GUIs in Java and I am trying to just print a message on the terminal when a button is pressed and another one when it is released.
This is what I have for a regular button pressing.
leftButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
System.out.println("Pressed");
}
});
I did this with the help of IntelliJ IDEA. I want to make the button send a message when pressed and a different thing when released.
You can just add a simple MouseAdapter, like this:
MouseAdapter ma = new MouseAdapter() {
public void mousePressed(MouseEvent e) {
System.out.println("Pressed");
}
public void mouseReleased(MouseEvent e) {
System.out.println("Released");
}
};
leftButton.addMouseListener(ma);
frame.add(button);
This will detect when it is the mouse is pressed on the button or released on the button.
If you want, you can also add a mouseClicked() method, mouseExited(), mouseEntered(), mouseMoved(), and (many) more methods in your MouseAdapter. Check out this JavaDoc
Use custom class and use it
leftButton.getModel().addChangeListener(new BtnCusttomListener());
private class BtnCusttomListener implements ChangeListener {
private boolean pressed = false; // holds the last pressed state of the button
#Override
public void stateChanged(ChangeEvent e) {
ButtonModel Buttonmodel = (ButtonModel) e.getSource();
// if the current state differs from the previous state
if (model.isPressed() != pressed) {
String text = "Button pressed: " + model.isPressed() + "\n";
textArea.append(text);
pressed = model.isPressed();
}
}
}
You can use a MouseListener instead:
leftButton.addMouseListener(new MouseListener() {
#Override
public void mouseClicked(MouseEvent e) {
// The mouse button was pressed and released
}
#Override
public void mousePressed(MouseEvent e) {
// The mouse button was pressed
}
#Override
public void mouseReleased(MouseEvent e) {
// The mouse button was released
}
#Override
public void mouseEntered(MouseEvent e) {
// The cursor entered the bounds of the button (i.e. hovering)
}
#Override
public void mouseExited(MouseEvent e) {
// The cursor exited the bounds of the button
}
});

how to change JLabel color

i have problem to change JLabel color. i am using three JLabel variables . i am putting mouse event on this JLabel variables. i run the and both are changes color when i am entring mouse on JLabels. i whis is that, at the time one JLabel change the color when I am entering the mouse on JLabel variable.
please solve this problem.
entry.addMouseListener(this);
entry.setOpaque(true);
profile.addMouseListener(this);
profile.setOpaque(true);
public void mouseClicked(MouseEvent mc)
{
}
public void mouseEntered(MouseEvent me)
{
entry.setBackground(color);
profile.setBackground(color);
}
public void mouseExited(MouseEvent me)
{
entry.setBackground(Color.white);
profile.setBackground(Color.white);
}
public void mousePressed(MouseEvent mp)
{
}
public void mouseReleased(MouseEvent mr)
{
}
Not entirely sure what you are asking... I assume that your problem is that you have two labels and when you enter the mouse into one of them you want just that label to have a red background, not both.
To do so, you can get the label that triggered the mouse event using e.getComponent() and then set the background for that label only. Also, you might want to use setBackground(null) to reset the background color, since the background of the underlying frame might not always be white. Finally, you can use the MouseAdapter class instead of the MouseListener, providing defaults (no-op) for all those other method you do not need.
MouseListener ma = new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
e.getComponent().setBackground(Color.RED);
}
public void mouseExited(MouseEvent e) {
e.getComponent().setBackground(null);
}
};
Your issue is the method setBackground(), change on setForeground():
entry.addMouseListener(this);
entry.setOpaque(true);
profile.addMouseListener(this);
profile.setOpaque(true);
public void mouseClicked(MouseEvent mc)
{}
public void mouseEntered(MouseEvent me)
{
entry.setForeground(Color.red);
profile.setForeground(Color.red);
}
public void mouseExited(MouseEvent me)
{
entry.setForeground(Color.white);
profile.setForeground(Color.white);
}
public void mousePressed(MouseEvent mp)
{}
public void mouseReleased(MouseEvent mr)
{}

Java - MousePress Hold and Release event listener

hello is there a way mouse even that can Hold the mouse and release cause I can't find it on google.
so for example this image..
When the jTextBox is **** when he click the button, he see the words oops...
then after he release the click of mouse the jTextBox will back to **** again
I know this code already but the mouseevent is I only don't know
Yes. You will want to implement the MouseListener interface with a new class and add this new Listener against your button with the following;
button.addMouseListener(new YourMouseListener());
An example custom MouseListener might look like this.
class YourMouseListener implements MouseListener {
#Override
public void mouseClicked(MouseEvent e) {
}
#Override
public void mousePressed(MouseEvent e) {
// Insert code to show password
}
#Override
public void mouseReleased(MouseEvent e) {
// Insert code to hide password again
}
#Override
public void mouseEntered(MouseEvent e) {
}
#Override
public void mouseExited(MouseEvent e) {
}
}
I hope this helps.
You'll need a Robot object. This can do things as follows:
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
The Mousebutton is pressed until you do this:
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
This should do what you want.

Java AWT mouse events

I have an applet that makes use of the AWT event model. It has a boolean that says if the left button is pressed or not. Here is a sample code:
public class Game extends Applet implements MouseListener
{
boolean isLeftButtonPressed;
public void init()
{
addMouseListener(this);
isLeftButtonPressed = false;
}
public void paint(Graphics g)
{
g.drawString("Is Button Pressed: " + isLeftButtonPressed, 20, 20);
}
#Override
public void mouseClicked(MouseEvent e)
{
isLeftButtonPressed = true;
repaint();
}
#Override
public void mouseReleased(MouseEvent e)
{
isLeftButtonPressed = false;
repaint();
}
//Other MouseListener methods not listed but have to be implemented
}
But it seems as if the left button is never released, even after you actually do so. What could be the problem?
The fundamental in this is incorrect,
These are the mouse events,
MousePressed -> a mouse button is pressed
MouseReleased -> a mouse button is released
MouseClicked -> a mouse button is clicked (pressed and released)
So, when you handle the click event that means mouse is clicked and released.
So i think you have to use mousepressed instead of clicked.
MouseEvent
Method mouseClicked will be called after mouseReleased method so value of isLgetButtonPressed will be true. You have to use MouseEvent.getButton() method to check which mouse button is pressed.

Categories