My custom component is composed of three JTrees inside a JPanel. Only one JTree should be selected at a time, so I've added a TreeSelectionListener to each of them that calls clearSelection() on the previously selected JTree. (See here for more details).
That works fine, but I need to prevent the TreeSelectionListeners to trigger when a JTree is deselected. A simple way to distinguish a selection event from a deselection one would be more than enough.
Thanks in advance!
Just get the current selection from the tree and if it's empty, return.
Related
I have a non editable table, on which I'd like to display a row contextual panel (likely a JPanel). Somewhat like Gmail is doing: when moving the mouse over mail rows there's a simple tool bar showing up on that specific row.
Like in gmail the action of controls I'd like to display won't edit the values, instead they will use the value in the row to perform some offer work.
I have played with the following :
TableCellRenderer, the display mostly works, but it has limitations:
the component is only used for rendering, so one cannot use it to simply add multiple buttons
it requires another column
for the hovering behavior (ie display on row only when the mouse is hovering the row) it requires collaboration with the table's MouseListener
TableCellEditor, my table is not editable so the cell editor is neither called
it also requires a specific column
it also requires collaboration with the table's MouseListener
MouseMotionListener can be used to display a popup for certain coordinates
the popup feels like it's the right component for this
there's quite some code to handle the popup lifecycle (closing it when the mouse move out of the row, don't re-open a popup if there is already one open)
tool tips: as far as I am aware the swing tooltips do not allow to have control components like buttons, etc
I did related question and answer on stack overflow. But they all require to add a column to display and use these swing components.
Given that you have posted no code, this question is a bit broad.
Nevertheless, the way to do it would be to stick a JPanel in a JPopupMenu. You need to create a listener on your GUI to know when and where the JPopupMenu should appear
--- Edit ---
I think you have to add JMenus to a JPopupMenu, and what I suggested about adding a JPanel won't work cleanly. You can either use JPopupMenu, or use a JWindow and put your JPanel in that.
When I run the application and I click on the JCombobox for the first time, the drop-down list looks like this
The second time I click, it seems to rearrange everything and all items are shown.
Any idea why this may be happening?
I would appreciate your help.
SOLVED: I was adding the JComboBox instance to the panel before adding the items to the JComboBox.
Check the height property, may be it is collapsing with other component. Design and placement of the component should be correct in swing to behave properly.
My program has the ability to change the selected item of my combobox. But how can I know if the item change has been caused by an human mouse click on the item itself or by my program.
I'm pretty much looking for a MouseListener that can be added to the items of the JComboBox and not the JComboBox itself.
A JComboBox is a compound component, and it is highly recommended that you avoid using low-level listeners such as a MouseListener with it. Instead why don't you disable your selection listener (perhaps you're using an ActionListener) when the code selects an item, and then re-enable it after the selection is done. Thus you'll know that any activity by the ActionListener is from a user's choice. You can disable and enable the listener by either removing and re-adding it, or by giving using a boolean variable that allows the listener to react only when the boolean is true.
I have a JTree on the left side of the frame. Each node in the tree has a corresponding panel that is to be displayed on the right side as the user clicks that node. It is not the same panel that is displaying different data. It can be entirely different panels. What is the best way to do this?
Use CardLayout to "flip" the panel that is being displayed.
See my answer on another SO question, which illustrates how to change the contents of a panel based on the selection in a JList and shows how to use a CardLayout.
Combine that sample code with the Swing JTree tutorial and a reference to the TreeSelectionListener interface (which is the equivalent of the ListSelectionListener which I used in my answer since that code uses a JList instead of a JTree) and you should be able to complete your requirement.
I have a UI with a Combo box. The list of items, which can be chosen, has to be refreshed every time the combo is about to open the list.
Is there any way - i.e. to add a listener which will inform UI that Combo is about to open?
Unfortunately I am not able to observe model to update the list when it changes.
Unfortunatelly there is no such method for SWT Components. In Swing it would be easy with the help of the PopupMenuListener Interface.
A workaround I can think of would be to implement a MouseListener and a KeyboardListener (As Comboboxes can be opened by pressing 'space') so you can at least update your Combobox List when those two Events take place.