Show/Hide JMenuItems in a JPopupMenu based on a JTree's selection - java

I have a JTree which displays a JPopupMenu when I right click nodes in the JTree. What is the best way to hide/show or enable/disable certain JMenuItems in my JPopupMenu based on the node selected in the JTree?
The first solution that came to mind was to add a TreeSelectionListener to the JTree for the 'value changed' event. In the event handling code I would use the TreeSelectionEvent's getNewLeadSelectionPath() method to get the path of the most recent selection, and use the resulting TreePath object's getLastPathComponent() to get the selected node. From here I would have a series of IF statements that access my JPopupMenu object and perform the modifications necessary to hide/show specific JMenuItems.
However, something feels off about this, and so I decided I would ask SO if there was a better approach.

The way that I chose to tackle this within my own app was to use the "userObject" property of the DefaultMutableTreeNode class which allows you to just store any data you want along with your node. I have a variety of types of things that extend from an abstract base class which defines a "createPopupMenu()" method. Then, in the selection listener (just as you described in your question) I get the user object and ask it to create a popup menu appropriate for the selected object and display that.

Getting the selected tree node is straight forward and should work as you described it. To modify the popup menu I would recommend using Actions. This way you wouldn’t have to modify your live menu and could also add e.g. a JToolBar that contains the same actions that react the same way the items in your menu do.

Related

Is there an way to remove a JTextFiled if a certain item in JComboBox is selected in java?

I have a registration panel where if student, instructor, and course administrator can register so if student is selected it should show something like this when Student is selected:
and should show like this if other two are selected like this when any of other two are selected:
I tried using if condition on the selected item in where I have added those text fields but it seems it only works at the beginning of the program when I run it on the basis of what is pre-selected and does not change when I select other items in JComboBox. Is there a solution to this?
You can achieve this in different ways. One of such ways is to use Action Listeners. A JComboBox object generates an action event when a selection is made (see Handling Events on a Combo Box).
In your case, you need to trigger an event based on the selection made in a combo box. This action should change the visibility of components in your panel, which are simply changing the visible attribute from true to false (or vice versa) depending on the selection made.

Get all selected checkboxes on button click [duplicate]

I have a dialog in Java that presents ~ 15 checkboxes to the user. Is there a way to get the names of all the checked checkboxes at once? Currently, I'm looking one by one if they are selected, which isn't that fancy of a solution.
I'm looking for something similar to Getting all selected checkboxes in an array but then in Java
When you are adding your Checkboxes to your dialog also keep a reference in a Collection of some sort. Then when you want to see which are checked you can just Iterate over the collection and check the state of each of them. You can get the name by calling getText on it.
List<JCheckBox> checkboxes = new ArrayList<JCheckBox>();
for( Component comp : panel.getComponents() ) {
if( comp instanceof JCheckBox) checkboxes.add( (JCheckBox)comp );
}
This assumes all of the JCheckBox instances are a direct child of the container panel. If not then you'd need to recursively visit all the containers of panel using the same logic. Now, while you can do this it's typically better to save these references as you created them into a list. Then you can easily iterate over all of the checkboxes without having to do this code above. If you have embedded components it's better to ask the embedded component to perform whatever operation you want over the checkboxes it owns (as opposed to pulling them out of the component through a getter so you can mess them in some way).

Prevent selections of the current selection from notifying listeners in JComboBox

I would like to prevent an event from being fired when a user selects a value already selected in a JComboBox.
For instance, assume I have a JComboBox whose model has the following values:
Cat
Dog
Fish
Bird
Snake
The currently selected value is "Cat". I would like to prevent an Listeners from being notified if the user selects "Cat" again, whilst "Cat" is already selected.
I have tried to implement this by adding a check in the setSelectedItem(Object) in the model. This however did not work.
My next assumption is that if I want this functionality, I will need to subclass JComboBox and override it's setSelectedItem(Object) and contentsChanged(ListDataEvent) functions.
Given the documentation for contentsChanged(ListDataEvent) however, I am hesitant to override it. As such my question for all of you:
Is there a better way to get this desired functionality that doesn't require sub classing JComboBox and overriding it's setSelectedItem(Object) and contentsChanged(ListDataEvent) functions?
I would like to prevent an action event from being fired when a user
selects a value already selected in a JComboBox.
use ItemListener,
wrap code into if (e.getStateChange() == ItemEvent.SELECTED) { as is shown, described in Oracle tutorial
for example

Without extending JTree is there anyway to force the update of the model after changing an object in the tree?

I have a JTree that stores "ShipmentItem"s and the .toString() on them shows the quantity, then the name of the ShipmentItem. At some point I change the quantity of multiple items at once but the toString() doesn't refresh until I've actually clicked on that particular tree node. I don't want to have to extend JTree to use 'Property Fired' I just want to be able to refresh it so that it shows the update.
I tried jtree.setModel(tree.getModel()) this didn't seem to work at all.
When you change something to your TreeModel (which you do by adjusting the ShipmentItem objects) you must make sure your TreeModel fires the correct event. This will cause the JTree to repaint the correct part. If you for example started from the DefaultTreeModel, your extension should call nodeChanged when the object of the node has changed.
Note: you do not have to adjust the toString method for correct rendering. The concept you are looking for is a TreeCellRenderer (check the Swing tutorial for more information)

Interfering Keylisteners in java

I have a JXTree and I'd like to add searching to it this way:
As the user types, the model returns the matching elements of the tree and selects the first one of the set in the tree's view.
The problem is, that by default the JXTree has an other keylistener, that selects an element starting with the currently typed letter.
Sometimes the native listener fires last making the outcome wrong. What can be done to prevent this behavior? I don't want to remove the inherent listener because it has arrow based navigation... (Which I have to reimplement.)
I've already read this: Is the order in which KeyListeners will be called guaranteed?, but I don't think that I can create the proposed listener wrapper without great effort, since BasicTreeUI's Handler class is private.
That's not a feature of JXTree but JTree. Overriding JTree#getNextMatch() to always return null should disable the JTree selection on key press.

Categories

Resources