0

I'm using a transparent (shaped) JFrame under Java 6 with Mac OS X.

My system tells me that Translucency.PERPIXEL_TRANSLUCENT is supported. Although my system tells me that Translucency.PERPIXEL_TRANSPARENT is not supported, I found out it works under Mac OS X when putting pixels with alpha=0.

Now, when selecting text in the JTextField, it also drags the window as if I would drag on the underlying contentpane.

here my sample code to try (contains main-method to run on your computer)

import com.sun.awt.AWTUtilities;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class DialogTest extends JFrame {

    public DialogTest() {
        setBackground(new Color(0, 0, 0, 0));
        JPanel p1 = new JPanel() {
            public void paintComponent(Graphics g1) {
                if (g1 instanceof Graphics2D) {
                    Paint p =
                            new GradientPaint(0.0f, 0.0f, new Color(240,240,240, 0),
                            0.0f, getHeight(), new Color(240,240,240, 255), true);

                    Graphics2D g2d = (Graphics2D) g1;
                    g2d.setPaint(p);
                    Shape s = new Ellipse2D.Double(0,0,getWidth(),getHeight());
                    g2d.fill(s);
                }
            }
        };

        add(p1);
        p1.setOpaque(false);
        p1.add(new JButton("button 1"));
        JTextField tf = new JTextField("Sample Text To Select");
        p1.add(tf);
        p1.add(new JButton("button 2"));

        setUndecorated(true);       
        setSize(400,400);
        AWTUtilities.setWindowOpaque(this, false);
    }

    public static void main(String[] s) {
        // Create the GUI on the event-dispatching thread
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                DialogTest tw = new DialogTest();

                // Display the window.
                tw.setVisible(true);
            }
        });


   }
}
trainrobbery
  • 500
  • 3
  • 14
  • Very important. Because your panel `p1` is transparent, you MUST be calling `super.paintComponent` or you could end up with any number of really bad paint problems... – MadProgrammer Aug 23 '13 at 07:28
  • Also, since, I think Java 1.4 or there abouts, Graphics is guaranteed to be an instance of `Graphics2D`... – MadProgrammer Aug 23 '13 at 07:30
  • Sure you completely right. I wrote it very fast to demonstrate just the problem..:) – trainrobbery Aug 23 '13 at 07:44
  • I don't think they relate to your problem, just feedback – MadProgrammer Aug 23 '13 at 07:45
  • I noticed that Mac OS X doesn't paint the shadow of the window anymore, but by calling getRootPane().putClientProperty("apple.awt.windowShadow.revalidateNow", ""+((new Random()).nextInt())); each time the window is repainted, it works. This could be also an add on to your really great tutorial on how to create transparent windows in the other thread. – trainrobbery Aug 23 '13 at 07:50
  • I'd say that the shadow is part of the decoration of the frame border... – MadProgrammer Aug 23 '13 at 07:54
  • 1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) Don't extend frame or other top level containers. Instead create & use an instance of one. 3) See [this answer](http://stackoverflow.com/a/18278531/418556) for ideas. – Andrew Thompson Aug 23 '13 at 17:52
  • sorry... hope it is a better SSCCE now – trainrobbery Aug 23 '13 at 22:40

0 Answers0