ScrollBar not updating (JTable, JScrollPane)
Hi,
I followed [url=http://www.codetoad.com/java_JTable.asp]this[/url] guide to understand how adding/removing rows in JTable works, however, when I add new row in my simple program, for some reasons scrollbar won't update -- here some sample code --
Method that fires when I press button (is supposed to add new row to table, and so it does):
privatevoid actionAdd(){
String data[] ={"A","B","C","D"};
Vector vec =new Vector();
for (int i = 0; i<data.length; i++)
vec.add((String) data[i]);
vecDataTop250.addElement(vec);
tableTop250.addNotify();
System.out.println("Element added.");
scrollPane.repaint();// new table content also wouldn't display if I didn't add this method (even tho it isn't used in guide above)
}
Creating JScrollPane in which JTable will be displayed:
public JScrollPane
createJScrollPane(String colNames[], Vector data,int colWidth[]){
tableTop250 = createTable(colNames, data, colWidth);
JScrollPane scroll =new JScrollPane(tableTop250);
scroll.setBorder(BorderFactory.createEmptyBorder());
scroll.setPreferredSize(new Dimension(370, 200));
scroll.getVerticalScrollBar().setUI(new MyScrollBarUI());// I use class extending BasicScrollBarUI -- possibly something to change there?
return scroll;
}
Anybody has idea what's wrong? I can post more code if that would help --
Thanks in advance,
Jimzy>
[2209 byte] By [
Jimzya] at [2007-11-27 4:28:34]

# 4
Okay, following those advices, new method adding rows to table looks like this:
private void actionAdd() {
String data[] = {"A", "B", "C", "D" };
Vector vec = new Vector();
for (int i = 0; i<data.length; i++)
vec.add((String) data[i]);
System.out.println("Element added.");
modelTop250.addRow(vec);
scrollPane.setViewportView(tableTop250);
}
It works -- adds new row of data to table without any repaints, notifys and so on.
However, it doesn't solve dead scrollbar thing tho. Also, mouse wheel scrolling doesn't work either... it looks like table (or maybe ScrollPane) doesn't know it's being updated.
Any guide on how to do it right way?>
# 5
It works fine for me. You don't do anything special, just use the addRow(...) method. No need to reset the viewport or anything else.
http://forum.java.sun.com/thread.jspa?forumID=57&threadID=582184
If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.
# 8
> If you have demo code to look at. That why I always ask for a SSCCE. We can't guess what you might be doing outside of the snippet of code you posted.
Of corse, it was simple for me ;-)
I've been preparing SSCCE (copying only essential parts of my main code to make it as simple as possible) and during testing it I found that scrollbar works fine in that sample -- realized some things I didn't copy caused the glitch.
I'll try to post SSCCE at first next time I encounter problems, thanks for help again!