Grab and drag JComboBox scroll - java

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();

Related

How to Make a JList with "x" Buttons in Each Cell

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.

Unable to autoscroll JScrollPane when dragging a JList item

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.

JScrollPane in JTables

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

How do I get a JPopupMenu to "shadow" a component consistently

I have a custom auto complete JTextField. I use the JPopupMenu to contain the selection.
I want to pop the JPopupMenu right under the JTextField using this method.
Rectangle r = textField.getBounds();
popup.show(textField, (int)(r.getX()), (int)(r.getY() + textField.getHeight));
popup.setVisible(true);
It works when I put the component in a simple JFrame. But when I put the component in a complex layout with JScrollPane. The location becomes random and inconsistent. I'd like the popupMenu to "shadow" the textField in any condition. How do I achieve that? Thanks.
The popup location is relative to the parent component.
popup.show(textField, 0, textField.getHeight());
should place it right under the textField.

Hide JList within JScrollPane

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.

Categories

Resources