How do I avoid the array bound error with JTables? - java

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

Related

JScrollBar doesn't update its maximum value after JTable changes

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

java JList adding elements to model is changing visibleRowCount

I am using the default JList model for adding elements to a JList. Occasionally when I would add an element to the model it would change the JLists visibleRowCount from 2 to 1. It does not seem to matter weather I add one or 10 elements, it keeps changing it to only 1 visible row even if I try to force it back after adding elements. I am using a panel with a flowlayout to hold the Jlist and its scroll with that panel in a gridBagLayout

Removing unused Rows from jTable

How do I remove unused rows from a JTable?
I first create a table of a fixed size in a JPanel and then fill elements as needed.
Now I don't want unused rows to be displayed in my table. Please help.
I presently get a table with lots of empty rows in bottom, only top 5-6 rows being used, with rest blank. I want to hide them or remove them somehow
Work the other way around. Start with an empty DefaultTableModel. DefaultTableModel supports an addRow() method. So, as you get data to add to the model use:
model.addRow(...);
Don't let the GUI editor determine how you code your GUI. Adding cleanup code to remove rows is a bad design.
DefaultTableModel has a method removeRow which removes a row from the table. Pass the row index which you want to remove from the table.
Now I don't want unused rows to be displayed in my table.
This I am not sure please post an SSCCE to show us what is the flag that says these rows are not used.

restore jTable focus and position after removing a row from table model

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.

Initial state of autoCreateRowSorter in Swing JTable

I have this JTable on my Swing app with the autoCreateRowSorter enabled. My table only has 3 columns, two strings and one int, it works well for all of them when I click the column headers.
However, I'm looking for way to do it programatically. I wanted to set the "initial state" for this table. With the Windows look and feel, the column header (when sorted) has a little arrow showing the sort order. But at startup that doesn't show, I have to do one initial click.
How can I do that by code?
To programaticallly sort the table you can do something like:
DefaultRowSorter sorter = ((DefaultRowSorter)table.getRowSorter());
ArrayList list = new ArrayList();
list.add( new RowSorter.SortKey(0, SortOrder.ASCENDING) );
sorter.setSortKeys(list);
sorter.sort();
I think DefaultRowSorter#toggleSortOrder(int column) will do the work
You don't even need the DefaultRowSorter interface.
table.getRowSorter().toggleSortOrder(column);
works as well.

Categories

Resources