Thursday, April 28, 2011

How can I create a Java Swing app that covers the Windows Title bar?

I'm working on a java swing application that will be used in a psychology experiment and the researchers have requested that I make the program "black out the screen" in order that there should be no outside stimuli for the user. They want the swing app to be truly full-screen and without any type of title bar or minimize/maximize/close buttons on the top.

The software will be running in a Windows XP environment using JavaSE 6.

How can I do this and please provide a code snippet if applicable.

Thanks!

From stackoverflow
  • Use the setUndecorated(true) property. Note that this has to be done before making the frame visible.

    JFrame frame = new JFrame();
    Toolkit tk = Toolkit.getDefaultToolkit();
    frame.setBounds(new Rectangle(new Point(0, 0), tk.getScreenSize()));
    frame.setUndecorated(true);
    frame.setVisible(true);
    
  • Use the Full Screen Java APIs?

    http://java.sun.com/docs/books/tutorial/extra/fullscreen/exclusivemode.html

    http://www.artificis.hu/2006/03/16/java-awtswing-fullscreen

    JFrame fr = new JFrame();
    fr.setResizable(false);
    if (!fr.isDisplayable()) {
        // Can only do this when the frame is not visible
        fr.setUndecorated(true);
    }
    GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
    try {
      if (gd.isFullScreenSupported()) {
        gd.setFullScreenWindow(fr);
      } else {
        // Can't run fullscreen, need to bodge around it (setSize to screen size, etc)
      }
      fr.setVisible(true);
      // Your business logic here
    } finally {
      gd.setFullScreenWindow(null);
    }
    
    Yoely : Can you explain (concisely) how these APIs are used instead of just posting links? I'll obviously read the links but still...
    erickson : Full-screen mode uses a different paradigm from normal Swing applications. It's intended for things like games or screen savers. As the link says, "Most full-screen exclusive applications are better suited to use undecorated windows." It's an option, but will require you to write more code yourself.
    JeeBee : That's a mash up of both links, untested. Helps?
    JeeBee : erickson - the user requirement: make the program "black out the screen" in order that there should be no outside stimuli for the user
    David Zaslavsky : Unfortunately I don't know this particular API well enough to give you a code sample, but if I were you I'd definitely invest the effort to learn it. It sounds like exactly the right solution for your problem.
    erickson : JeeBee, that's a simple requirement easily met by undecorating the frame.
    Yoely : erickson - How much extra code are we talking about here using this API? Are you saying that I wouldn't be able to use the standard Swing containers and dialog boxes because those are absolutely imperative to the application?
    erickson : @Yoely, you can use normal Swing containers. I'm not sure about dialogs and other top-level frames. If you aren't writing low-level painting for some of your own components, then you aren't taking advantage of full-screen mode. Try it and see what breaks, or just use the simple, reliable approach.
    Tom Hawtin - tackline : You can use JInternalFrames. http://java.sun.com/docs/books/tutorial/uiswing/components/internalframe.html

0 comments:

Post a Comment