I have a JTable that when clicking on the row expands it and filters all other rows in the table. When the user clicks on this expanded row again it returns to its normal size and the filter shows all the table again. All this works fine but when returning from the view of a single expanded row to the whole table I would like to set the scroll pane vertical scroll bar value to that one which is the position of the row that have been expanded. I correctly compute this value and try to set it with
scrollPane.getVerticalScrollBar().setValue(value);
but as I have found with scrollPane.getVerticalScrollBar().getMaximum() the scroll bar doesn't update its values after sorter.setRowFilter(filter); invocation. getMaximum() returns the height of that single row that have been expanded and for that reason scroll bar fails to set the updated value (that is typically much greater). This is the code:
// after this call the table shows all its rows again
sorter.setRowFilter(filter);
System.out.println("maximum=" + scrollPane.getVerticalScrollBar().getMaximum());
// shows old value when the table has consisted from a single expanded row
scrollPane.getVerticalScrollBar().setValue(value); // fails
Can anyone tell me how to let the scroll bar to know that the view of JScrollPane has changed so it can correctly set its maximum value? (though the scroll bar itself seems to be ok and looks like it has look properly scrolling all table). I have tried to call revalidate and updateUI methods on the JTable but it doesn't help.
scrollPane.getVerticalScrollBar().setValue(value);
Try wrapping the above code in a SwingUtilities.invokeLater(). This will add the code to the end of the event queue.
You need to revalidate the JScrollPane, but only after you invalidate the JTable. Thus, something like
sorter.setRowFilter(filter);
sorter.invalidate();
// shows old value when the table has consisted from a single expanded row
scrollPane.revalidate();
Related
I have a custom table class which extends JTable. I want a column to be visible/invisible based on a checkbox. I have an actionlistener on the checkbox that changes the max, min, and preferred width of the column in action.
When the checkbox is NOT selected, then I set the min/max/preferred width to 0. When it is selected, I set the min/max/preferred width to a normal size.
My issue is, when I click to select the checkbox, the table does not change. I do not see the column. However, when I resize the application or any of table columns, the table gets updated and my new column is now visible properly.
I tried calling "table.repaint()", and that did not do anything. My ideal approach now would be to just figure out what gets called during an application/column resize that causes the update, and just call it from my listener.
I have an actionlistener on the checkbox that changes the max, min, and preferred width of the column in action.
Don't play with the column size. The column will still be part of the table and will confuse the user as they tab from column to column since the cursor will disappear on the minimized column.
Instead you want to remove the column from the view of the table. So you need to remove a TableColumn from the TableColumnModel of the table.
Check out the Table Column Manager. This allows you to hide/show columns as required.
Well my question is fairly simple, I have put a JTable in a JFrame, but I want that the table to automatically resizes according the size of an ArrayList or to have a side scroll bar
For example if I do something like this:
for (int i=0;i<list.size();i++){
jTable1.getModel().setValueAt(list.getVar(i),i,0);
}
If my ArrayList has 10 elements and my JTable was set up to 4 elements I got the arraybound error. Is there a way to put a scroll bar or something in the JTable so it can shows all the rows that I want (depending on the size of the ArrayList of course)?
Set the number of rows in the table before you run your code:
((DefaultTableModel)(jTable1.getModel())).setRowCount(list.size());
DefaultTableModel documentation
If you want to put the JTable in a scroll box, when you add it to the container you have to say
container.add(new JScrollPane(jTable1));
instead of
container.add(jTable1);
Is there a way to make, let say first 3 out of 7, columns fixed in Cell table. I want to be able to see always first three columns and have horizontal scroll on others.
You will have to create a custom widget that consists of a ScrollPanel, which includes two CellTable widgets set side by side. The right table should be wrapped in a FlowPanel with overflow-x property set to AUTO (overflow-y should remain at HIDDEN).
You can use the same DataProvider for both tables to synchronize all changes. Be careful with the SelectionModel though, if you need it. I would limit selection to the first column of checkboxes and disable selection by clicking on a row.
Make sure that your widget fits into its space, or you may end up with two horizontal scrollbars - one for the ScrollPanel and one for the right table. Finally, remember to set sizes on both tables so that they have the same height.
one question with the java jTable class. Actually I am not a Java programmer and just now using Java to design a GUI in Matlab. What I've done is:
A jTable is built into a Matlab GUI.
I used/called a RowFilter in jTable, which can make the jTable to show the filtering results.
Then from the results in this filtered view I used the removeRow method from table model to remove one or several selected rows.
The problem is that everytime if I remove a row, the table content refreshs itself as wanted, but the scroll bar jumps back to the beginning.
Does anyone know how to inhibit this jumping and keep the original view of jTable? Because this helps me not to have to scroll back to find the original position where I started the deleting.
Thank u for ur advice and help.
Invoke the table's scrollRectToVisible() method; pass it the Rectangle returned by getCellRect() for the desired row.
How can i keep no of rows constant in text area.
I need to create a console window for my application.
If rows exceeds predefined no of rows first rows must get disposed.
As if first written row will be destroyed first when i append anything which exceeds no of rows set.
One more thing , i need to keep vertical scroll bar. That means no of rows must not be the whatever rows are visible when text area it opened.
For example : - no of visible rows on view port are 30.
It should keep 120 rows information, which will can be seen with the help of scroll bar.
Sounds like what you want to do is create your own Document implementation. See Document.
The Message Console shows one way.