1

I'am trying right now to set the opacity of a JFrame to 50 % from a JMenuItem. My Last try was this:

op50.addActionListener((ActionEvent y) -> {
        setUndecorated(true);
        AWTUtilities.setWindowOpacity(this, 0.5F);
    });

But no matter what I'm doing, I run into an java.awt.IllegalComponentStateException Error with the Message: The Frame is displayable.

I don't know how to do it, so please help me.

xcynthos
  • 49
  • 2
  • 5
  • 1
    You can't set change the opacity of a JFrame when it is already visible – FredK Jan 04 '18 at 20:44
  • Show a complete example, I believe it is possible. [mcve] – Amber Jan 04 '18 at 22:12
  • You would need to `dispose` of the frame first, in order to release it's reference to the native peer (which is making it `displayable`), (probably) create a new instance of the frame, apply the opacity and then show it again. Your next problem is, you can't make a window which has native decorations transparent :P – MadProgrammer Jan 04 '18 at 22:24
  • This might help you. https://stackoverflow.com/questions/875132/how-to-call-setundecorated-after-a-frame-is-made-visible – Amber Jan 04 '18 at 22:31

1 Answers1

0

Without seeing a full code sample it's impossible to tell where you're going wrong, but here's a sample for how to do it from a JMenuItem. *Note from the comments this doesn't work beyond Java 1.6.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import com.sun.awt.AWTUtilities;

public class Main
{
    public static void main(String[] args)
    {
        final JFrame frame = new JFrame();

        JMenuBar menuBar = new JMenuBar();

        JMenu menu = new JMenu("Menu");
        menuBar.add(menu);

        JMenuItem menuItem = new JMenuItem("Change Opacity");
        menuItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e)
            {
                AWTUtilities.setWindowOpacity(frame, 0.5F);
            }
        });

        menu.add(menuItem);

        frame.setJMenuBar(menuBar);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}
Amber
  • 2,413
  • 1
  • 15
  • 20
  • Have you run the code? It ends in the exact same way as the OPs current problem – MadProgrammer Jan 04 '18 at 22:23
  • @MadProgrammer Yes I did run the code. I wonder if it depends on the java version? I happened to be using 1.6.0_25. – Amber Jan 04 '18 at 22:25
  • Sure enough, works with 1.6 but not 1.7 or 1.8. I'm not changing the look and feel - running only with the code shown. – Amber Jan 04 '18 at 22:26
  • I'm running Java 8, from my experience, it's never worked - are you using the default look and feel? – MadProgrammer Jan 04 '18 at 22:26
  • Since Java 1.6 and 1.7 are both beyond "end of life" support, personally, I'd find it irrelevant. You could document the fact, but since the OP is experiencing the said issue, I would assume they aren't using 1.6 – MadProgrammer Jan 04 '18 at 22:30