Friday, August 31, 2012

Implement Translucent Window

Example to implement Translucent Window of JFrame.

Translucent Window


package javatestswing;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaTestSwing {

    public static void main(String[] args) {
        JFrame.setDefaultLookAndFeelDecorated(true);
        SwingUtilities.invokeLater(runJFrameLater);
    }
    
    static Runnable runJFrameLater = new Runnable() {

        @Override
        public void run() {
            JFrameWin jFrameWindow = new JFrameWin();
            jFrameWindow.setOpacity(0.85f);
            jFrameWindow.setVisible(true);
        }
    
    };
}


package javatestswing;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JFrameWin extends JFrame{
    
    public JFrameWin(){
    
        this.setTitle("java-buddy.blogspot.com");
        this.setSize(500, 400);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         
        JButton buttonExit = new JButton(" Exit ");
        buttonExit.addActionListener(new ActionListener(){
 
            @Override
            public void actionPerformed(ActionEvent ae) {
                System.exit(0);
            }
        });
         
        this.add(buttonExit);
    
    }
}


To set opacity of JFrame, call it's setOpacity(float opacity) method. Please note that the following conditions must be met in order to set the opacity value less than 1.0f:
  • The TRANSLUCENT translucency must be supported by the underlying system
  • The window must be undecorated (see setUndecorated(boolean) and Dialog.setUndecorated(boolean))
  • The window must not be in full-screen mode (see GraphicsDevice.setFullScreenWindow(Window))

If the requested opacity value is less than 1.0f, and any of the above conditions are not met, the window opacity will not change, and the IllegalComponentStateException will be thrown. That's why we have to call JFrame.setDefaultLookAndFeelDecorated(true).

Read details here: setOpacity.


Wednesday, August 29, 2012

Video: JavaFX Canvas

JavaFX Canvas Node
A quick run through of what the JavaFX Canvas node is and how to use it in an application.