Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stopping JButton highlighting on press

Any JButton that is pressed will seem to "highlight" itself when pressed like so:

Pressed button

I can't seem to find any way of disabling this.

like image 965
Fraser Price Avatar asked Mar 20 '14 19:03

Fraser Price


1 Answers

There are a number of ways you might achieve this...

You Could...

Override paintComponent and implement you own paint logic. This is kind of dangerous and now means that for each state change you want to modify will either require a new JButton based class or some other serious of flags to implement. It's also possible that this could effect other look and feels...

You Could...

Create your own ButtonUI, which would normally be the preferred way, but it's not an insignificant amount of work and you'd need one for each platform you wanted to support

You Could...

Use the icon property of the button to "simulate" the button boundaries. This is preferred solution (over customising the the painting process) as it's easy to apply and doesn't require a specialised button to achieve. It also overcomes some of the issues of how buttons are painted across different platforms (as not all buttons use the background color property the same)

You Could...

Define your own ButtonModel which could ignore certain state changes (like pressed or rollover).

This is a preferred solution as it works with the current look and feel to achieve your results.

FixState

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.DefaultButtonModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestButton {

    public static void main(String[] args) {
        new TestButton();
    }

    public TestButton() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            JButton normal = createButton("Normal", Color.RED);
            JButton fixed = createButton("Fixed", Color.BLUE);
            fixed.setModel(new FixedStateButtonModel());
            setLayout(new GridLayout(1, 0));
            add(normal);
            add(fixed);
        }

        protected JButton createButton(String text, Color background) {
            JButton btn = new JButton(text);
            btn.setFocusPainted(false);
            btn.setBackground(background);
            btn.setForeground(Color.WHITE);
            return btn;
        }

    }

    public class FixedStateButtonModel extends DefaultButtonModel    {

        @Override
        public boolean isPressed() {
            return false;
        }

        @Override
        public boolean isRollover() {
            return false;
        }

        @Override
        public void setRollover(boolean b) {
            //NOOP
        }

    }

}
like image 124
MadProgrammer Avatar answered Sep 30 '22 18:09

MadProgrammer