Results 1 to 13 of 13
  1. #1
    majkel is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

    Default transparent jframe?

    Hello,

    how to get transparent frame, i cannot find the way how to make transparent frame, maybe someone have the solution for that and can share it

    few things:
    netbeans6.5m + swing + frames
    i'm new to java, i used to programming in c++builder (vcl) and little in M$ VS

    thank you in advance,
    michael

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

  3. #3
    majkel is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by mtyoung View Post
    how "transparent" do you want?
    i'm looking for frame that I can see through, where i only have the borders of this frame and 100% transparent background - I can click and other things, like in borland c++ builder, there you have Transparency (0-255) and if you set 255 for your form, form is invisible, only visual components are visible.

    of course if you will print a code with transparency on, on some level of transparency i think i will find the way to change this level for my own ;), i'm programmer for last few years so i think this won't be a problem, problem is to just ON transparency

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Try setUndecorated(true); getContentPane.setOpaque(false);

  5. #5
    majkel is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Steve11235 View Post
    Try setUndecorated(true); getContentPane.setOpaque(false);
    first one - setUndercorated(true) - if true - the borders of frame are invisible, if false nothing change

    second one - getContentPane(). and there's no function like setOpaque or any like that, i've tried to set opaque to false in every element by myself and nothing happened

    btw thanks for helping, i'm still looking for solution, now i'm on the level: click chaotically, maybe you will have unbelievable luck ;d

    next one is: crying, begging compilator and pray(edited by me(author) coz mistake) to god, (btw in polish we have something very funny: God = Bóg, ó and u - two form of 'u', so somethimes we pray to Bóg, but for me its still praying to Bug ;d)
    Last edited by majkel; 01-24-2009 at 12:43 AM. Reason: mistake

  6. #6
    playwin2 is offline Member
    Join Date
    Aug 2008
    Posts
    58
    Rep Power
    0

    Default

    Native or not-native?

    In case of not-native then the following article might useful.
    (I haven't tried it )

    Hacking Swing: Translucent Windows | O'Reilly Media

  7. #7
    majkel is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by playwin2 View Post
    Native or not-native?

    In case of not-native then the following article might useful.
    (I haven't tried it )

    link
    thanks a lot for that!! i've implemented it, i think i will change it to my problem and solve it ;d, thanks very much, this is what i was looking for, maybe it's not transparent but its very very usefull ;d

    one more time thanks a lot "playwin2" ;d

    ps: i think the thread could be closed, the solution given by "playwin2" is solving the problem!!

  8. #8
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Sorry about the setOpaque(), that doesn't apply to Container.

    The old screen shot trick, eh? The easiest one is to make a screen shot of the entire desktop, save the image, and set the image as the background. Copy all the icons to a folder. Suddenly, the "desktop" icons don't work!

    By the way, "pray" to God, "prey" on victims. I liked the Bug part.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    Hmm... prey on bugs? Insectivorous? ;)

  10. #10
    lichmanm is offline Member
    Join Date
    Mar 2009
    Posts
    1
    Rep Power
    0

    Default

    Hi majkel,

    well, I did as you were told to do, creating a screenshot and using it as a background.

    The problem is that every time you change something on your screen the JFrame has to refresh resulting in hiding the window several times in order to take the new snapshots. I can easily say that I'm not satisfied with this. it is seemed as you have found a way to solve that problem... I will be forever in your debt if you'll tell me how...

  11. #11
    majkel is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

    Default answer

    Quote Originally Posted by lichmanm View Post
    Hi majkel,

    well, I did as you were told to do, creating a screenshot and using it as a background.

    The problem is that every time you change something on your screen the JFrame has to refresh resulting in hiding the window several times in order to take the new snapshots. I can easily say that I'm not satisfied with this. it is seemed as you have found a way to solve that problem... I will be forever in your debt if you'll tell me how...
    hi,

    so I must tell you that this solution isn't very clever, I just copied it from the link above, editing it a little and that's all

    it doesn't work on another screen - it works on the screen where it's window appeared, if you'll move window to other screen it will be white (maybe someone will repair it ;d), when you move window you will see the screen like water - this is super extra fast java ;p, for me this was enough, i had to show my employer that i can do that in case that they need it

    so the code =>
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package starter;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
    import java.util.Date;
    
    public class TransparentBackground extends JComponent
            implements ComponentListener, WindowFocusListener,
            Runnable {
        private static final long serialVersionUID = 1L;
        private JFrame frame;
        private Image background;
        private long lastupdate = 0;
        public boolean refreshRequested = true;
        public TransparentBackground(JFrame frame) {
            this.frame = frame;
            updateBackground( );
            frame.addComponentListener(this);
            frame.addWindowFocusListener(this);
            new Thread(this).start( );
        }
        public void componentShown(ComponentEvent evt) { repaint( ); }
        public void componentResized(ComponentEvent evt) { repaint( ); }
        public void componentMoved(ComponentEvent evt) { repaint( ); }
        public void componentHidden(ComponentEvent evt) { }
    
        public void windowGainedFocus(WindowEvent evt) { refresh( ); }
        public void windowLostFocus(WindowEvent evt) { refresh( ); }
    public void refresh( ) {
        if(frame.isVisible( )) {
            repaint( );
            refreshRequested = true;
            lastupdate = new Date( ).getTime( );
        }
    }
    public void run( ) {
        try {
            while(true) {
                Thread.sleep(250);
                long now = new Date( ).getTime( );
                if(refreshRequested &&
                    ((now - lastupdate) > 1000)) {
                    if(frame.isVisible( )) {
                        Point location = frame.getLocation( );
                        frame.hide( );
                        updateBackground( );
                        frame.show( );
                    frame.setLocation(location);
                        refresh( );
                    }
                    lastupdate = now;
                    refreshRequested = false;
                    }
                }
            } catch (Exception ex) {
               // p(ex.toString( ));
                ex.printStackTrace( );
            }
        }
    public void updateBackground( ) {
        try {
            Robot rbt = new Robot( );
            Toolkit tk = Toolkit.getDefaultToolkit( );
            Dimension dim = tk.getScreenSize( );
            background = rbt.createScreenCapture(
            new Rectangle(0,0,(int)dim.getWidth( ),
                              (int)dim.getHeight( )));
        } catch (Exception ex) {
            //p(ex.toString( ));
            ex.printStackTrace( );
        }
    }
        @Override
    public void paintComponent(Graphics g) {
        Point pos = this.getLocationOnScreen( );
        Point offset = new Point(-pos.x,-pos.y);
        g.drawImage(background,offset.x,offset.y,null);
    }
    public static void main(String[] args) {
        JFrame frame = new JFrame("Transparent Window");
        TransparentBackground bg = new TransparentBackground(frame);
        bg.setLayout(new BorderLayout( ));
        //JButton button = new JButton("This is a button");
        //bg.add("North",button);
    JLabel label = new JLabel("This is a label");
        bg.add("South",label);
        frame.getContentPane( ).add("Center",bg);
        frame.pack( );
        frame.setSize(150,100);
        frame.show( );
    }
    }
    feel free to tell us how you improve it ;d, maybe in future someone will need something like this but little upgraded ;)

    cheers,
    michael

  12. #12
    Begrezen is offline Member
    Join Date
    Oct 2010
    Posts
    1
    Rep Power
    0

    Default

    isn't better just use:
    com.sun.awt.AWTUtilities.setWindowOpacity(<any component>, 0.5f);

    <any component> - i think that any :) i tried with JWindow and JFrame and both work :)

    Java Code:
    import java.awt.FlowLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JWindow;
    
    public class Main
    {
        public static void main(String[] args)
        {
            JFrame frame = new JFrame("The Frame");
            frame.setSize(300, 300);
            frame.setLocation(100, 200);
    
            JWindow window = new JWindow();
            window.setSize(300, 300);
            window.setLocation(500, 200);
            com.sun.awt.AWTUtilities.setWindowOpacity(window, 0.5f);
            com.sun.awt.AWTUtilities.setWindowOpacity(frame, 0.5f);
    
            JPanel pan = new JPanel();
            JPanel pan2 = new JPanel();
    
            window.add(pan, "Center");
            frame.getContentPane().add(pan2);
    
            pan.setLayout(new FlowLayout());
            pan.add(new JButton("Hello"));
    
            pan2.setLayout(new FlowLayout());
            pan2.add(new JButton("Hello"));
    
            frame.setVisible(true);
            window.setVisible(true);
    
        }
    }

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    @Begrezen: shh, this thread is long dead; it doesn't even smell funny anymore.

    kind regards,

    Jos

Similar Threads

  1. Java SWT Transparent Buttons
    By ynnorj in forum AWT / Swing
    Replies: 7
    Last Post: 11-25-2009, 05:48 AM
  2. SWT Transparent button
    By ynnorj in forum SWT / JFace
    Replies: 2
    Last Post: 03-04-2009, 06:33 AM
  3. Making an Image transparent?
    By audinue in forum Java 2D
    Replies: 3
    Last Post: 01-03-2009, 07:32 PM
  4. Replies: 1
    Last Post: 07-17-2007, 04:10 AM
  5. Transparent JTextPane
    By Ada in forum AWT / Swing
    Replies: 1
    Last Post: 05-31-2007, 09:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •