Java Swing Making JTable Columns fill JScrollPane -


i have made gui jtable displays database. have 1 problem sizing.

how can make columns of jtable fill jscrollpane if there lot of columns in 1 table example keep them default size , let them scroll.

basically..

if 1 of sql tables don't fill jtable , don't need scrolling want columns of jtable made bigger fit.

if sql jtable need scrolling want left needs scrolling.

this code have making jtable:

        jpanel panel1 = new jpanel();         jtable table = new jtable(){         private static final long serialversionuid = 1l;              @override                 public boolean iscelleditable(int row, int column) {                    return false;                 }          };         jscrollpane stable = new jscrollpane (table);         stable.setverticalscrollbarpolicy(jscrollpane.vertical_scrollbar_as_needed);         stable.sethorizontalscrollbarpolicy(jscrollpane.horizontal_scrollbar_as_needed);         table.setautoresizemode(jtable.auto_resize_off);         panel1.add(stable); 

to asked call following method after table updated new model:

public static void tweakcolumns(jtable table){     enumeration<tablecolumn> columns = table.getcolumnmodel().getcolumns();      int required = 0;     while(columns.hasmoreelements()){         tablecolumn column = columns.nextelement();         int width = (int)table.gettableheader().getdefaultrenderer()                     .gettablecellrenderercomponent(table, column.getidentifier()                             , false, false, -1, column.getmodelindex()).getpreferredsize().getwidth();         required += width;     }      jviewport viewport = (jviewport)swingutilities.getancestorofclass(jviewport.class, table);     int viewportwidth = viewport.getwidth();     table.setautoresizemode(required<viewportwidth ? jtable.auto_resize_all_columns : jtable.auto_resize_off); } 

Comments