Charlie
Charlie

Reputation: 3374

How to make a border around Jbutton thicker?

I have the following JButton on the GUI interface I'm building.

http://i.imgur.com/MucqvGj.png

I want to make the border around the button more thicker so it will stand out from the background. Is it possible to do this in Java?

Upvotes: 0

Views: 6081

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347332

You could simply use a LineBorder

JButton btn = ...;
btn.setBorder(BorderFactory.createLineBorder(Color.BLACK, 4));

Take a look at How to Use Borders for more details and ideas

Updating the border state based on the model state

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Test {

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

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public static class TestPane extends JPanel {

        protected static final Border NORMAL_BORDER = BorderFactory.createLineBorder(Color.BLACK, 4);
        protected static final Border ROLLOVER_BORDER = BorderFactory.createLineBorder(Color.RED, 4);

        public TestPane() {

            JButton btn = new JButton("Click me!");
            btn.setContentAreaFilled(false);
            btn.setBorder(NORMAL_BORDER);
            btn.getModel().addChangeListener(new ChangeListener() {
                @Override
                public void stateChanged(ChangeEvent e) {
                    if (btn.getModel().isRollover()) {
                        btn.setBorder(ROLLOVER_BORDER);
                    } else {
                        btn.setBorder(NORMAL_BORDER);
                    }
                }
            });

            setLayout(new GridBagLayout());
            add(btn);

        }

    }

}

Upvotes: 5

Razib
Razib

Reputation: 11173

Create a Border first -

Border border = new LineBorder(Color.WHITE, 13);

Then create a JButton and set the Border -

JButton button = new JButton("Button Name");
button.setBorder(border);  

Hope it will Help.
Thanks a lot.

Upvotes: 2

Related Questions