java - JTable autoscrolling to bottom -
i have jpanel containing 3 jscrollpanes(each contains jtable) added boxlayout, see 3 tables in page , load data dynamically, code logic same 3 tables , column names , cell rendering different, each tables wanted have auto scrolling bottom of table when new rows added table, first 2 tables work perfect , scrollbar goes bottom of table, last table's scroll bar weird things! use same scrolling method 3 tables first 2 works not work!
any ideas?
i removed column adding code clearity idea;
private jscrollpane fillthirdtable(arraylist<displayvariable> displaylist) { defaulttablemodel model = new defaulttablemodel(); tooltiptable answer = new tooltiptable(model); answer.setrowheight(60); answer.setautoresizemode(jtable.auto_resize_off); answer.setsize(1300, 400); defaulttablecellrenderer dtcr = new defaulttablecellrenderer(); dtcr.sethorizontalalignment(swingconstants.center); answer.getcolumn("display variable id").setcellrenderer(dtcr); jscrollpane scrollpane = null; (displayvariable var : displaylist) { model.addrow(new object[] { id, shown, name, value }); answer.setfillsviewportheight(true); } tablecolumn c= answer.getcolumnmodel().getcolumn(3); c.setcellrenderer(new multilinecellrenderer()); tablecolumn c2= answer.getcolumnmodel().getcolumn(2); c2.setcellrenderer(new multilinecellrenderer()); scrollpane = new jscrollpane(answer); scrollpane.setsize(1300, 400); //here call method scrolltovisible(answer, (displaylist.size()-1), 1); return scrollpane; }
and method autoscrolling;
public void scrolltovisible(jtable table, int rowindex, int vcolindex) { if (!(table.getparent() instanceof jviewport)) { return; } jviewport viewport = (jviewport)table.getparent(); // rectangle relative table // northwest corner of cell (0,0) (0,0). rectangle rect = table.getcellrect(rowindex, vcolindex, true); // location of viewport relative table point pt = viewport.getviewposition(); // translate cell location relative // view, assuming northwest corner of // view (0,0) rect.setlocation(rect.x-pt.x, rect.y-pt.y); // scroll area view viewport.scrollrecttovisible(rect); }
as far know, don't need work on parent. use this:
public void scrolltovisible(jtable table, int rowindex, int vcolindex) { table.scrollrecttovisible(table.getcellrect(rowindex, vcolindex, true)); }
i did have issue scrolling sorted using swingutilities.invokelater(), might want try well:
public void scrolltovisible(final jtable table, final int rowindex, final int vcolindex) { swingutilities.invokelater(new runnable() { @override public void run() { table.scrollrecttovisible(table.getcellrect(rowindex, vcolindex, false)); } }); }
Comments
Post a Comment