JScrollPane in JTables - java

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

Related

How to force repaint of JTable, JScrollPane?

I'm creating a top-down parser, that checks the syntax of a given source code according to a given grammar, then displays the result, within a simple GUI.
Basically, the user selects the grammar and source files, clicks "Check Syntax" and the result is displayed like in the picture...
And it initially works fine. However, when selecting different files or updating a selected file, then clicking the "Check Syntax" button again, some graphical issues start to occur after interactions such as scrolling, resizing, or clicking rows:
The issues are more apparent when resizing the window:
Here is the structure, of the frame:
JFrame frame
JPanel headerPanel
...
JPanel bodyPanel
JLabel tableTitle
JScrollPane tableScrollPane
JTable table
JLabel tableSummary
I've tried methods such as revalidate() and repaint() on components where issues happen, but they only fixed those of labels...
I've also tried using a SwingWorker but it didn't solve the issue.
Here is the code portion that might be relevant:
...
var tableScrollPane = new JScrollPane(table);
tableScrollPane.setBorder(new MatteBorder(0,1,0,1, palette.get("strongTeal")));
tableScrollPane.setBackground(palette.get("strongTeal"));
tableScrollPane.revalidate();
tableScrollPane.repaint();
...
var bodyPanel = new JPanel();
bodyPanel.setLayout(new BorderLayout());
bodyPanel.add(tableTitle, BorderLayout.NORTH);
bodyPanel.add(tableScrollPane, BorderLayout.CENTER);
bodyPanel.add(tableSummary, BorderLayout.SOUTH);
bodyPanel.setBorder(new EmptyBorder(45,45,45,45));
bodyPanel.revalidate();
bodyPanel.repaint();
frame.add(bodyPanel, BorderLayout.CENTER);
frame.setVisible(true);
...
Any ideas on how to force the frame or target components to redraw completely (that is to forget about previous state, and only show the newest information when scrolling, resizing, etc...), ultimately overcoming these issues? Thank you.
. However, when selecting different files or updating a selected file, then clicking the "Check Syntax" button again, some graphical issues start to occur
Don't keep creating new components. The old components are still added to the frame. So now you have two sets of components.
Instead, you can replace:
the data in the JTable by using table.setModel( yourTableModel )
the component in the JScrollPane by using scrollPane.setViewportView( yourTable )

JTable only showing after I resize the JPanel in Eclipse

I have a button called btnDisplay on a JPanel called TempPanel. When the button is clicked, it should display a JTable that is created manually.
However the table is only visible to me after I resize the panel manually with a mouse. Even if I make the panel smaller than it originally was, it shows the table, otherwise it doesn't.
What is the reason for this? And how can I fix it?
I'm writing the comment as an answer for clarity purpose:
Call revalidate and repaint on the container to which the JTable is been added

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.

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.

Grab and drag JComboBox scroll

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

Categories

Resources