How to add custom menu action and titlebar icon to JFrame - java

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!

Related

JavaFx: StageStyle.UTILITY without close button

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.

Where in Java Swing Source is the implementation of the Enter Keypress on a menu?

I am mucking around with a hierarchical menu trying to make it scrollable. Yes, I know about Menu Scroller at the Java Tips Weblog, but it doesn't quite do what I want, so I've been mucking about with a stripped down version of it it and I'm not quite getting it to work.
Basically I want a JMenu with too many items to display on which the user can press the up and down arrow keys to scroll the menu. I have gotten tanatalizingly close to what I want but I have come to a hurdle which I can best describe this way:
When [ENTER] is pressed while a popup menu has focus, default behavior is to do the action associated with the selected item and dispose of the menu. If the menu is nested, popups above it in the hierarchy also close (become invisible). Where is this behavior coded? I've looked all over JMenu, JPopupMenu, JMenuItem, AbstractButton and I don't see what I am looking for. Where is the Swing source code that executes this common behavior?
If I knew the answer to that, I might understand why my implementation isn't working. I can do the action, but the menu and its parents won't disappear. I can make the menu disappear by setVisible(false) of course, but I can't walk the containment hierarchy to find the parent menus and make THEM disappear.
I can do the action, but the menu and its parents won't disappear.
I think you can use:
MenuSelectionManager.defaultManager().clearSelectedPath()
I'm not 100% certain for menus, but I know for JTextComponents that all of the keystrokes (copy, paste, enter, move forward by words/sentences/lines, deleting, etc.) are implemented via the InputMap and ActionMap. JTextcomponents also use Keymaps, but I'm pretty sure those are specific to text components.

Drag component to create another one

In all major Java IDEs, there is a GUI designer.
When we select a component (A Jbutton, for example) and move it to a JPanel or JFrame, how is it done?
Is it a copy of the dragged component that is created on the other container?
On a project I'm working on, I have some JButton I would like to be able to drag to a panel. Theses JButton represent some actions, like "copy file", "move file", etc...
When one of those JButton is dragged, some options of the action will be displayed.
I checked TransferHandler but I don't know if it's the way to go. Is it?
It's certainly possible. You'll need to study the Drag and Drop tutorial. In particular, you may want to implement Drop Location Rendering, discussed here, to symbolize the action.
By encapsulating a button's name, icon, listener, etc. in an Action instance, your importData() implementation can easily use setAction() to change the target button's behavior dynamically.
An alternative approach might be to add your buttons to a JToolBar. In normal mode, clicking the button evokes the Action; in editor mode , clicking the button changes the Action, again via setAction(), to one chosen from a list.

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.

How to move JPopup up to Glasspane

I am putting a combobox component on the glasspane for users to select from a list of items. When the drop down list is clicked though the JPopupMenu is hidden behind other parts of the component on the glasspane since the popups are displayed on the LayeredPane.
I would like to find out how to make the popup display on the glasspane with the component. I have tried JPopupMenu.setDefaultLightWeightPopupEnabled(false) before the frame was initialized but it seems that makes the popup not display at all anywhere and I am not sure why.
Any advice on how to get the popup to display on the glasspane instead of the jlayeredpane would be helpful. I searched but most responses seem to related to pushing events down that are captured on the glasspane.
I am actually using a JideAutoCompletionComboBox which extends JComboBox.
Edit for question: I have a system wide (my app has a bunch of workspaces on tabs) popup type system. I would like to not use a Modal dialog for this and just use the glasspane. The component is basically for creating a message but one of the subcomponents is a combobox. Effectively you can think of the whole component like a popup though, but using the glasspane.
I don't like little floating windows that users can screw up by pushing around.
JDialog dialog = new JDialog(...);
dialog.setUndecorated(true);

Categories