Problems with Look and Feel

Mike Bray 's Avatar

Mike Bray

30 Jan, 2021 03:19 PM

I have a pop up window with a toolbar containing buttons. This works fine on Windows 10 and Debian linux, however on Feren Linux some of the buttons have a black background. I have tried setting the background to white but this is ignored. I suspect that the Look and Feel of MD is overriding this. Is this possible?

  1. Support Staff 1 Posted by Sean Reilly on 31 Jan, 2021 09:43 PM

    Sean Reilly's Avatar

    Hi Mike,
    The look and feel is responsible for setting colours in components, so that'd be the place to look. Which theme is in use?

    Thanks,
    Sean

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

  2. 2 Posted by Stuart Beesley ... on 31 Jan, 2021 09:56 PM

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

    .

  3. 3 Posted by Mike Bray on 01 Feb, 2021 06:31 AM

    Mike Bray 's Avatar

    I have saved the L&F, set it to the cross platform one and then reinstated it around the pop-up window. This does the trick.

  4. 4 Posted by Stuart Beesley ... on 01 Feb, 2021 06:34 AM

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

    Would you mind sharing the snippet of code that you used..?

  5. Support Staff 5 Posted by Sean Reilly on 01 Feb, 2021 07:23 AM

    Sean Reilly's Avatar

    I've been doing a lot of work around L&F lately, so it'd be best if you didn't change the L&F. Instead could you override public void updateUI() in the button and use it to set the color in your popup, making sure to call super.updateUI() at the beginning of your override?

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

  6. 6 Posted by Mike Bray on 01 Feb, 2021 08:20 AM

    Mike Bray 's Avatar

    OK, I will give it a go. Just as an aside the enclosed image below the problem we are trying to overcome:
    By changing the L&F I achieved the second image.

    Stuart, in this is my code: The window containing the buttons is MYJRViewer

         LookAndFeel previousLF = UIManager.getLookAndFeel();
            try {
                UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
            } catch (ClassNotFoundException |InstantiationException |IllegalAccessException |UnsupportedLookAndFeelException e) {}
    
            this.viewer = new MyJRViewer(jasperReportsContext, sourceFile, isXMLFile, locale, resBundle);
            this.pnlMain.add(this.viewer, BorderLayout.CENTER);
            try {
                UIManager.setLookAndFeel(previousLF);
            } catch (UnsupportedLookAndFeelException e) {}
        }
    
  7. Support Staff 7 Posted by Sean Reilly on 01 Feb, 2021 08:39 AM

    Sean Reilly's Avatar

    Ok, on most platforms by default we use getSystemLookAndFeelClassName which I think might call through to GTK on linux, if available. I'll change it to use getCrossPlatformLookAndFeelClassName, except in Dark Mode which uses the open source Darcula L&F.

    On the mac you'll want it to use the system look and feel and using the cross platform L&F will make it look really bad.

    So please don't change the L&F at all, and let me know if the updateUI() override isn't working for you.

    Thanks,
    Sean

  8. 8 Posted by Mike Bray on 01 Feb, 2021 08:45 AM

    Mike Bray 's Avatar

    OK, I will let you know what happens

  9. 9 Posted by Mike Bray on 01 Feb, 2021 04:26 PM

    Mike Bray 's Avatar

    Hi Sean, subclassing the JButton and JToggleButton classes and setting the background in the updateUI method seems to have worked, we are getting the same results on Windows 10, Debian, Feren and Mint Linux and Big Sur macOs. I have removed the changes to Look and Feel.

  10. 10 Posted by Stuart Beesley ... on 01 Feb, 2021 04:44 PM

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

    Hi Mike, could you share the code you used to do this please? Thx

  11. 11 Posted by Mike Bray on 03 Feb, 2021 09:25 AM

    Mike Bray 's Avatar

    Hi Stuart there isn't much to see, all I do is subclass the buttons and use them in my code. For JasperReports I have had to create own versions of JasperViewer, JRViewer and JRViewerToolbar so I can change the tool bar to use my subclassed buttons.

    public class MyButton extends JButton {
        public MyButton() {
            super();
        }
        @Override
        public void updateUI() {
            super.updateUI();
            setBackground(Color.white);
    
        }
    }
    public class MyToggleButton extends JToggleButton {
        public MyToggleButton() {
            super();
        }
        @Override
        public void updateUI() {
            super.updateUI();
            setBackground(Color.white);
    
        }
    }
    

    in JRViewerToolbar

     protected MyToggleButton btnActualSize;
        protected MyButton btnFirst;
        protected MyToggleButton btnFitPage;
        protected MyToggleButton btnFitWidth;
        protected MyButton btnLast;
        protected MyButton btnNext;
        protected MyButton btnPrevious;
        protected MyButton btnPrint;
        protected MyButton btnReload;
        protected MyButton btnSave;
        protected MyButton btnZoomIn;
        protected MyButton btnZoomOut;
    
  12. Mike Bray closed this discussion on 03 Feb, 2021 09:26 AM.

  13. Stuart Beesley - JUST A FELLOW USER and Toolbox ‘guy’ re-opened this discussion on 06 Feb, 2021 04:51 PM

  14. 12 Posted by Stuart Beesley ... on 06 Feb, 2021 04:51 PM

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

    FYI - I've just realised that I had a different problem, and also that I've seen and fixed your problem (setting the background) before (in Python) without overriding the button... I had to call theButton.setOpaque(True) before setBackground(color) would work....

  15. System closed this discussion on 08 May, 2021 05:00 PM.

Comments are currently closed for this discussion. You can start a new one.

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