JButton change default border
Asked Answered
K

2

5

I'm using the SystemLookAndFeel which makes the default border around my buttons.

Now I would like a ticker black border, but when I set the border it just adds my new border around the default one, so I have two.

How can I change or remove the border without removing the LookAndFeel?

Also: I am using java 7 and Win 8.1

Koger answered 27/11, 2015 at 9:53 Comment(11)
You'd probably have to tweak the LAF.Cartouche
JButton#setBorderPaintedDorrie
@Cartouche Then the uimanager.put() method would do the job?Koger
@Dorrie wouldn't that just remove all borders?Koger
It will (or should) remove the button "border" which is painted by the look and feel, but the border applied through setBorder will continue to be paintedDorrie
@Dorrie so I tried button.setBorderPainted(false); button.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK)); It just removes my own border and leaves the default. Or am I using it wrong? btw I am using Windows 8.1 so maybe the os doesn't support it...Koger
What's really funny, if I just use setBorder on MacOS, it works, removes the look and feel and paints the border :PDorrie
Maybe try JButton#setContentAreaFilledDorrie
@Dorrie nope, doesn't work either. Trying my luck atm at tweaking the lafKoger
Some L&F don't set any border but paint it as a part of background image. In this case you have a bad luck and must imlement your own Button UI.Thelen
@SergiyMedvynskyy okay, thanks for the lead ;)Koger
D
8

Work with Java 8 on Windows 10, I did this little test

Lots of buttons

As you can see, about the only method I can get to work is using setContentAreaFilled

The general problem is, many look and feels don't use the border property, but instead paint their own borders independently (hence the reason for setBorderPainted), but the look and feel for Windows 10 just wants to be different

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class ButtonTest {

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

    public ButtonTest() {
        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 class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.fill = GridBagConstraints.HORIZONTAL;

            JButton btn = new JButton("Normal");
            add(btn, gbc);

            btn = new JButton("With border");
            btn.setBorder(new LineBorder(Color.BLACK));
            add(btn, gbc);

            btn = new JButton("borderPainted false");
            btn.setBorderPainted(false);
            btn.setBorder(new LineBorder(Color.BLACK));
            add(btn, gbc);

            btn = new JButton("contentArea false");
            btn.setContentAreaFilled(false);
            btn.setBorder(new LineBorder(Color.BLACK));
            add(btn, gbc);

            btn = new JButton("focusPained false");
            btn.setContentAreaFilled(false);
            btn.setFocusPainted(false);
            btn.setBorder(new LineBorder(Color.BLACK));
            add(btn, gbc);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

}
Dorrie answered 27/11, 2015 at 23:0 Comment(1)
That part with setContentArea(false) works like it should, thank you! I'm using Java 7 and WIndows 8.1 but it worked nonetheless. Problem was I wasn't using setContentAreaFalse right: I set it false then setBorder then set it back true which was kinda stupid... Accepted your answer, thanks again ;)Koger
R
3

Try this program it has all types of borders you can have in a Jbutton

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

    public class jbuttonBoders extends JFrame {

        private JButton button[];

        private JPanel panel;

        public jbuttonBoders() {

            super("JButton Border");

            setSize(220,190);

            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            button = new JButton[12];

            panel = new JPanel();

            panel.setLayout(new FlowLayout(FlowLayout.CENTER));

            //Constructing all 12 JButtons using "for loop"

            for(int count=0; count<button.length; count++) {

                button[count] = new JButton("Button "+(count+1));

                panel.add(button[count]);

            }



            //Setting Different Borders on each JButton

            button[0].setBorder(BorderFactory.createLineBorder(Color.blue)); // Simple Line Border

            button[1].setBorder(BorderFactory.createLineBorder(Color.red, 5)); // Line Border + Thickness of the Border

            button[2].setBorder(BorderFactory.createBevelBorder(1)); // Inner Bevel Border

            button[3].setBorder(BorderFactory.createBevelBorder(0)); // Outer Bevel Border

            button[4].setBorder(BorderFactory.createBevelBorder(1, Color.red, Color.blue)); // Two Colors Inner Bevel

            button[5].setBorder(BorderFactory.createBevelBorder(0, Color.green, Color.orange)); // Two Colors Outer Bevel

            button[6].setBorder(BorderFactory.createBevelBorder(1, Color.green, Color.orange, Color.red, Color.blue)); //Four Colors Inner Bevel

            button[7].setBorder(BorderFactory.createBevelBorder(0, Color.green, Color.orange, Color.red, Color.blue)); //Four Colors Outer Bevel

            button[8].setBorder(BorderFactory.createEmptyBorder(5,10,5,50)); // Empty Border (Upper Space, Left Space, Bottom Space, Right Space)

            button[9].setBorder(BorderFactory.createEtchedBorder(0)); //Raised Border Line

            button[10].setBorder(BorderFactory.createEtchedBorder(1)); //

            button[11].setBorder(BorderFactory.createTitledBorder("My Titled Border")); // Titled Border



            /** The Borders shown above are the basic borders that we commonly used.

             *  There are still lots of Border Styles available so all you have to do is to discover

             *  and have some experiment using all the available borders. I recommend you use JCreator Pro

             *  if want to know more about different border styles and learn how to implement them.

             */



            //Setting up the container ready for the components to be added.

            Container pane = getContentPane();

            setContentPane(pane);



            //Adding the JPanel to the container

            pane.add(panel);



            /**Set all the Components Visible.

             * If it is set to "false", the components in the container will not be visible.

             */

            setVisible(true);

        }



        //Main Method

        public static void main (String[] args) {

            jbuttonBoders jbb = new jbuttonBoders();

        }

    }


    Important Part of the Program: 

    //Setting Different Borders on each JButton

            button[0].setBorder(BorderFactory.createLineBorder(Color.blue)); // Simple Line Border

            button[1].setBorder(BorderFactory.createLineBorder(Color.red, 5)); // Line Border + Thickness of the Border

            button[2].setBorder(BorderFactory.createBevelBorder(1)); // Inner Bevel Border

            button[3].setBorder(BorderFactory.createBevelBorder(0)); // Outer Bevel Border

            button[4].setBorder(BorderFactory.createBevelBorder(1, Color.red, Color.blue)); // Two Colors Inner Bevel

            button[5].setBorder(BorderFactory.createBevelBorder(0, Color.green, Color.orange)); // Two Colors Outer Bevel

            button[6].setBorder(BorderFactory.createBevelBorder(1, Color.green, Color.orange, Color.red, Color.blue)); //Four Colors Inner Bevel

            button[7].setBorder(BorderFactory.createBevelBorder(0, Color.green, Color.orange, Color.red, Color.blue)); //Four Colors Outer Bevel

            button[8].setBorder(BorderFactory.createEmptyBorder(5,10,5,50)); // Empty Border (Upper Space, Left Space, Bottom Space, Right Space)

            button[9].setBorder(BorderFactory.createEtchedBorder(0)); //Raised Border Line

            button[10].setBorder(BorderFactory.createEtchedBorder(1)); //

            button[11].setBorder(BorderFactory.createTitledBorder("My Titled Border")); // Titled Border

Output:

enter image description here

Radburn answered 27/11, 2015 at 10:51 Comment(2)
No, unfortunately as i stated in the comments above it just removes my own border and leaves the defaultKoger
Yes that works normally but when you put in a (system)LAF it also uses the default border additionalyKoger

© 2022 - 2024 — McMap. All rights reserved.