Cannot change background color of button

Bob B's Avatar

Bob B

10 Jul, 2021 10:57 PM

I'm trying to change the background color of a button in my extension. Nothing I do will cause it to change from the colors of the buttons in the rest of the MD app. I found the following discussion:

https://infinitekind.tenderapp.com/discussions/moneydance-developme...

which suggested subclassing the button. I tried that exactly as written but I still don't get any change in my button color. I can change the font but not the color.

Any help would be appreciated.

This is my button class:

public class MyButton extends JButton {
    public MyButton() {
        super();
    }
    @Override
    public void updateUI() {
        super.updateUI();
        this.setOpaque(true);
        setBackground(Color.yellow);
   }
  1. 1 Posted by Bob B on 10 Jul, 2021 11:04 PM

    Bob B's Avatar

    Here is a screenshot of the window. The button labeled 'Show Returns' uses the new MyButton class, the others do not.

  2. 2 Posted by Stuart Beesley ... on 11 Jul, 2021 05:07 AM

    Stuart Beesley - JUST A FELLOW USER and Toolbox ‘guy’'s Avatar

    Odd. I do this all this time. No subclassing, extension, or overrides... Example Jython script below (you can paste/run in Moneybot):

    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)
    

    I know you're in pure java, but should be no different.....

  3. 3 Posted by Bob B on 12 Jul, 2021 04:13 PM

    Bob B's Avatar

    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.

  4. 4 Posted by Bob B on 22 Jul, 2021 12:17 AM

    Bob B's Avatar

    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).

  5. Support Staff 5 Posted by Sean Reilly on 30 Jul, 2021 10:09 PM

    Sean Reilly's Avatar

    Hi Bob,

    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.

    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.

    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.

    Thanks,
    Sean

    --
    Sean Reilly
    Developer, The Infinite Kind
    https://infinitekind.com

  6. 6 Posted by Bob B on 06 Oct, 2021 07:27 PM

    Bob B's Avatar

    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:

    https://stackoverflow.com/questions/46823562/jbutton-background-col...

    //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);
        }
    }
    
    
    

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac