1

I have and undecorated JFrame, not resizable and placed bottom/center of the screen. The JFrame have 100 pixels height and 1000 pixels width. If the taskbar change his position, I want to place the JFrame according to the new position and size of the taskbar.

I already know how to place the JFrame just above of the taskbar, just need to know if there is any way to "listen" for this changes and set the new location of the JFrame automaticaly. I am using Eclipse and jdk 1.8

EDIT: I am not asking about listening screen changes. Is about taskbar changes. I can handle the screen changes.

Bob Gilmore
  • 12,608
  • 13
  • 46
  • 53
alfatymajo
  • 11
  • 2

1 Answers1

0

The answers to Detect screen resolution change made by user (Java Listener?) seem indicate there is no built in support for detecting that type of change to the native window system.

One of the answers suggested there was to hook into a native event using something like JNA or JNI. Unfortunately, they didn't provide any information on what underlying windows event to hook into.

Another option would be to run a background thread which periodically checks if the insets have changed, then adjust the position of your JFrame accordingly. You'd need to play around with polling rate, but I suspect you could do this a couple times per second without harming system performance.

Community
  • 1
  • 1
Jason Braucht
  • 2,358
  • 19
  • 31
  • Thanks for your answer. I thinking the same about using a background thread checking the insets but as you said is hard to do this without harming system performance. Also thinked to make 4 customs jframes and create or show them during runtime but I do not know how to do it so that it seems to be automatic. – alfatymajo Mar 08 '17 at 18:02
  • I've used polling in Swing apps before and found that as long as the polling frequency is reasonable, there is virtually no harm to performance. I suggest you try polling every 500ms as starting point and work your way down to 200ms (below 200ms probably isn't going to offer much benefit anyway) – Jason Braucht Mar 08 '17 at 18:08
  • What is the best way to do it ? I am new with threads. I think about a TimerTask but i doubt if it is a good solution – alfatymajo Mar 08 '17 at 18:32
  • I'd suggest taking a look at [`ScheduledThreadPoolExecutor`](https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledThreadPoolExecutor.html). As this [answer](http://stackoverflow.com/a/14315345/109412) shows, `ScheduledThreadPoolExecutor` is (basically) a drop-in replacement for `Timer`, but it addresses some of the shortcomings of `Timer`. – Jason Braucht Mar 08 '17 at 18:55