1
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Frame {

    private JFrame jFrame;

    public Frame() {
        try {
            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
            JFrame.setDefaultLookAndFeelDecorated(true);
            JDialog.setDefaultLookAndFeelDecorated(true);
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }
    }

    private void create() {
        jFrame = new JFrame("frame");
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setSize(200, 200);
        jFrame.setVisible(true);
    }

    public static void main(String[] args) {
        new Frame().create();
    }

}

The code above works fine but if I set jFrame.undecorated to true, it doesn't remove the frame? Does anyone know why not? Thanks.

Edit: Also found that if I set jFrame.undecorated to false, another frame with default look and feel also displays. Like this:

example

user1009569
  • 477
  • 6
  • 22
  • please my question is - in Win8 & Java7? – mKorbel Feb 17 '14 at 20:33
  • 1
    AFAIK there is issue with [Transparency and undecorated container in Java7(OS isn't important)](http://stackoverflow.com/questions/16219111/cant-transparent-and-undecorated-jframe-in-jdk7-when-enabling-nimbus) – mKorbel Feb 17 '14 at 22:52

2 Answers2

1

Check the doc on the setUndecorated() method - can only be called when not visible. Your code with the two calls commented out in the constructor, but added jFrame.setUndecorated(true); before the setVisible() call.

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Frame {

private JFrame jFrame;

public Frame() {
    try {
                UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        //JFrame.setDefaultLookAndFeelDecorated(true);
        //JDialog.setDefaultLookAndFeelDecorated(true);

    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
        e.printStackTrace();
    }
}

private void create() {
    jFrame = new JFrame("frame");
    jFrame.setUndecorated(true);
    jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jFrame.setSize(200, 200);
    jFrame.setVisible(true);

}

public static void main(String[] args) {
    new Frame().create();
}

}
mikemil
  • 1,213
  • 10
  • 21
  • Yes I knew that it to be not visible. With the JFrame.setDefaultLookAndFeelDecorated(true); commented, the JFrame is not going to display the look and feel. Also, I want to be able to remove the frame with the click of a button which, setUndecorated(true) does not work. – user1009569 Feb 17 '14 at 19:34
  • So I wonder if doing something like minimizing the frame, then setting to undecorated would still be considered as 'not visible'? Guessing not but might be worth a try. – mikemil Feb 17 '14 at 19:43
  • It's got nothing to do with it being visible, as i said above, setting the undecorated flag to true, whether it's visible or not, does not removed the frame. – user1009569 Feb 17 '14 at 19:51
-1

I had a similar issue, what worked for me:

setUndecorated(true) inside the Constructor of frame you want to have undecorated. In the main class before setting Look and Feel instantiate the Object (your Frame). Once called setVisible down the road it worked for me.

slfan
  • 8,950
  • 115
  • 65
  • 78
  • `JFrame.setDefaultLookAndFeelDecorated(true)` will not effect the JFrame if if you invoke the method **after** JFrame is initialized. `Provides a hint as to whether or not newly created JFramesshould have their Window decorations (such as borders, widgets toclose the window, title...) provided by the current lookand feel` - `JFrame.setDefaultLookAndFeelDecorated(true)` Javadoc – Coding Mason Feb 13 '21 at 10:45