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]
# 1
hey,try setting the ViewPortView of the scrollPane to the table instead of calling repaint.scrollPane.setViewportView(table);
izziieea at 2007-7-12 9:37:21 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thanks izziiee.

I did some "investigations" why wouldn't it work, and figured out it might not be ScrollBarUI extending class failure -- I tried with default ScrollBarUI, also not working.

Also, why isn't table.addNotify(); enough to force table to update itself and display new content (note it works this way in tutorial, tho in my code table won't redisplay without calling repaint() or setVeiwportView() as suggested by izziiee)..?

Message was edited by:

Jimzy

Jimzya at 2007-7-12 9:37:21 > top of Java-index,Desktop,Core GUI APIs...
# 3

> I followed this guide to understand how adding/removing rows in JTable works,

Unfortunately, its not a very good example. The person who did the example doesn't understand the MVC design of Swing components.

Your TableModel should be firing the appropriate event to tell the table to repaint itself. This functionality already exists in the DefaultTableModel which supports the addition and removal or rows from the TableModel.

camickra at 2007-7-12 9:37:21 > top of Java-index,Desktop,Core GUI APIs...
# 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?>

Jimzya at 2007-7-12 9:37:21 > top of Java-index,Desktop,Core GUI APIs...
# 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.

camickra at 2007-7-12 9:37:21 > top of Java-index,Desktop,Core GUI APIs...
# 6
Solution was simple after all, I had somewhere in table creating method call to --table.setPreferredSize(new Dimension(width, height));Removing this fixed the problem (which seems obvious now).Thanks everyone for help & tips!
Jimzya at 2007-7-12 9:37:21 > top of Java-index,Desktop,Core GUI APIs...
# 7
> Solution was simple after all, 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.
camickra at 2007-7-12 9:37:21 > top of Java-index,Desktop,Core GUI APIs...
# 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!

Jimzya at 2007-7-12 9:37:21 > top of Java-index,Desktop,Core GUI APIs...