I have many JLists(each within its own JScrollPane) arranged horizontally on a JPanel. This JPanel is in turn added to a JScrollPane of its own. I have set a TransferHandler on each JList which handles the drag and drop of list items via the methods canImport, importData, createTransferable, exportDone and getSourceActions(Only valid action is MOVE).
I can drag and drop list items from 1 JList to another. However, because I am unable to see all JLists at once, if I want to drag list item(s) from a JList to another that is not visible, scrolling horizontally is required. Thus, the problem arises when I am unable to scroll the scrollpane while dragging the chosen list items.
I have tried using MouseMotionListener mouseDragged method to no avail as no mouseDragged events are sent when I am dragging the list item.
Related
I would like to make a custom component that is much like a JList, except there is a little "x" button on the right side of each cell that removes that cell from the list (and triggers an event). I know that you would have to extend JList, but looking at the code for JList I have no idea where to go from there. For reference, I would like the list to be like on the macOS Messages app (except the "x" button can always be visible, not just when the mouse is over the cell).
I would like to make a custom component
I suggest you do that by extending JPanel and adding real components to your panel. Then you can actually add JButton with the "x" that can respond to the mouse event.
A JList does not display real components, only rendered images of the component and therefore is does not respond to events if you try to click on the "x".
The other option is to use a JTable. A JTable does allow you to display values in a column format. In this case it does support the concept of editors, which would allow you to add a button to a column. For example check out Table Button Column.
In my wizard page I am using gridlayout with 3 colums. The 3 column will have remove button. I am using add button for the composite. when i push add button it will add new row to the grid layout.
Now I am trying to add listener to remove button. When I push the remove button it should remove the row in which the remove button is pushed from the gridlayout and should resize the composite.
How to achieve this. And how to get the row index of the gridlayout?
To remove the controls completely you need to call dispose() on each control and then call layout() on the parent composite.
GridLayout does not make any information about the positions of the controls available so you can't really get a row index. It does sound like you might be better using a TableViewer to show a proper table.
I wonder if you guys had problem such if you got some JPanel which got his own JScrollPane and in your panel you are using a lot of JTables with their own JScrollPanes, there is a problem to scroll up/down your panel?
I mean when your mouse is on viewport of some table, then JScrollPane of JTable is listening on scroll, so when I got many JTables I am able to scroll only in a few places of the panel, it's so annoying...
Are there some functions which will send my scroll event to parent JPanel JScrollPane when the JTable scroll is even not shown? I mean I want to disable JScrollPane whenever some JTable don't need to use scroll (when it is hidden, cause there are too less records).
Yes, JTable understands several named scrolling actions, listed below. They are normally used in key bindings, but you can evoke them yourself, as shown in this related example that commandeers the actions defined for a scroll pane.
Addendum: In outline, get the named action from the component's action map:
Action action = table.getActionMap().get(name);
Evoke the action by name when needed:
action.actionPerformed(new ActionEvent(table, 0, name));
Scrolling related action names for JTable:
scrollDownChangeSelection
scrollDownExtendSelection
scrollLeftChangeSelection
scrollLeftExtendSelection
scrollRightChangeSelection
scrollRightExtendSelection
scrollUpChangeSelection
scrollUpExtendSelection
When I try setting the JList visibility to false to hide the content, I discover that the list is not hidden entirly when I scroll down. I have tried validate() and repaint() of JList, JScrollPane and JPanel (MainPanel), but no changes, I have also tried this:
jScollPane.setVerticalScrollBarPolicy(
javax.swing.ScrollPaneCantants.HORIZONTAL_SCROLLBAR_NEVER);
Also, no results. Some elements of the list remain visible if I scroll down the scroll bar.
I see at least two ways to do that:
Remove the JList from the scrollpane: scrollPane.setViewPortView(null);
Set the model of the JList to an empty model: list.setModel(new DefaultListModel());
Another alternative is to change the scroll mode of the JViewPort:
scrollpane.getViewport().setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
but of course this is not efficient at all. If you use that solution, when you make your component visible again, make sure to also properly reset the scroll mode to BLIT_SCROLL_MODE or BACKINGSTORE_SCROLL_MODE which are much more efficient.
I would like to make JComboBoxes scrollable by dragging their contents. It makes sense for a touch-screen app. I think I could manage to do it if there was a way to programmatically scroll a JComboBox. Is there?
I'm not sure I understand the question since the popup of the combo box is scrollable by default.
However, in general, to scroll a component added to a scrollpane you would use the scrollRectToVisible(...) method on that component.
The combo box popup uses a JList to hold each item. You can access the JList using:
Object child = comboBox.getAccessibleContext().getAccessibleChild(0);
BasicComboPopup popup = (BasicComboPopup)child;
JList list = popup.getList();