Summary: make invisible app visible again by clicking desktop icon.
I have created a java desktop application and used Launch4j to create the executable. My application needs to run as a single instance. I have achieved this my checking that option in the Launch4j configuration GUI. When the user clicks the x button on my window (I'm using a JFrame) the window is made invisible. It is NOT disposed. When the user double clicks the system tray icon, the application window is made visible again. When the user double clicks the application desktop icon, and the application is already running, I need my main window to become visible again just like the behavior with the system tray icon.
Launch4j provides an option for a window title under the Single instance tab. I think that this option may be intended for what I need. However, when I set a window title and carry out the steps described above, I get a blank white window. With the blank white window displayed, double clicking my system tray icon will cause my app to appear in the white window. This behavior suggests that maybe changing my code relating to the top level container (Window,Frame,JFrame) might have an effect.
Do you have any ideas on how to incorporate this behavior while still using Launch4j? (it's convenient).
edit:
The core of my question is how do I define what happens when the user double clicks my executable desktop icon? If I could check for an existing instance and redirect the double click to the associated system tray icon, that would be fantastic.
I was able to accomplish my goal. In the Single Instance tab I gave the title of my main JFrame in the field for "Window title". I then added a WindowFocusListener to my main JFrame. When my JFrame receives focus, I check to see if it is already visible. If it is not, I call the appropriate method for displaying my JFrame.
The key observation is that double clicking the exe icon generated by Launch4j fires a window focus event when a window title is given in the setup. I am not sure if that is the only event that is fired, but by listening for that event, I can take whatever actions are necessary from within my java code when the user double clicks the desktop icon.
Related
As we all know, when a user double-clicks on a window title bar, that window gets resized to the full available screen size. At least I have seen this happening in Mac OS. Any idea how to capture this event? What should I listen to in my code so that I know that the user has single or double-clicked the Window title?
Don’t try to intercept interactions with Window decorations directly
Don’t try to work with clicks. Handling state changes due to keyboard or window decoration interactions differs between OSes and isn’t a JavaFX function anyway.
Use Stage properties and API instead
Stage has a maximized property (and iconifed), listen for changes on relevant properties. Similarly, there is a full screen mode which is a bit different (see Stage Java doc).
Creating your own window decorations
If you really want complete control, you can use an undecorated stage style and add your own decorations in JavaFX for handling basic window system functions. That way, a lot of the decoration functionality can be handled internally by your app, but I really don’t recommend that. There was an old project named Undecorator which assisted doing this if you wanted to go that route (I don’t advise coding it yourself).
Capturing clicks is a bad idea as #jewelsea mentioned in his comment. The effort in the question was to know why the behavior. Upon research, I found that it is a bug already listed by Oracle - please find below: Each window when double-clicked on the title bar is expected to assume full-screen height and width (Maximised) and on double-clicking title bar again should get back to its previous size. This is not working beyond JDK8 is what I understood from this link. However, on macOS Monterey 12.1 (OpenJDK 17) the window is assuming maximized size on double-clicking the title bar, but, is unable to go back to the previous size when double-clicking again.
https://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8232812
First off I am using Intellij IDEA's GUI tool.
I have a window that has a browse button, if the user clicks the browse button they can explore their computer for an image file. If they choose an Image file then the window will add that image to the screen next to the previous image, If the window just barely shows all the images and the user adds another one, I want the window to expand to be able to show the new image.
Does Java Swing have this capability? If so, how should I go about implementing it?
If you are using appropriate layout managers, you can simple call Window#pack.
You should also consider checking out How to use scroll panes, cause I have some very large images...
The other thing you can look at is the Scrollable interface
I've set my program to appear in the system tray by doing what Oracle told to do. My first question is about the icon. I have a lot of programs in the system tray and mine is hidden. Can I make it to show in the bar without needing to click the arrow in the tray?
I also figured out that I can display a message by calling trayIcon.displayMessage(title, content, icon). I wonder if I can change the outlook of the balloon in the way Skype has done it.
Or do I need to use someting else do display a message? It should appear always in the front of all the applications and it shouldn't hinder other applications. For example if the user is playing a game, the information dialog shouldn't steal the focus from mouse and keyboard.
No you cant change the style of the baloon using the java systray mechanism. Skype doesnt use the java mechanism to show the systray. It is the systems task to style and display the baloon.
To show your icon, it is a windows configuration - when you click the arrow, there is a "customize" link, where you can configure which icons are displayed.
If you want to influence the style of the window, you need to implement your custom Frame that feels like and is positioned like a systray info window. And you would not use the Tray classes.
Concerning your question regarding skinning. The SystemTray displayMessage balloon can not be customized in any way.
I am creating an desktop application that runs at background and while clicking the control button it should be visible i have made the setVisible(false) to the JFrame any idea how to do it . The application should triggered if you click the control button twice in desktop or in any application this should work.
This can't be done in your application.
If it runs in the background, it doesn't have the focus, so it can't react on that event. Any other application might have the focus and being interested in consuming the Ctrl button.
Think of multiple such programs, all in concurrence for that button. Which should get informed? Think of 3 Editors, all getting the next character typed.
Your OS or DE might have a way to define a hotkey, look whether your program is running (once?), and send a message to your application. The application might then react.
Are there any current implementations or frameworks for Java Swing that include functionality for a context-switcher menu?
More detail:
In our application, we have several sub-parts of the application, and only one of them is displayed at once. Presently there are several ways to switch between them, including tool bar buttons and via the View menu. We would like to add another means, that is accessible via a keyboard shortcut. This would bring up a context-switch menu, similar in concept to those available in modern OS'es.
If you press Alt+Tab and release the Tab while still holding down Alt, you will get a little window in the middle of the screen, displaying the various applications that are running at the moment. In Ubuntu, you get a screenshot of each application, plus its window manager icon. In Windows you get the window manager icons, and so on.
I think this is possible. You could apply a transformation to a Graphics option that you pass to each JFrame and have it paint a small version of itself on it. Then take those images and place them on a GlassPane on top of your application. The highlighting of the selected window might be tricky, but I think it would work nicely.