Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't transparent and undecorated JFrame in JDK7 when enabling nimbus

Look at this picture : Transparent JFrame

here is the code that transparent's the frame:

GraphicsEnvironment ge = 
        GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();

        if (!gd.isWindowTranslucencySupported(TRANSLUCENT)) {
            System.err.println(
                "Translucency is not supported");
                System.exit(0);
        }

        JFrame.setDefaultLookAndFeelDecorated(true);

this works good but when trying to enable LookAndFeel by adding

    try {
    for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
         if ("Nimbus".equals(info.getName())) {
            javax.swing.UIManager.setLookAndFeel(info.getClassName());
            break;
          }
    }
}catch(.......)

it gives me this error

Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: The frame is decorated

What's this error ? and how to solve it ?

Thanks for your answers and suggestions.

EDIT

Question Asked/CrossPosted

  • OTN

  • Daniweb

  • CodeRanch

like image 634
Azad Avatar asked Apr 25 '13 15:04

Azad


1 Answers

Change the laf in the main method before ui is created by @Sri Harsha Chilakapati

and @Sri Harsha Chilakapati I am sorry but I didn't get you I'll be appreciated if you describe more by @Azad Omer

  • more in Oracle tutorial Modifying the Look and Feel,

  • issue is caused code line JFrame.setDefaultLookAndFeelDecorated(true);, required to disable/comment this code line //JFrame.setDefau...

  • by default there no issue to create translucent JFrame with Nimbus L&F

enter image description here

from code

import java.awt.*;
import javax.swing.*;

public class TranslucentWindow extends JFrame {

    private static final long serialVersionUID = 1L;

    public TranslucentWindow() {
        super("Test translucent window");
        setLayout(new FlowLayout());
        add(new JButton("test"));
        add(new JCheckBox("test"));
        add(new JRadioButton("test"));
        add(new JProgressBar(0, 100));
        JPanel panel = new JPanel() {

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 300);
            }
            private static final long serialVersionUID = 1L;

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.red);
                g.fillRect(0, 0, getWidth(), getHeight());
            }
        };
        panel.add(new JLabel("Very long textxxxxxxxxxxxxxxxxxxxxx "));
        add(panel);
        pack();
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        try {
            for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //JFrame.setDefaultLookAndFeelDecorated(true);
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                Window w = new TranslucentWindow();
                w.setVisible(true);
                com.sun.awt.AWTUtilities.setWindowOpacity(w, 0.7f);
            }
        });
    }
}
like image 145
mKorbel Avatar answered Nov 15 '22 18:11

mKorbel