0

I want to know the taskbar position for my javafx project. Means I want to know if the taskbar is in the top, bottom, left or right. I want to do something in code when the taskbar position is changed. How can I do these things?

  • I think you have to provide us more information about what you want. I mean, are you talking about the taskbar which is in the operating system? What do you want to do when the position of the taskbar changes? – Imaddin Ahmed Mohamed Jun 07 '18 at 13:28
  • I don't agree with closing the question as duplicate of this one: https://stackoverflow.com/questions/14431467/how-do-i-determine-the-position-of-the-system-tray-on-the-screen since there is a solution unique to the JavaFX API which imho warrants a seperate question. (The dupe candidate is borderline too broad.) I'll soon post this solution... – fabian Jun 07 '18 at 13:28

2 Answers2

2

The following approach uses the JavaFX API only.

I tested it with Win 10 & java 8 update 161. It works but isn't triggered when the task bar is allowed to hide dynamically displayed (it always considers the bounds to be equal to the visual bounds in this case).

Since the refreshing of the list is implemented by the non-public Toolkit API, it may not work properly for a different OS.

The ObservableList<Screen> returned by Screen.getScreens() changes when the (visual) bounds of a Screen changes. This fact can be used to determine the side of the task bar in the primary screen based on the difference between the bounds and the visualBounds:

InvalidationListener listener = new InvalidationListener() {

    private Side currentSide = null;

    public void invalidated(Observable o) {
        Screen primary = Screen.getPrimary();
        Rectangle2D bounds = primary.getBounds();
        Rectangle2D visualBounds = primary.getVisualBounds();
        Side side = null;

        // determine side of task bar, if size differs
        if (bounds.getWidth() > visualBounds.getWidth()) {
            if (bounds.getMinX() < visualBounds.getMinX()) {
                side = Side.LEFT;
            } else if (bounds.getMaxX() > visualBounds.getMaxX()) {
                side = Side.RIGHT;
            }
        } else if (bounds.getHeight() > visualBounds.getHeight()) {
            if (bounds.getMinY() < visualBounds.getMinY()) {
                side = Side.TOP;
            } else if (bounds.getMaxY() > visualBounds.getMaxY()) {
                side = Side.BOTTOM;
            }
        }

        // only notify once per change independent of size of task bar
        if (side != currentSide) {
            currentSide = side;
            System.out.println(side == null
                    ? "Side of task bar unknown/task bar not displayed"
                    : "The task bar is now displayed in the " + side);
        }
    }
};
Screen.getScreens().addListener(listener);
listener.invalidated(null);
fabian
  • 80,457
  • 12
  • 86
  • 114
2

You can obtain the screen insets for a given screen device using java.awt.Toolkit.

For the primary screen:

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice defaultScreenDevice = ge.getDefaultScreenDevice();
GraphicsConfiguration defaultConfiguration = defaultScreenDevice.getDefaultConfiguration();
Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(defaultConfiguration);

Insets for my primary screen (using Windows 10):

java.awt.Insets[top=0,left=0,bottom=40,right=0]

Therefore, the task bar is 40 pixels height, and at the bottom of the screen.

Peter Walser
  • 15,208
  • 4
  • 51
  • 78
  • Even if it should work for JavaFX application, it is a more relevant answer for Swing/awt apps. Moreover it is almost a duplicate of [this answer](https://stackoverflow.com/a/14431536/4629012). – Pagbo Jun 07 '18 at 14:14