Search code examples
javaswingjbuttonlook-and-feel

Background color of button when using Windows LAF?


I have a Java application which uses the native LAF like so:

    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

This is working great, however, I am trying to make a button have a red background, but ends up like this:

enter image description here

As you can see, I'm setting background and foreground on the button, but what results is not visually pleasing. Is there a way to have the button draw a red background without subclassing JButton?


Solution

  • For anybody who comes along the problem I did, here's the solution I went with:

    I switched to using an image added as a child of the button using ImageIcon:

        BufferedImage stopPicture = null;
        try {
            stopPicture = ImageIO.read(new File("stop.png"));
        } catch (IOException ex) { }
        JLabel picLabel = new JLabel(new ImageIcon( stopPicture ));
        JButton btnStop = new JButton("");
        btnStop.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                SerialTest.getInstance().stopMoving();
            }
        });
        btnStop.add(picLabel);