2

So I'm writing a program in which I would eventually like a JButton to change color when pressed. So far, here is my dilemma:

-I cannot get the JButton I currently have set up to show the background color, despite toggling the true/false of setOpaque() and setContentAreaFilled(). I would like to be able to do this before even attempting to add an ActionListener.

-I would like to use something similar to JToggleButton() to change the color, however I would like to do this with just a background instead of an icon.

I am using a mac, and I'm wondering if it has something to do with the default button settings, but I don't know how to change/override these.

I would post a screenshot, but unfortunately my reputation isn't high enough yet.

Here is an example of what I am getting:

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

public class Example extends JFrame {

  public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        createAndShowGUI();
      }
    });
  }

  public static void createAndShowGUI() {

    JFrame main = new JFrame("Example");
    JPanel content = new JPanel();
    JButton button = new JButton("Press");

    button.setBackground(Color.orange);
    button.setContentAreaFilled(false);
    button.setOpaque(true);

    content.add(button);

    main.setContentPane(content);
    main.setVisible(true);
    main.setSize(40,60);
    main.setLocation(500, 200);
    main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

}

For those (maybe on a windows or linux platform) who can't see what I'm talking about, the button appears and the layout behind the button (not the border or the JPanel, just a small rectangle around the button) is colored to what was specified.

Thank you in advance for your time.

Also, I know that inside the ActionListener the getSource() method will return the text associated with the button. Is there any way to use something similar to change the color of the button from inside the ActionListener or is this idea just a redundancy for something that can be done in a much simpler fashion.

Much appreciated.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
SmithNineSix
  • 69
  • 2
  • 8
  • Tried button.setContentAreaFilled(true); already? – Norbert Apr 21 '15 at 19:26
  • @NorbertvanNobelen Yes I have. Curiously it doesn't appear to have changed anything.. – SmithNineSix Apr 21 '15 at 19:27
  • 1
    Works on windows. Don't have a mac handy, but check this out: http://stackoverflow.com/questions/1065691/how-to-set-the-background-color-of-a-jbutton-on-the-mac-os – copeg Apr 21 '15 at 19:30
  • @copeg Tried these options already. The setBorderPainted(false); appeared to completely remove the border of the button and I couldn't select it anymore. I have encountered issues like this before with apple's default layouts when working with user interfaces. Thanks though – SmithNineSix Apr 21 '15 at 19:38

3 Answers3

4

i think you want play something around with the look and feel. try

button.setUI(new MetalButtonUI());

or try some other background color supported UI for the button.

subash
  • 3,116
  • 3
  • 18
  • 22
2

I would eventually like a JButton to change color when pressed

here are two different ways (note JButton has an arrays of colors in UIManager)

  • override events from ButtonModel (ChangeListener or the same methods are implemented e.g. isPressed, isArmed in JButtons API), accelators are valid for mouse and KeyEvens (selection or focusInWindow)

  • override BasicsButtonUI (for real project)

I am using a mac, and I'm wondering if it has something to do with the default button settings, but I don't know how to change/override these.

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Plus for `ButtonUI`; some alternatives that also read well on Mac OS X are suggested [here](http://stackoverflow.com/a/3420431/230513). – trashgod Apr 21 '15 at 20:38
  • Okay, thanks for the answer. In reading those examples I have concluded that I don't have a thorough enough understanding of swing to be able to tackle this without changing OS's. Its frustrating that this works on Windows but not Mac.. I appreciate your help though :) – SmithNineSix Apr 21 '15 at 21:28
0

I think that button.setOpaque(true); is unnecessary.

Remove it, or change it to: button.setOpaque(false); and see what happened

roeygol
  • 4,908
  • 9
  • 51
  • 88
  • 1
    `setOpaque(true)` is a necessary but not sufficient condition on some L&F implementations; some alternatives are suggested [here](http://stackoverflow.com/a/3420431/230513). – trashgod Apr 21 '15 at 20:36