Changing default window position to 2nd monitor? - java

I'm writing some code in Java that needs to show file chooser and alert dialogs. These dialogs always need to show up on a second monitor, rather than the main monitor.
If the application were entirely written in Java/Swing, these dialogs would be positioned relative to the application's main JFrame, and so would show up on the second monitor if that was the location of the main frame. In this application, however, the main window is created by native code. So Java doesn't know the location of the main window, and can't position its dialogs relative to it.
Is there a way that I can tell Java to show dialogs on the second monitor by default? (perhaps by changing some of the Look and Feel?)

"In a multi-screen environment, the GraphicsConfiguration objects can be used to render components on multiple screens."—GraphicsDevice. See also the Full-Screen Exclusive Mode API tutorial.

Related

Draw a window only on top of an external application in Java

I am trying to create a JWindow that sits on top of an external application.
I want the window to resize whenever the external application resizes (to match it) and be only on top of the external application and not everything else. (I can't use setAlwaysOnTop(), because it would be always on top of everything).
By modifying code from the first answer from here I am able to find the external window title (for example Untitled - Notepad) and its handle if needed.
Using code from the first answer from here, I can get the dimensions of an external window from it's window title.
The problem with finding the dimensions from the code at the second link is that I have to always loop and check them (which is not optimal) and it isn't helpful in putting my window only on top of the external one.
Is there a way to implement what I want in Java? Ideally, I would (using JNA?) make a listener to the messages (WinEvents?) the OS sends to the external program to resize it, gain/lose focus etc and handle them somehow.

Jframe loading window in java

I need to do a loading window in java like this:
I tried with the jframe but I can't figure it out, I can't remove the close button, my jframe should look like a jpanel before that program begin, somebody know how to do this.
You Could...
Use Java's inbuild splash screen support. This is good as it gets loaded relatively earlier in the process by the JVM itself, so it comes up reasonably quick.
The major drawback (in my opinion) is it can be troublesome to get right as it doesn't follow the standard painting process that most developers are use to and you are going to have to paint it all yourself, there's no component support
How do I use SplashScreen without throwing a NullPointerException?
How to Undo the splash screen painting in java
Splash Screen Progress bar not drawing
You Could...
Create a JWindow. This is a frameless window, which won't appear on the taskbar (at least for some OSs).
You gain complete control over what and how things get displayed, as it's like dealing with any other window.
The only problem is that it won't become available for you to load until the JVM has completed loading your application. If the JVM needs to download resources over the network, for example, this could produce an undesirable delay
For example
Splashscreen in Java
SplashScreen java change alpha
Why won't this draw the image?
You Could...
Use an undecorated frame. This is pretty much the same as the JWindow option, except that a taskbar icon will be displayed, but on some OSs, this can't be completely helped.
See Frame#setUndecorated for more details
Final thoughts
Swing is not thread safe. So if you're displaying a splash screen, this means you should be ensuring that whatever actions you are taking are done off the Event Dispatching Thread, possibly through the use of SwingWorker

Change Display Used for Eclipse Launches

I'm developing a Swing-based Java application in Eclipse on Windows XP. I have a dual monitor setup.
I want to have the program launch on a different monitor than the one I'm running Eclipse in. How can I set up my Debug Configuration to make this happen?
When a new frame is opened, it defaults to coordinates 0,0. Exactly where this is depends on your operating system's monitor layout. Typically, 0,0 will be the upper-left corner of the primary monitor.
The solution to your problem is to run Eclipse on your secondary monitor, so that the applications will open on the other (primary) monitor. You don't actually have to move Eclipse to do this. Go into your operating system's monitor settings and switch the primary monitor to the monitor you wish to start the application on. Eclipse will now be running on the secondary monitor.
Your new frame will now be opened on the other monitor, and you have changed no code in the process, just your operating system's configuration settings.
It depends a bit how you did set up your second monitor. For my answer, I'm assuming that you added it as an "extension" to your desktop (so you can move windows between them by dragging with the mouse).
In this mode, your desktop becomes bigger. To see that, call GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds.
The width should be the sum of the widths of your two monitors and the height should be the larger of the two heights.
Note that the x coordinate can be < 0 (this happens if your make the right monitor the default one).
To move your window to the other monitor, simply use a position (setPosition()) with an appropriate value and pass that position as an option to the program. Or save the current position as a preferences node and open the window again in the same place when the app is run again.
If your monitors are configured independently, you should look into the GraphicsEnvironment.getScreenDevices() API.
I think you can't do that. The only solution I found is to move the program from the first display to the second one manually and then close it in this position (with the close button and not the Console terminate red button that stop brutally the VM without saving anything). Then on the next restart, your program launch should appear on the second display. In order to have this solution working, your launch configuration should not clear the workspace and the config at each launch in order to keep the programm screen location.
See RCP opening monitor for related stuff on RCP app.
You can probably achieve that behavior programmatically. I'm sure you can Google and find examples of how to open your window on the second monitor. If it is something you want only at debug-time, add a switch in your launch configuration (more specifically, a JVM runtime argument) and check for the switch (System.getProperty) when your program starts.

JDialog Not Displaying When in Fullscreen Mode

I have an application that runs in fullscreen mode and has been working fine. Now I need to add a simple, undecorated dialog and I'm running into trouble. If I run the application maximized but not in fullscreen, the dialog displays and functions as expected. When I switch back to fullscreen, the dialog will not display.
The dialog extends JDialog and only contains a JSlider and a couple of buttons. It is undecorated and not modal. (I disabled modality for testing purposes -- it was a pain to force exit the app every time the dialog blocked input.) I'm entering full screen mode using setFullScreenWindow(), passing in the main JFrame for the app. It doesn't make a difference if I set that very JFrame as the owner of the JDialog or not. Nor does it seem to help if I call toFront() on the dialog.
The dialog seems to be active -- especially since it blocks input if I make it modal -- but just not showing or being hidden. So, is there any obvious trick to displaying a JDialog in fullscreen mode? Something I might be overlooking or omitting?
If there's no obvious solution, I can post some code later. Unfortunately, I don't have time right now.
JOptionPane.showInternalXXXDialog()
methods render dialogs as JInternalFrames.
Maybe you could consider using a JIternaFrame to simulate the dialog box.
And in fact, as M1EK alluded in his answer and I mentioned in a comment, Java applications in full screen mode will not allow other windows to show over them. The Javadoc API for GraphicsDevice reads:
Windows cannot overlap the full-screen window. All other application windows will always appear beneath the full-screen window in the Z-order.
In the end, I reconfigured my application so that it doesn't enter full screen mode until a bit later. This still gives me a fairly class presentation at the start and allows my JDialog to function as it should. The transition to full screen mode is quick and smooth, even in the "middle" of my app.
Do you really want to be in full-screen mode for this app? That's more of a gaming feature - to get more direct access to the frame-buffer, I always thought. Have you read this tutorial:
http://java.sun.com/docs/books/tutorial/extra/fullscreen/index.html
Really seems to me not to be the best choice for a Swing app with child windows.
Try to use this. Is not an exclusive full screen but it is close enough.
setExtendedState(JFrame.MAXIMIZED_BOTH);
setUndecorated(true);

Mouse Listener Question

I am developing a screen capturing utility in Java & I want to capture any background window, when I click on that particular window. But I am not getting how to add mouseClicked event to background window. Can somebody please help me?
I may be way off base but if the other window is not a Java window then it should be outside the Java sandbox. To interact with it requires a native API which is anathema to Java.
Quite obviously as it is you can't interact with other application windows. It can be any random window in your case I presume. Therefore, your mouselistener approach is not correct.
Rather, try to approach it like fetching pixel information displayed on the screen. There is an awt package java.awt.Robot or something that could be used for your purpose. If you want to implement capturing of active window then see if there are java APIs to interact with O.S. and get information of current active window and it's pixel co-ordinates. The co-ordinates could then be supplied to the rectangle attribute that is used with java.awt.Robot APIs to define screen capture area.
If that window is not part of your application you can't do much with it.
Otherwise you just have to add the mouse listener to that window too.
What's your situation?
java.awt.Robot has a method createScreenCapture(Rectangle screenRect) to capture screenshots.
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Robot.html
however, to get the current active window you would have to use OS specific extensions (mostly via JNI)

Categories

Resources