java - TableModel not called in a JScrollPane containing a JTable -
i having unexpected behavior java gui. want create jscrollpane
containing jtable
add jscrollpane
frame
.
here code :
public class urlsscrollpanel extends jscrollpane { private static final long serialversionuid = 1l; public urlsscrollpanel() { //setup urls , collections arraylist<url> urls = url.getall(); arraylist<collection> collections = new arraylist<>(); for(url url : urls) collections.add(new collection(url)); //table string[] columns = { "database", "status", "consumption", "last snapshot date", "last message", "details", "stop collect" }; abstracttablemodel datamodel = new abstracttablemodel() { private static final long serialversionuid = 1l; @override public object getvalueat(int rowindex, int columnindex) { system.out.printf("row: %d, column: %d \n", rowindex, columnindex); //is never called. url url = urls.get(rowindex); collection collection = collections.get(rowindex); arraylist<message> lastmessages = collection.getlastsnapshot().getmessages(); message lastmessage = lastmessages.get(lastmessages.size() - 1); if(columnindex == 0) return url.tostring(); if(columnindex == 1) return collection.getstatus(); if(columnindex == 2) return consumptionchart.getchartpanel(collection); if(columnindex == 3) return collection.getlastsnapshot().getdate(); if(columnindex == 4) return string.format("[ %s ] %s", lastmessage.getdate(), lastmessage.getbody()); return "comming soon."; } @override public int getrowcount() { return urls.size(); } @override public int getcolumncount() { return columns.length; } @override public boolean iscelleditable(int row, int column) { return false; } }; jtable table = new jtable(datamodel); add(table); setbackground(color.red); setsize(500, 500); } }
and here how call :
public static void main(string[] args) { jframe frame = new jframe(); frame.setvisible(true); frame.setresizable(true); frame.setdefaultcloseoperation(jframe.dispose_on_close); frame.setextendedstate(jframe.maximized_both ); frame.setlocationrelativeto(null); urlsscrollpanel panel = new urlsscrollpanel(); frame.add(panel, borderlayout.center); swingutilities.updatecomponenttreeui(frame); }
the result red square blinking @ top left of frame disappearing immediately. datamodel
seems never called.
any in doing wrong appreciated, time!
don't extend jscrollpane
. instead, use table
construct jscrollpane
:
frame.add(new jscrollpane(table), borderlayout.center);
the approach described here; complete example shown here; alternative approach using setviewportview()
examined here.
are these 2 not similar?
jscrollpane panel = new jscrollpane(); panel.add(table); … jscrollpane panel = new jscrollpane(table);
no. shown in how scroll pane works, first formulation adds table
directly jscrollpane
, replacing jviewport
component occupies central position in scrollpanelayout
, have been used display table
. second formulation invokes setviewportview(table)
internally, tells scroll pane's jviewport
component display.
Comments
Post a Comment