开发者

show Jframe but not show title bar on task bar

开发者 https://www.devze.com 2022-12-16 09:38 出处:网络
In my application, I show a Jframe at the corner o开发者_如何学Pythonf screen for notification.

In my application, I show a Jframe at the corner o开发者_如何学Pythonf screen for notification. And I want to show only Jframe and do not display a title bar at task bar.

How can I do that?


If you want the window to just appear and have neither a title bar nor appear in the taskbar, use a JWindow.

If you want the window to appear and have a title bar but to not appear in the taskbar, use a JDialog.


JDK 1.7 brings you method setType. Use following method.

JFrame.setType(javax.swing.JFrame.Type.UTILITY)


"All you'd have to do is to set your JFrame "type" property to "UTILITY" and there you have it!"

This does work, at least under Java 1.7, its the same as the above "myframe".setType(Type.UTILITY) instead of Type.POPUP. Testing type popup (under win 7) does not work to remove it from the taskbar, Type.UTILITY does.

Undecorated will not have the desired results (as that removes the title bar from the window, not the taskbar)

public class Window extends Container implements Accessible {
    /**
     * Enumeration of available <i>window types</i>.
     *
     * A window type defines the generic visual appearance and behavior of a
     * top-level window. For example, the type may affect the kind of
     * decorations of a decorated {@code Frame} or {@code Dialog} instance.
     * <p>
     * Some platforms may not fully support a certain window type. Depending on
     * the level of support, some properties of the window type may be
     * disobeyed.
     *
     * @see   #getType
     * @see   #setType
     * @since 1.7
     */
    public static enum Type {
        /**
         * Represents a <i>normal</i> window.
         *
         * This is the default type for objects of the {@code Window} class or
         * its descendants. Use this type for regular top-level windows.
         */
        NORMAL,

        /**
         * Represents a <i>utility</i> window.
         *
         * A utility window is usually a small window such as a toolbar or a
         * palette. The native system may render the window with smaller
         * title-bar if the window is either a {@code Frame} or a {@code
         * Dialog} object, and if it has its decorations enabled.
         */
        UTILITY,

        /**
         * Represents a <i>popup</i> window.
         *
         * A popup window is a temporary window such as a drop-down menu or a
         * tooltip. On some platforms, windows of that type may be forcibly
         * made undecorated even if they are instances of the {@code Frame} or
         * {@code Dialog} class, and have decorations enabled.
         */
        POPUP
    }


You could try using a JWindow instead.


All you'd have to do is to set your JFrame "type" property to "UTILITY" and there you have it!


Use this, but it work only on JDK 1.7 or openJDK 1.7 :

// only on JDK 1.7 or openJDK 1.7

 JFrame f = new JFame(" frame not displayable in the task bar ");
    ...
    ...
    f.setType(Type.POPUP); // No Place on task bar, but stays on top of all others system applications frame


Just use a JWindow...

import javax.swing.JWindow;
import java.awt.Toolkit;
import java.awt.Dimension;

public class Notification extends JWindow {
   private final int WIDTH = 200;
   private final int HEIGHT = 30;

   public Notification() {
      positionWindow();
      setVisible(true);
   }

   // Place the window in the bottom right corner of the screen
   private void positionWindow() {
      Toolkit aToolkit = Toolkit.getDefaultToolkit();
      Dimension screen = aToolkit.getScreenSize();
      int xPosition = screen.width - (WIDTH + 10); // Right edge of the screen
      int yPosition = screen.height - (HEIGHT + 10); // Bottom edge of the screen
      setBounds(xPosition, yPosition, WIDTH, HEIGHT);
   }
}


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication4;

import javax.swing.JFrame;

/**
 *
 * @author ravi
 */
public class Main extends JFrame{

    /**
     * @param args the command line arguments
     */
    Main()
    {
       setState(JFrame.ICONIFIED);
        setSize(400, 400);
        setVisible(true);
    }
    public static void main(String[] args) {
        // TODO code application logic here
        Main m=new Main();
    }

}


Try adding a call to setUndecorated(true);. It tells the window manager to not add the title bar and window buttons.

Note: this must be called while the frame is not displayed.


One answer mentions to use JWindow which works out of the box for Windows and 2 others suggest to use javax.swing.JFrame.Type.UTILITY which is not needed in Windows if you already use JWindow. If you find yourself using JWindow under Linux and you still see it in the taskbar, you can use POPUP instead:

sWindow.setType( Window.Type.POPUP );

Read the documentation though, this might do other things as well (like removing decorations) which you might not need.

0

精彩评论

暂无评论...
验证码 换一张
取 消