netbeans - Adding a component to jPanel java -
i'm trying add jtable component jpanel unable see it. doing wrong?.
table gui = new table(data,colum); mainpanel.add(gui.table); class table extends jframe { public jtable table; public table(vector data, vector colum) { setlayout(new flowlayout()); table = new jtable(data,colum); table.setpreferredscrollableviewportsize(new dimension(900,10)); table.setfillsviewportheight(true); jscrollpane scrollpane = new jscrollpane(table); add(scrollpane); } }
extending jframe seems odd; don't use of top level container capabilities. here's example extends jpanel, main() drops panel jframe.
--edited accept existing jpanel
public class tablepanel { public static void addtabletopanel(jpanel jpanel, vector rowdata, vector columnnames) { jtable jtable = new jtable(rowdata, columnnames); jtable.setfillsviewportheight(true); jscrollpane jscrollpane = new jscrollpane(jtable); jscrollpane.setpreferredsize(new dimension(300, 50)); jpanel.add(jscrollpane); } public static void main(string[] args) throws exception { swingutilities.invokeandwait(new runnable() { @override public void run() { vector cols = new vector(); vector rows = new vector(); vector row1 = new vector(); cols.add("a"); cols.add("b"); cols.add("c"); row1.add("1"); row1.add("2"); row1.add("3"); rows.add(row1); rows.add(row1.clone()); rows.add(row1.clone()); rows.add(row1.clone()); jframe frame = new jframe("tabletest"); frame.setdefaultcloseoperation(jframe.exit_on_close); jpanel jpanel = new jpanel(); jpanel.setlayout(new borderlayout(0, 0)); tablepanel.addtabletopanel(jpanel, rows, cols); frame.getcontentpane().add(jpanel); frame.pack(); frame.setvisible(true); } }); } }
Comments
Post a Comment