5

I trying to get my JButton to stop its background change when clicked. I have been reading and testing answers from other questions like such but none have helped me. I am very new to Java so specific details would be helpful, here is my code and a demonstration of what is happening. Also, I am running ubuntu if that matters at all.

Note: if I hold my mouse down and hover over it, it will also trigger the background change.

import javax.swing.*;
import java.awt.*;

public class RaisedButton extends JButton{

    private String      text     =     "";
    private String      type     =     "default";


    public RaisedButton(String text, String type) {
        this.text = text;
        this.type = type;
        __init__();
    }

    private void foundations() {
        NotoFont noto = new NotoFont(true);
        this.setText(this.text.toUpperCase());
        this.setRolloverEnabled(false);
        this.setFocusPainted(false);
        this.setBorderPainted(false);
        this.setFont(noto.get());
        this.setPreferredSize(new Dimension(108 + this.text.length() * 4, 37));
    }

    private void default_type() {
        this.foundations();
        this.setBackground(Color.decode("#FFFFFFF"));
        this.setForeground(Color.decode("#000000"));
    }

    private void primary_type() {
        this.foundations();
        this.setBackground(Color.decode("#00BCD4"));
        this.setForeground(Color.decode("#FFFFFF"));
    }

    private void secondary_type() {
        this.foundations();
        this.setBackground(Color.decode("#FF4081"));
        this.setForeground(Color.decode("#FFFFFF"));
    }

    private void disabled_type() {
        this.foundations();
        this.setBackground(Color.decode("#E5E5E5"));
        this.setForeground(Color.decode("#B2A4A4"));
    }

    private void __init__() {
        switch(this.type.toLowerCase()) {
            case "default":
                default_type();
                break;
            case "primary":
                primary_type();
                break;
            case "secondary":
                secondary_type();
                break;
            case "disabled":
                disabled_type();
                break;
        }
    }
}

Demonstration of what's occurring...

Demonstration

Thanks!

tjkso
  • 141
  • 8
  • 1
    Did you try setting a custom [button- model](https://stackoverflow.com/a/22547218/3992939) ? – c0der Aug 26 '17 at 08:42
  • THANKS! I tried it and it seemed to work this time. I must have just done something wrong with my previous attempt. – tjkso Aug 26 '17 at 08:47

3 Answers3

2

Try put this in the RaisedButton constructor:

    setUI(new BasicButtonUI());
Antony Ng
  • 767
  • 8
  • 16
2

Use FixedStateButtonModel by adding setModel(new FixedStateButtonModel()); to foundations() .

c0der
  • 18,467
  • 6
  • 33
  • 65
0

Within you foundations() method, either change

this.setRolloverEnabled(true);

to set it false as:

this.setRolloverEnabled(false);

or remove the assignment to fallback to default value false

Details from the JavaDoc for setRolloverEnabled

Naman
  • 27,789
  • 26
  • 218
  • 353