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

3

8

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

Kidwell answered 25/4, 2013 at 15:40 Comment(8)
Change the laf in the main method before ui is createdCommotion
@Sri Harsha Chilakapati I am sorry but I didn't get you I'll be appreciated if you describe moreKidwell
The problem is that since you are setting the look n feel after enabling transparency. It gives the exception since nimbus didn't have support for decorated frames.Commotion
@Kidwell Omer have to wait one two days, please notify me if you'll don't any valuable response, then I'll ask a new question about this issueAmends
@Amends Ok I will wait Thanks :)Kidwell
@Kidwell Omer you should add an answer to this questin, otherwise my 100p bounty go away to the trash, I can't attach those points to my answer here, no answer here for those points :-)Amends
@Amends I am very thankful for your efforts, the 100p should be for the accepted answer if you want me to post it I will post your answer :)Kidwell
See also #6259769Fullmer
A
4
  • accepted answer by @JamesCherrill on Daniweb,

  • 1st. Top-Level Container created on InitialThread must be decorated and isDisplayable(), then after is possible whatever with rest of

  • problably required short delaying by Swing Timer

.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class DemoWindows implements ActionListener {

    public static void main(String[] args) {
        // create a new demo, and update it every 50 mSec
        new Timer(30, new DemoWindows()).start();
    }
    int phase = 0; // demo runs a number of consecutive phases
    int count = 0; // each of which takes a number of timesteps
    JFrame window1 = new JFrame("Java windows demo");
    JLabel text1 = new JLabel("<HTML><H1>Hello" + "<BR>Everyone");
    // "<HTML><H1>This is a demo of some of the effects"
    // + "<BR>that can be achieved with the new Java"
    // + "<BR>transparent window methods</H1>"
    // + "<BR>(requires latest version of Java)");
    JFrame window2 = new JFrame("Java windows demo");
    JLabel text2 = new JLabel("<HTML><center>Java<BR>rocks");
    JButton button = new JButton("Whatever");
    int w, h, r, x, y; // parameters of iris circle

    DemoWindows() {
        // build and diplay the windows
        window1.add(text1);
        window1.pack();
        window1.setLocationRelativeTo(null);
        window1.setVisible(true);
        window2.setUndecorated(true);
        window2.setBackground(new Color(0, 0, 0, 0)); // alpha <1 = transparent
        window2.setOpacity(0.0f);
        text2.setFont(new Font("Arial", 1, 60));
        text2.setForeground(Color.red);
        window2.add(text2);
        window2.add(button, BorderLayout.SOUTH);
        window2.pack();
        window2.setLocationRelativeTo(null);
        window2.setVisible(true);
        // parameters of the smallest circle that encloses window2
        // this is the starting pouint for the "iris out" effect
        w = window2.getWidth();
        h = window2.getHeight();
        r = (int) Math.sqrt(w * w + h * h) / 2; // radius
        x = w / 2 - r; // top left coordinates of circle
        y = h / 2 - r;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        try {// L&F changed on Runtime, repeatly fired from Swing Timer 
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DemoWindows.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(DemoWindows.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(DemoWindows.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(DemoWindows.class.getName()).log(Level.SEVERE, null, ex);
        }
        SwingUtilities.updateComponentTreeUI(window2);
        // called by timer 20 times per sec
        // goes thru a number of phases, each a few seconds long
        switch (phase) {
            case 0: { // initial pause               
                if (++count > 50) {
                    phase = 1; // go to next phase
                    count = 0;
                }
                break;
            }
            case 1: { // fade in               
                if (++count < 100) {
                    window2.setOpacity(0.01f * count);
                } else {
                    phase = 2; // go to next phase
                    count = 0;
                }
                break;
            }
            case 2: { // move               
                if (++count < 160) {
                    if (count < 28 || count > 80) {// pause for best effect
                        window2.setLocation(window2.getX() + 1, window2.getY() + 1);
                    }
                } else {
                    phase = 3; // go to next phase
                    count = 0;
                }
                break;
            }
            case 3: {// iris out                
                if (++count < r) {
                    Shape shape = new Ellipse2D.Double(
                            x + count, y + count, 2 * (r - count), 2 * (r - count));
                    window2.setShape(shape);
                } else {
                    phase = 99; // go to final (exit) phase
                }
                break;
            }
            case 99:
                System.exit(0);
        }
    }
}
Amends answered 7/5, 2013 at 9:43 Comment(2)
@Kidwell Omer please to see Java / Swing -> Creating a notification JFrame, and the error “The frame is displayable”, but still not possible to create decorated translucent window and with changed L&FAmends
Not a solution! - There's a default LAF decorated frame and changed LAF undecorated frame. Trying to make the changed LAF frame decorated results in the OP's error again.Outlawry
A
5

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);
            }
        });
    }
}
Amends answered 25/4, 2013 at 16:27 Comment(7)
Thanks for the answer, but IllegalComponentStateException tells me that the frame is decorated. for information : I am using jdk1.7.0_15 and Netbeans 7.3Kidwell
The problem is in this line AWTUtilities.setWindowOpacity(w, 0.2f);Kidwell
aaach you are right bug for me too, all code that works for public AWTUtilities.setWindowOpacity(), doesn't works in Java7, including undecorated containerAmends
@aterai please (AFAIK you are most active in this area) can you comment this issueAmends
it seems to be this package (com.sun.awt.AWTUtilities) changes a lot that's why they advice to not use it if possible .Kidwell
agreed but code from official tutorial doesn't works too (undecorated container)Amends
I am so sorry I went out Mr.mKorbel I am here knowKidwell
A
4
  • accepted answer by @JamesCherrill on Daniweb,

  • 1st. Top-Level Container created on InitialThread must be decorated and isDisplayable(), then after is possible whatever with rest of

  • problably required short delaying by Swing Timer

.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class DemoWindows implements ActionListener {

    public static void main(String[] args) {
        // create a new demo, and update it every 50 mSec
        new Timer(30, new DemoWindows()).start();
    }
    int phase = 0; // demo runs a number of consecutive phases
    int count = 0; // each of which takes a number of timesteps
    JFrame window1 = new JFrame("Java windows demo");
    JLabel text1 = new JLabel("<HTML><H1>Hello" + "<BR>Everyone");
    // "<HTML><H1>This is a demo of some of the effects"
    // + "<BR>that can be achieved with the new Java"
    // + "<BR>transparent window methods</H1>"
    // + "<BR>(requires latest version of Java)");
    JFrame window2 = new JFrame("Java windows demo");
    JLabel text2 = new JLabel("<HTML><center>Java<BR>rocks");
    JButton button = new JButton("Whatever");
    int w, h, r, x, y; // parameters of iris circle

    DemoWindows() {
        // build and diplay the windows
        window1.add(text1);
        window1.pack();
        window1.setLocationRelativeTo(null);
        window1.setVisible(true);
        window2.setUndecorated(true);
        window2.setBackground(new Color(0, 0, 0, 0)); // alpha <1 = transparent
        window2.setOpacity(0.0f);
        text2.setFont(new Font("Arial", 1, 60));
        text2.setForeground(Color.red);
        window2.add(text2);
        window2.add(button, BorderLayout.SOUTH);
        window2.pack();
        window2.setLocationRelativeTo(null);
        window2.setVisible(true);
        // parameters of the smallest circle that encloses window2
        // this is the starting pouint for the "iris out" effect
        w = window2.getWidth();
        h = window2.getHeight();
        r = (int) Math.sqrt(w * w + h * h) / 2; // radius
        x = w / 2 - r; // top left coordinates of circle
        y = h / 2 - r;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        try {// L&F changed on Runtime, repeatly fired from Swing Timer 
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DemoWindows.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(DemoWindows.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(DemoWindows.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(DemoWindows.class.getName()).log(Level.SEVERE, null, ex);
        }
        SwingUtilities.updateComponentTreeUI(window2);
        // called by timer 20 times per sec
        // goes thru a number of phases, each a few seconds long
        switch (phase) {
            case 0: { // initial pause               
                if (++count > 50) {
                    phase = 1; // go to next phase
                    count = 0;
                }
                break;
            }
            case 1: { // fade in               
                if (++count < 100) {
                    window2.setOpacity(0.01f * count);
                } else {
                    phase = 2; // go to next phase
                    count = 0;
                }
                break;
            }
            case 2: { // move               
                if (++count < 160) {
                    if (count < 28 || count > 80) {// pause for best effect
                        window2.setLocation(window2.getX() + 1, window2.getY() + 1);
                    }
                } else {
                    phase = 3; // go to next phase
                    count = 0;
                }
                break;
            }
            case 3: {// iris out                
                if (++count < r) {
                    Shape shape = new Ellipse2D.Double(
                            x + count, y + count, 2 * (r - count), 2 * (r - count));
                    window2.setShape(shape);
                } else {
                    phase = 99; // go to final (exit) phase
                }
                break;
            }
            case 99:
                System.exit(0);
        }
    }
}
Amends answered 7/5, 2013 at 9:43 Comment(2)
@Kidwell Omer please to see Java / Swing -> Creating a notification JFrame, and the error “The frame is displayable”, but still not possible to create decorated translucent window and with changed L&FAmends
Not a solution! - There's a default LAF decorated frame and changed LAF undecorated frame. Trying to make the changed LAF frame decorated results in the OP's error again.Outlawry
K
3

After some researching I find out that the problem is between JDK7 and com.sun.awt.AWTUtilities, I think we'v better to not use com.sun packages except as a last resort as they may cause problems with upgrading JDK versions (they are not part of JDK API).

Read more about this issues Here

From Oracle

The Nimbus look-and-feel for Swing was introduced in JDK 6u10 as a replacement for the old Metal LoF. With JDK 7, Nimbus will be moved from an Oracle-proprietary extension (com.sun.java.swing) to a standard API (javax.swing) so that it becomes a true first class Swing citizen.

It seems to be com.sun.awt.AWTUtilities works fine with JDK6 but Nimbus LAF is in JDK7. Ills of less I find out the answer of my first question (What's this error), and for the second question(How to solve it) I must wait till new version of com.sun release.

I am grateful for mKorbel's efforts, thank you .

Kidwell answered 6/5, 2013 at 21:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.