Does an opaque JButton background in a non-top-level window become opaque?

Before you read, here are some clarifications about the question:

  • SSCCE is for Java 7. You could use sun. * AWTUtilities to adapt it to Java 6, but I don’t care how it works in Java 6.
  • The fault line [...] is new JDialog(someWindow) . Phantom can be fixed in SSCCE by simply changing that line to [...]new JDialog() .

Why aren't top-level windows with a halo displayed?


Expected Behavior: final JDialog d = new JDialog() (see SSCCE) both TL and non-TL windows have translucent background

As you can see, the right window has a translucent background (as expected).

Actual behavior: final JDialog d = new JDialog(f) (see SSCCE) TL window shows translucent background, while non-TL background becomes opaque even after a single repaint

In this case, the right window has an opaque background. Essentially, for some reason, 3-4 peer reviews are required (the easiest way to reproduce the redraw when rolling over) is for the background to become completely opaque.


SSCCE:

 import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.plaf.synth.ColorType; import javax.swing.plaf.synth.Region; import javax.swing.plaf.synth.SynthConstants; import javax.swing.plaf.synth.SynthContext; import javax.swing.plaf.synth.SynthLookAndFeel; import javax.swing.plaf.synth.SynthPainter; import javax.swing.plaf.synth.SynthStyle; import javax.swing.plaf.synth.SynthStyleFactory; public class SynthSSCCE { public static void main(String[] args) throws Exception { final SynthLookAndFeel laf = new SynthLookAndFeel(); UIManager.setLookAndFeel(laf); SynthLookAndFeel.setStyleFactory(new StyleFactory()); SwingUtilities.invokeLater(new Runnable() { @Override public void run() { final JFrame f = new JFrame(); { f.add(new JButton("Works properly")); f.setUndecorated(true); f.setBackground(new Color(0, true)); f.setSize(300, 300); f.setLocation(0, 0); f.setVisible(true); } { final JDialog d = new JDialog(f); final JButton btn = new JButton("WTF?"); // uncomment and notice that this has no effect // btn.setContentAreaFilled(false); d.add(btn); d.setUndecorated(true); d.setBackground(new Color(0, true)); d.setSize(300, 300); d.setLocation(320, 0); d.setVisible(true); } } }); } static class StyleFactory extends SynthStyleFactory { private final SynthStyle style = new Style(); @Override public SynthStyle getStyle(JComponent c, Region id) { return style; } } static class Style extends SynthStyle { private final SynthPainter painter = new Painter(); @Override protected Color getColorForState(SynthContext context, ColorType type) { if (context.getRegion() == Region.BUTTON && type == ColorType.FOREGROUND) return Color.GREEN; return null; } @Override protected Font getFontForState(SynthContext context) { return Font.decode("Monospaced-BOLD-30"); } @Override public SynthPainter getPainter(SynthContext context) { return painter; } @Override public boolean isOpaque(SynthContext context) { return false; } } static class Painter extends SynthPainter { @Override public void paintPanelBackground(SynthContext context, Graphics g, int x, int y, int w, int h) { final Graphics g2 = g.create(); try { g2.setColor(new Color(255, 255, 255, 128)); g2.fillRect(x, y, w, h); } finally { g2.dispose(); } } @Override public void paintButtonBackground(SynthContext context, Graphics g, int x, int y, int w, int h) { final Graphics g2 = g.create(); try { if ((context.getComponentState() & SynthConstants.MOUSE_OVER) == SynthConstants.MOUSE_OVER) g2.setColor(new Color(255, 0, 0, 255)); else g2.setColor(new Color(0xAA, 0xAA, 0xAA, 255)); g2.fillRoundRect(x, y, w, h, w / 2, h / 2); } finally { g2.dispose(); } } } } 

And these are my questions ...

  • What's happening? As in, why does this demonstrate the behavior of a personalized opaque component that forgets to call super?
  • Why does this not happen with TL windows?
  • What is the easiest way to fix this other than using non-TL windows?
+7
java user-interface swing jwindow synth
source share
2 answers

3-4 peer reviews are required for any reason (the easiest way to reproduce the repainting is when rollover) so that the background becomes completely opaque.

Check out Background with transparency , which should give you some idea of ​​the problem.

I have never played with Synth, so I don’t know if the same solution will work.

+1
source share

Why aren't top-level windows with a halo displayed?

According to Oracle (Java tutorials):

Each top-level container has a content panel that, generally speaking, contains (directly or indirectly) the visible components in this top-level container GUI.

http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html

enter image description here

A glass panel is often used to intercept input events passing through a top-level container, and can also be used to dye several components. This does not allow transparency.

Hence how you used

 final Graphics g2 = g.create(); 

If you have javax.swing.JComponent.paintComponent overridden in a method opposite to creating a graphic object, it should soften the transparency with super.g ();

Fix this by creating a separate method above for graphics

0
source share

All Articles