3

I have a strange behaviour with a legacy application I have to maintain. On startup, the JFrame of the application is moved to the position where it has been closed recently (which works fine, even on multi monitor settings).
When the JFrame is moved to a secondary monitor (done by calling setLocation(x, y)) and a JDialog is shown after calling setRelativeTo(mainFrame), the JDialog appears on the primary monitor.

One has to drag the mainframe on the primary monitor and back to the secondary one to avoid the problem.

I have found, that calling mainFrame.getGraphicsConfiguration().getDevice().getIDstring() returns \Display0 when the application is started (means 'Primary Display' afaik), even though it is shown on the secondary monitor. When I drag the main frame to the primary monitor and back to the secondary one, the Method returns \Display1.

Now I have 2 questions:

  1. Is there a way to tell the application on which monitor it is positioned?
  2. I have tried to create a simple example with JFrame and a JDialog. Unfortunately it behaves as expected and I can't reproduce the problem. Do you have a hint on what to look for in my application, that could cause this behaviour?

Thank you very much
klib

klib009
  • 263
  • 5
  • 18
  • Hopefully this [thread](http://stackoverflow.com/q/14273836/1057230), might be able to help somewhat on the issue :-) Do watch the answer of the OP, I guess that can help a bit. – nIcE cOw Jul 29 '13 at 05:37

2 Answers2

3

After some more research I found that I had to call mainframe.setVisible(false);and again mainframe.setvisible(true);.
This is sufficient that mainframe.getGuiConfiguration.getDevice() returned the right device-ID.

I know, this is not really a solution to the original problem, but it might help if someone else experiences the same behaviour.

Anyway, Explanations / real solutions for the problem are still very appreciated.

klib009
  • 263
  • 5
  • 18
2

For your first question, you could use JFrame(GraphicsConfiguration).

If the problem isn't repeatable, there might be something else going on that is messing things up

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • That sounds like a good idea, but unfortunately, the JFrame moves itself to the desired position, so the object is already created when it is moved. Changing this would be a lot of work and might cause trouble elsewhere (as I mentioned, it's a legacy application I have to maintain now) – klib009 Jul 29 '13 at 05:29
  • IF it's in the creation of the frame code, it should be relatively safe. `setLocationRelativeTo` seems to use the frame's graphics conficguration. You would need to determine which GC would use the required location and pass that in as the primary GC... – MadProgrammer Jul 29 '13 at 05:32
  • Hmm.. How do i pass a `GraphicsConfiguration` to a JFrame except at construction time? I thought that is not possible. – klib009 Jul 29 '13 at 06:36
  • That's what I mean. If you control the construction of the frame. Find the `GraphicsConfiguration` that matches where the frame is to be shown and pass it to the frame constructor... – MadProgrammer Jul 29 '13 at 06:40
  • and a JDialog is shown after calling setRelativeTo(null), the JDialog appears on the primary monitor. == it proper output, for bettert help sooner post an SSCCE, short, runnable, compilable, just about Top-Level Containers – mKorbel Jul 29 '13 at 07:12
  • mKorbel, thank you for the input: There was a mistake in my original Question. I do not call `setRelativeTo(null)`, but `setRelativeTo(mainFrame)`. This should show the JDialog on top of the Frame on the secondary display, but it shows up on the primary monitor. As I said, i was not able to reproduce the behaviour as SSCCE (therefore my second querstion). – klib009 Jul 29 '13 at 08:26
  • 1
    @klib009 this was bug with some JDK (not sure but caused with JOptionPane, have to check bugs parade), never tried, never caused on stable (my decision that they are stable) JDKs, to try to test by using Point as coordinates instead of parent (just to avoiding bug) – mKorbel Jul 29 '13 at 15:07