Exception in thread "main" java.awt.Illegalcomponentstateexception: The frame is Decorated

Asked

Viewed 139 times

1

Guys I came back, I started studying AWT I made a simple program that just opened a window with a green background, but I try to create an event to the program window close and is giving the title error

Error:

Exception in thread "main" java.awt.Illegalcomponentstateexception:
Theframe is Decorated at
java.desktop/java.awt.Frame.setOpacity(Frame.java:963) at
Ventana.main(Ventana.java:19)

code that I’m using:

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;

public class Ventana {

    public static void exitForm(WindowEvent evt){
        System.exit(0);

    }

    public static void main(String[] args){
        Frame frame = new Frame("Novo video!");
        frame.setUndecorated(false);
        frame.setSize(600,600);
        frame.setLocation(520,250);
        frame.setOpacity((float)0.50);
        frame.setBackground(new Color(161, 203, 70));
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt){
                exitForm(evt);

           }


       });
        frame.show();



   }

}

I’m pretty new, sorry if I’m making a mistake well "Noob", but I already appreciate the attention <3

1 answer

1

Let’s see the description of setUndecorated:

Disables or Enables decorations for this frame.

This method can only be called while the frame is not displayable. To make this frame Decorated, it must be Opaque and have the default Shape, otherwise the IllegalComponentStateException will be thrown. Refer to Window.setShape(java.awt.Shape), Window.setOpacity(float) and Window.setBackground(java.awt.Color) for Details

Translating:

Disables or activates decorations for this frame.

This method can only be called when the frame is not available. To make this frame decorated, it should be opaque and have the standard format, otherwise the IllegalComponentStateException shall be launched. Reference shall be made to Window.setShape(java.awt.Shape), Window.setOpacity(float) and Window.setBackground(java.awt.Color) for details

Well, the frame.setUndecorated(false); means that the frame will be decorated. If it is decorated, then according to the documentation it should be opaque. However, this contradicts what is specified in frame.setOpacity((float)0.50);. Therefore, you are putting a situation that the documentation of the setUndecorated makes it clear that it results in a IllegalComponentStateException.

When you use the frame.setUndecorated(false);, means that the frame is a normal window, rectangular format, with normal icons and borders and no transparency. The reason for this is that this is the type of standard window used by 99% of the windows of our applications and that is therefore much simpler and faster to be handled by the operating system.

You would hardly want or need a transparent or non-rectangular window, and your code does not indicate that this would be the case. Therefore, I suggest only remove the frame.setOpacity((float)0.50);.

Ah, I also recommend giving a look at this other question, because using the swing inside the main not a good idea. The best solution would be to put the code that uses swing in a method private static void mostraTela() { /* ... */ } and then in the method main, wear a EventQueue.invokeLater(Ventana::mostrarTela);.

In addition, the use of System.exit(0); is usually a bad programming practice. Avoid this and prefer to use a frame.dispose(); within its method windowClosing.

You can also use setLocationRelativeTo(null) to center the frame.

Your complete code, although it still doesn’t do anything very useful, would look like this:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;

public class Ventana {

    public static void main(String[] args) {
        EventQueue.invokeLater(Ventana::mostrarTela);
    }

    private static void mostraTela() {
        Frame frame = new Frame("Novo video!");
        frame.setUndecorated(false);
        frame.setSize(600, 600);
        frame.setLocationRelativeTo(null);
        frame.setBackground(new Color(161, 203, 70));
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent evt){
                frame.dispose();
            }
        });
        frame.show();
   }
}
  • because it is not a "good idea" to use the swing inside the main?

  • Ali, can you give me an example of how to do it? I started studying Java a little while ago, and the way I’m learning I’ve never used it;-;...I could give an example of "private Static ..." anyway...an example of the right way to do it

  • @joaoazevedo I set the example. Follow the link I placed to see why it is not a good idea to use the swing inside the main.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.