Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non opaque ToolBar buttons are opaque when not rolled over #191

Closed
Chrriis opened this issue Oct 16, 2020 · 4 comments
Closed

Non opaque ToolBar buttons are opaque when not rolled over #191

Chrriis opened this issue Oct 16, 2020 · 4 comments
Milestone

Comments

@Chrriis
Copy link
Contributor

Chrriis commented Oct 16, 2020

I don't know if this is a bug or a free choice from look and feel implementers. But we rely on a nice aspect of the Windows L&F, which is that a non opaque button in a toolbar is not opaque when the mouse is not over it. This allows the button to blend to custom toolbars.
Here is a test case and an actual use of this capability, both in Windows and in FlatLaf:
ToolBarButtonOpacity

You can also see that the background color is not respected either (but this is not a problem for us).

And here is the source of the test case:
ToolBarButtonOpacityTest.zip

Is there a way to achieve the desired result with toolbar buttons? Do you think this opacity behavior could be implemented in FlatLaf?

@stanio
Copy link

stanio commented Nov 6, 2020

package test.flatlaf;

import java.awt.Color;
import java.awt.FlowLayout;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.UIManager;

import com.formdev.flatlaf.FlatLightLaf;

public class ToolBarButtonOpacityTest {

    public static void main(String[] args) throws Exception {
        UIManager.setLookAndFeel(new FlatLightLaf());
        //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel centerPane = new JPanel(new FlowLayout());
        centerPane.setBackground(Color.DARK_GRAY);

        JToolBar toolBar = new JToolBar();
        toolBar.setBorder(BorderFactory.createEmptyBorder());
        toolBar.setOpaque(false);
        toolBar.setFloatable(false);
        toolBar.setRollover(true);

        JButton nonOpaqueButton = new JButton("Not opaque");
        nonOpaqueButton.setOpaque(false);
        nonOpaqueButton.setBackground(Color.RED);
        nonOpaqueButton.setFocusable(false);
        toolBar.add(nonOpaqueButton);

        JButton opaqueButton = new JButton("Opaque");
        opaqueButton.setFocusable(false);
        opaqueButton.setBackground(Color.RED);
        toolBar.add(opaqueButton);

        centerPane.add(toolBar);
        frame.getContentPane().add(centerPane);
        frame.setSize(400, 300);
        frame.setVisible(true);
    }

}

As a workaround, you could try setting the toolbar background as well:

        toolBar.setBackground(null); // inherit from parent

@stanio
Copy link

stanio commented Nov 26, 2020

I've just found the following https://bugs.java.com/bugdatabase/view_bug.do?bug_id=6687960:

This is a problem the the orginal design of Swing and how it has been confusing for years. The issue is setOpaque(false) has had a side effect in exiting LAFs which is that of hiding the background which is not really what it is ment for. It is ment to say that the component my have transparent parts and swing should paint the parent component behind it. Nimbus is the first Sun LAF to need transparency in components for rounded corners and outer glow focus. This means that in Nimbus most of the standard components are non-opaque by default. So setting them to non-opaque has no effect. JButton is nice that it has a setContentAreaPainted() method that can be used to turn off background painting but unfortunatly there has been no option for this with other Nimbus components such as JTextField. One workaround has been to set the background color to transparent like new Color(0,0,0,0). We have decided for this issue to make a color which is 100% transparent (eg, Alpha == 0) mean that the background will not be painted at all.

So the solution for this bugs requirement is:

textField.setBackground(new Color(0,0,0,0));

So setOpaque(false) on its own doesn't mean don't paint the background. One should also setBackground(new Color(0,0,0,0)) with a fully transparent color. opaque merely hints the parent should be painted as the child may have some non-fully opaque areas.

In this case one wants fully transparent background of the not rolled over button state which appears controlled by FlatLaf, however.

@stanio
Copy link

stanio commented Nov 26, 2020

So, setting the background color of the toolbar to a fully transparent color does the trick for me:

        toolBar.setBackground(new Color(0, 0, 0, 0));

This casuses toolbar buttons to inherit the fully transparent color (when not rolled over).

DevCharly added a commit that referenced this issue Jan 13, 2021
…ackground color. If no background color is set, then the button background is not painted anymore (issue #191)
@DevCharly
Copy link
Collaborator

Have improved toolbar button background painting in commit b49a498.

This changes following:

  1. If the background color of a toolbar button is set, then it is now used. (was ignored before)
  2. Otherwise no button background is painted. (parent background color was used before)

This is the same behavior as in Nimbus, Substance and Windows Laf (with opaque set to false on buttons).

Buttons in FlatLaf are non-opaque by default, which is necessary for round edges and for wide focus-indicator borders (in IntelliJ and Darcula themes).

So, to get the desired behavior in ToolBarButtonOpacityTest, do not set background color on nonOpaqueButton.

@DevCharly DevCharly added this to the 0.47 milestone Jan 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants