Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a JFrame without overlapping with taskbar

Tags:

java

swing

jframe

I need to have a undecorated JFrame(setUndecorated(true)) which need to be shown fullscreen, without overlapping with the taskbar.

I have tried the below solutions.

  • Calling setExtendedState(MAXIMIZED_BOTH).

    • Advantage: This works fine as expected, i.e., the window is getting adjusted itself dynamically, except it has the below issues.
    • Issues Initially the window occupies the fullscreen Though the frame get adjusted itself dynamically, it overlaps with the taskbar.
  • Tried the below solution as stated in Does JFrame.setExtendedState(MAXIMIZED_BOTH) work with undecorated frames?

     GraphicsConfiguration config = aFrame.getGraphicsConfiguration();
     Rectangle usableBounds = SunGraphicsEnvironment.getUsableBounds(config.getDevice());
     aFrame.setBounds(0, 0, usableBounds.width, usableBounds.height);
    
    • Advantage: I am not getting overlaps and window looks fine.
    • Issue: Window is not adjusting itself dynamically when the taskbar position/size is changed.

Any help would be greatly appreciated.

I thought of a design. But not sure about its feasibility. I can use the setBounds(). But then I need my frame to be notified when the task bar is adjusted or repositioned. Is there a way?

like image 989
Kannan Ramamoorthy Avatar asked Oct 31 '25 01:10

Kannan Ramamoorthy


1 Answers

Able to able to fix the above issue with the below code,

Rectangle usableBounds = SunGraphicsEnvironment.getUsableBounds(config.getDevice());
setMaximizedBounds(usableBounds);
setExtendedState(MAXIMIZED_BOTH);

So by getUsableBounds I am able to get the bounds leaving the taskbar. And hence I am using setExtendedState(MAXIMIZED_BOTH) the window is getting updated automatically when I re-size/re-position the taskbar. :-)

like image 145
Kannan Ramamoorthy Avatar answered Nov 02 '25 15:11

Kannan Ramamoorthy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!