tag:infinitekind.tenderapp.com,2009-01-14:/discussions/moneydance-development/6327-cannot-change-background-color-of-buttonInfinite Kind: Discussion 2022-01-05T19:30:22Ztag:infinitekind.tenderapp.com,2009-01-14:Comment/492858292021-07-10T23:04:22Z2021-07-10T23:04:22ZCannot change background color of button<div><p>Here is a screenshot of the window. The button labeled 'Show Returns' uses the new MyButton class, the others do not.</p></div>Bob Btag:infinitekind.tenderapp.com,2009-01-14:Comment/492858292021-07-11T05:07:29Z2021-07-11T05:07:29ZCannot change background color of button<div><p>Odd. I do this all this time. No subclassing, extension, or overrides... Example Jython script below (you can paste/run in Moneybot):</p>
<pre>
<code>from java.awt import Color
from javax.swing import JFrame, JButton, JLabel, JPanel
b=JButton("OK")
b.setBackground(Color.ORANGE)
b.setForeground(Color.WHITE)
b.setVisible(True)
b.setBorderPainted(False)
b.setOpaque(True)
p=JPanel()
f=JFrame("Hello")
l=JLabel("More.................")
p.add(b)
p.add(l)
f.add(p)
f.pack()
f.setLocationRelativeTo(None)
f.setVisible(True)</code>
</pre>
<p>I know you're in pure java, but should be no different.....</p></div>Stuart Beesley (Mr Toolbox)tag:infinitekind.tenderapp.com,2009-01-14:Comment/492858292021-07-12T16:13:34Z2021-07-12T16:13:34ZCannot change background color of button<div><p>Thanks. I think the difference might be that I am running as an extension. I'm not the only one with the issue (see link in original post) but the solution there didn't work for me and I'm not sure why.</p></div>Bob Btag:infinitekind.tenderapp.com,2009-01-14:Comment/492858292021-07-22T00:17:42Z2021-07-22T00:17:42ZCannot change background color of button<div><p>Does anyone have an answer for me? I know it's not the end of the world but it's bugging me (mostly because I'm kinda new at this and can't figure it out).</p></div>Bob Btag:infinitekind.tenderapp.com,2009-01-14:Comment/492858292021-07-30T22:09:45Z2021-07-30T22:09:45ZCannot change background color of button<div><p>Hi Bob,</p>
<p>We've added a lot of code recently to try to automatically keep button (and other UI component) colors inline with the currently selected color theme. So you will be working against the system (well, moneydance anyway) to override the colors. However I am surprised that setting the colors in updateUI() doesn't do the trick.</p>
<p>The only thing I can think of is to possibly call .updateUI() again after the button is created. I know that updateUI() is called in the super-class's constructor even before your own constructor is run, but calling it after your own button constructor might help a bit.</p>
<p>To be honest, I have gotten really frustrated dealing with swing's look and feels and color customisations, and I've probably hacked in some color overrides which weren't necessary.</p>
<p>Thanks,<br>
Sean</p>
<p>--<br>
Sean Reilly<br>
Developer, The Infinite Kind<br>
<a href="https://infinitekind.com">https://infinitekind.com</a></p></div>Sean Reillytag:infinitekind.tenderapp.com,2009-01-14:Comment/492858292021-10-06T19:27:12Z2021-10-06T19:28:03ZCannot change background color of button<div><p>I've done more research on this and the issue is with the WindowsLookAndFeel - it does not respond to background color changes for JButton. I found this and it solved the problem:</p>
<p><a href="https://stackoverflow.com/questions/46823562/jbutton-background-color-not-changing-with-getsystemlookandfeel-in-windows">https://stackoverflow.com/questions/46823562/jbutton-background-col...</a></p>
<pre>
<code>//When using WindowsLookAndFeel, the background color of buttons cannot be set.
//This custom button class overcomes that issue.
//FROM:
//https://stackoverflow.com/questions/46823562/jbutton-background-color-not-changing-with-getsystemlookandfeel-in-windows
package com.moneydance.modules.features.utilitiesrb.common.UI;
import javax.swing.*;
import java.awt.*;
public class ColorButton extends JButton {
JLabel label = new JLabel();
private Color bg;
public ColorButton(String text) {
super(text);
label.setHorizontalTextPosition(JLabel.CENTER);
label.setText(text);
label.setOpaque(true);
setLayout(new BorderLayout());
add(label, BorderLayout.CENTER);
}
@Override
public void setBackground(Color bg) {
if(!UIManager.getSystemLookAndFeelClassName().endsWith("WindowsLookAndFeel")) {
super.setBackground(bg);
}
this.bg = bg;
if(label != null) {
label.setBackground(bg);
}
}
@Override
public void setForeground(Color fg) {
super.setForeground(fg);
if(label != null){
label.setForeground(fg);
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Rectangle rectangle = getBounds();
rectangle.x = 3;
rectangle.y = 3;
rectangle.width -= rectangle.x * 2;
rectangle.height -= rectangle.y * 2;
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(bg);
g2d.fill(rectangle);
}
}</code>
</pre>
<pre>
</pre></div>Bob B