I have the following simple code:
btn = new JButton();
btn.setBackground(backgroundColor)
I worked when I used:
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
But it stopped to work after I have commented the above line. Does anybody know why it can happen and how I can set a background color to a button without the usage of an explicit Look and Feel?
ADDED
It seems to me that I need to use getBackground
. But I do not know how.
How to trigger a JButton with a key press? Add an ActionListener to the button. It will fire an event when the button is in focus and the user presses the enter key.
By default, we can create a JButton with a text and also can change the text of a JButton by input some text in the text field and click on the button, it will call the actionPerformed() method of ActionListener interface and set an updated text in a button by calling setText(textField.
To add icon to a button, use the Icon class, which will allow you to add an image to the button. Icon icon = new ImageIcon("E:\editicon. PNG"); JButton button7 = new JButton(icon); Above, we have set icon for button 7.
it is necessary to set Opaque of the element to true for color to be filled
btn = new JButton();
btn.setOpaque(true);
btn.setBackground(backgroundColor);
From setBackground() javadoc:
It is up to the look and feel to honor this property, some may choose to ignore it.
Maybe your LAF just ignored it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With