How to add close button to all tabs but one in JavaFX? - java

I am trying to create a tab system, with similar function to tabs in Chrome, where there is a cross on each tab to close it, like this:
With JavaFX, I can get close, by setting the tabpane closing policy to ALL_TABS. Unfortunately this means my new tab button (also a tab iself) can be closed:
I am aware of the SELECTED_TAB rule, which would fix this problem, but this would defeat the purpose. I am not aware of any other closing policy that would allow exceptions to the ALL_TABS rule.
I tried adding my own cross to each tab individually using the setGraphic method for the Tab class, however I couldn't figure out how to handle that mouse click event such that it closed the correct tab.
I realise I could also make the new tab button something other than a tab, but I wouldn't really know how to integrate that with the tab pane.
So, is there a simpler method that I'm not seeing? If not, then how can I achieve this kind of tabbing system in JavaFX?

plusTab.setClosable(false);
I did a simple skim of the documentation. Will this work?

Related

Context menu on Pane that disappears correctly

I am trying to have a JavaFX Pane (VBox in my case, but I don't think it matters) that has a ContextMenu that behaves correctly.
I have found these two questions: why Panes can't have ContextMenus and How to create ContextMenu within a Pane.
The problem I'm having with these two solutions (which are very similar) is that while the context menu correctly disappears if I click on the pane, it doesn't disappear if I click inside a control within that pane. The simplest way to observe this flaw is to create such a pane with a TextField. Right-click on the pane to show the context menu, then click inside to TextField to focus on it. While a proper context menu would disappear at this point, this "hacked-in" context menu (for lack of a better term) happily stays in it's place, possibly blocking the user's view of the text field they are trying to fill.
Now, I know I can add a change listener to the focused property of each and every control on my pane, but that feels redundant. Is there a better way to make sure the context menu is hidden when a control in my pane is selected (or, more accurately - when the user click the mouse anywhere in the owning window outside the context menu)?
What I tried so far and doesn't work -
Adding a change listener to the pane's focused property - it appears the pane isn't considered focused if one of it's children is
Adding a change listener to the context menu's focused property - it appears the context menu's focus isn't changed when clicking outside of it.
Ok, so after some digging in the source code for JavaFX I have found this workaround (which is what "solves" this problem for normal controls). Simply add this line of code -
contextMenu.setImpl_showRelativeToWindow(true);
Now, I know using internal implementation methods is discouraged as they may disappear, but this is the only solution I have found. If anyone has a better solution I'll be glad to hear it, but I suspect this is simply a bug that should be filed (i.e. - there should be a way to use the showRelativeToWindow mechanism when setting context menus on panes).
I guess a somewhat safer solution would be to have a throwaway control (not in the scene graph) on which to set the context menu, but I don't know if this would have any unwanted side effects:
Label throwaway = new Label(); // No special reason for using Label, could be any Control.
throwaway.setContextMenu(contextMenu); // note that this is the only place `throwaway` is used, it is never added to the scene graph
// or referenced again, but just setting the context menu on a control solves the problem.
Edit
After digging some more I have found this, and a solution in the discussion - you should call the show method overload which takes a window, not a node! Not very clear, but it works:
myPane.setOnContextMenuRequested(event ->
contextMenu.show(myPane.getScene().getWindow(), event.getScreenX(), event.getScreenY())
);

Java - Extra Window Button

Google Chrome Recently had an update that added an extra button onto to the top of the window that allowed you edit your account settings. I have a great use for an extra button like this one but I do not know how to make it. So, how can I add an extra button at the top of the window?
This is what I would like to do or have in mind.
This is more of an amateur/simple answer but it could be possible just to make a title bar with all of the drop downs without text until you reach the button you want and customize that

JButton inside JMenuItem

I'm trying to build a particular JMenu.
I want a JMenuItem with JMenu functionality, I.E. when we click it the item should do something (like opening a dialog). But the JMenuItem should also contain a button (or other component) that when we click it, should open a popup with a couple of options.
So, till now I have something like this:
That is what I have before click the arrow.
My problem is that, when I press the button (arrow), the sub-menu is actually opened, but the menu item that contains that button closes because loses focus.
That is the result after clicking in the arrow button.
Is there any way to manage this? Or a better way to have this behavior?
We can guide you if we know exactly what you are trying to implement. If you just want to select an option, you can implement that in better way with the JRadioButtonMenuItem,so you dont really need to implement a button and then select an option.But it depends on what you really want.
That's not what a menu is intended for. Use a ribbon instead, and these things will be easy and natural.
Why not just use simple nested JMenuItem instead?
Something like this (First screen from the top).
On the other hand you can benefit from a similar solution described here.
Couldn't actually find a solution for this particular problem.
As a workaround, I used just a simple button that toggle between the option 1 and option 2, instead of having the button (arrow) that open a new popup.
Thanks a lot for your help.

Should I create a new window or modify the old one?

I am programming a GUI application in Java. I do it for the first time.
I would like to have a form (with radio buttons and so on). After the form is filled in and the "Submit" button is pressed I would like to have a new window. I see two potential ways to do it:
Close the "old" window and open a "new" one.
Remove "old" elements from the existing window and put there "new" elements.
What is the standard way to go? If it is the first way, what is the command to close the window? If it is the second one, how can I remove elements from the existing window?
What you should do is create new JPanel for all the windows you want to show, then remove (or hide) the panel you want to hide and add or show the one you want to show.
I don't know too much about Java so I can't answer to your specific questions, but I want to remind you of the window opening/closing effect since Windows Vista: It looks kind of weird in some older setup wizards where everytime you click next the window fades out and in...
I think the most logical way is to have 2 objects("Close the "old" window and open a "new" one")
Anyway, I suggest you make an abstract class with the common elements, and then extend it with Window1 and Window2.
Java frames are destroyed with the dispose() method.

Java Menu issue

I have a menu with a few JCheckBoxMnuItems. How do I ensure that the Menu stays open until I have done all my selections (i.e. checked the menuitems) and does not close on just clicking one of them?
I'd rather not try to change the normal menu behavior for an application or for a part of the menu tree. A User expects that the menu closes automatically after a menu item is clicked. And, if you kept the menu expanded, what kind of action would you invent to close it manually after you've done your last selection?
If there's a requirement to change more then one setting within one use case, then you should consider to provide a small dialog where the use can apply the changes and confirm them at once. I believe, that's more consistent with typical behaviors of UIs.
And it declutters the menu bar, you'll have just one 'setup' menu item instead of a dozen (?) check box actions :)
I guess menu's aren't supposed to allow multi-selection.
But you may offer keyboard shortcuts to set the menuitems without using the menu at all.
If the set-operation of your flags is a central aspect in your application, I would tend to use a dialog here. These are all suggestions which do not require to change the internal implementation of the existing controls, even though I know, that it would be possible in swing.
I agree that it is better to do this with standard UI. However, if do you want to add checkboxes that do not close the menu it is surprisingly easy:
JCheckBox checkBox = new JCheckBox("Text");
checkBox.setOpaque(false);
checkBox.setRequestFocusEnabled(false);
menu.add(checkBox);
This may not work on every look and feel and the check boxes will not line up with menu items in the same manner as JMenuItems but it seems to be a reasonable place to start.

Categories

Resources