JavaFx: StageStyle.UTILITY without close button - java

I need to use StageStyle utility because I need to hide window icon in taskbar. However, I also need to hide and close button. How to do it? Or maybe there is another solution - no window title bar + no icon in task bar?

This answer is more of a general one: The core problem is that JavaFX doesn't allow you to hide the taskbar icon. So I guess you really don't want to use a Utility StageStyle, but rather are forced to.
Swing allows you to hide the taskbar icon. So the hackaround is simple: Use JavaFX inside a Swing JFrame and hide it from the taskbar.
You can take a look at the widget code in the answer here as an example.

Related

JavaFX dialog without icon

I don't know how to create a dialog without any icon in JavaFX. I've tried
((Stage)dialog.getDialogPane().getScene().getWindow()).getIcons().clear();
but it still leaves the ugly default icon (on Windows 10) of dialog. Is there any better way than setting empty PNG as icon? If so give me the hint.
stage.initStyle(StageStyle.UTILITY);
When you want to get rid of icon you may switch window style to make only content visible without closed, minimize, etc, buttons and also without icon.
javadoc Dialog class
dialog.initStyle(StageStyle.UNDECORATED);
Specifies the style for this dialog. This must be done prior to making
the dialog visible. The style is one of: StageStyle.DECORATED,
StageStyle.UNDECORATED, StageStyle.TRANSPARENT, StageStyle.UTILITY, or
StageStyle.UNIFIED.

How to add custom menu action and titlebar icon to JFrame

I have a custom JFrame. On the title bar I have an icon in the top left, a title, and then the standard minimize, maximize, and close buttons on the right.
When I click the icon in the title bar I get the standard options: Restore, Move, Size, Minimize, Maximize, Close.
How can I add my own menu option here? I'd like to add and "Always On Top" option here.
Additionally I'd like to add a button next to the max,min,close buttons on the title bar to allow uses to toggle the "Always On Top" state of the JFrame.
You could create your own customized Components. To do that, create a new class which extends JMenuBar for example and override the methods which fit your needs. Very often, for example, one wants to override paintComponent(Graphics).
That is not what you want, 'though. Customizing the JMenuBar wont work as you expect it to. The "JMenuBar" is another bar below the title bar. I am Mac user, but as far as my knowledge goes, it is not possible to customize the title bar, because that isn't handled by the JVM. The only thing that is modifiable without using native code is the Icon in the top left.
For further information on that, look at this question and the best answer there. This will help you a lot.
Your problem (adding a button at the top for toggling the alwaysOnTop status) is best solved by creating normal instances of a JMenuBar, a JMenu and a JMenuItem.
To then add that MenuBar to your Frame, use JFrame.setJMenuBar(JMenuBar). See also How to use Menus.
I hope this helps!

Java Swing - user alerts

I am trying to build a user alert mechanism by bringing up the window to the front and then flashing the icon on the screen for the user. I have two questions with regards to this approach:
How can you find the current window you are at in Java and then de-minimize it and bring to front?
Is there a mechanism in Java that would enable me to simply show the icon for a second or two and then hide it, in the middle of the screen? If not, what would be the way to achieve that?
Thanks a lot for any replies.
How can you find the current window you are at in Java and then de-minimize it and bring to front
Window[] allWindows = Window.getWindows();
returns arrays of all Top-Level Containers from current JVM e.g. J/Frame, J/Dialog(JOptionPane), J/Window,
you can to test for (example) if (allWindows[i] instanceof JFrame) {
then WindowState returned WindowEvent
by bringing up the window to the front and then flashing the icon on the screen for the user
use undecodated JDialog (works toFront, toBack) with
create only once time
setDefaultCloseOperations(HIDE_ON_CLOSE)
use Swing Timer for hide JDialog
Is there a mechanism in Java that would enable me to simply show the icon for a second or two and then hide it, in the middle of the screen? If not, what would be the way to achieve that?
have look at Java Translucent Window, put there Icon to the JLabel (or to the JButton)
use Swing Timer for flashing by hiding Icon or swithing bewtween two or more Icons (three or four is good)
I think the simplest way to get the window ancestor is :
SwingUtilities.getWindowAncestor(yourComponent);

How to disable iconified button in Menu bar in JFrame Window?

How to disable iconified button in JFrame Window ?
something like setResizable, but for minimize button
At First, you can use the method setUndecorated(boolean). It may disable the title bar and the border.
In the end, you will create the icon label and close button at your frame top or the others position.
But this way will lose the border look and feel for the frame. If you choose this way, you must create a lot of code.
In fact, If you could not use JNI, this way may be the only.
You could use a JDialog, which natively does not have a minimize button.
In fact, the minimize, close and maximize/un-maximize buttons are drawn by the Operating System itself. This means you can't really disable them within Java.
That's why my suggestion is to use a JDialog.

Using standard buttons from a Look and Feel

I am making a UI with Swing, and I want the buttons I am using for my custom dialogs to have the same style as the ones in standard dialogs.
For instance, in the attached image I have a custom dialog and the standard file select dialog. I want the 'OK' and 'Cancel' buttons from the file select dialog to be used for the equivalent buttons in my custom dialog.
I want my application to use the default system look and feel of whatever OS it is running on, so I don't want to try to manually re-create these standard buttons. Using a more rigid Swing class that automatically provides these buttons wouldn't work either, as I'd like to also use them in other, more exotic places in my UI.
Is there an easy solution to this problem?
Sorry to be the bearer of bad news, but it look like there is no standard way to do this cross-platform. The behaviours, mnemonics and icons on default buttons are handled in very specific ways by each look-and-feel.
Here is a SO question that answers the question on how to set the default OK and Cancel buttons on a dialog (the default button is set using getRootPane().setDefaultButton(...) and the Cancel button needs a custom keyboard listener. If you're very lucky, setting the default button might add an icon to it, depending on how the UI is coded.
This forum thread addresses the issue of getting icon resources from the UIManager. Each LaF has a set of UI defaults such as colors, text, borders and icons. There are a number of default icons which are found across all LaFs, but for non-standard icons, such as ones on buttons, there are no guarantees. However, if you tell me which LaF you are using in the screenshot you provided, I can look up the resource keys used by its custom UI classes (or you can find it yourself if you have the source). You could then write a helper method which looks for the icons via these keys, and adds them to the buttons if they are found.
JButton.setUI(ButtonUI) sets the UI for just one JButton. Use that in conjunction with a factory:
public static JButton createStyledButton(String text) {
JButton button = new JButton(text);
button.setUI(STYLE_UI);
return button;
}

Categories