java - How to add jtable inside of jscrollpane? -
i know,how add table inside of scrollpane. i've introduced loop there creating problem please me rid of problem (i want add table inside of scrollpane) seems it's working whenever don't introduce scrollpane whenever put table inside of scrollpane, doesn't work. please help.
thanks in advance.
here code:
import javax.swing.defaultcelleditor; import javax.swing.jframe; import javax.swing.jscrollpane; import javax.swing.jtable; import javax.swing.table.defaulttablemodel; import javax.swing.table.tablecolumn; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.jframe; import java.awt.*; import javax.swing.*; public class simple { jscrollpane mainscroll; jpanel panel; jframe frame; public simple() { frame=new jframe(); panel=new jpanel(); mainscroll=new jscrollpane(); object[][] rowdata = {}; object[] columnnames = { "sr.no","subject","department","status" }; jcombobox mainbox=new jcombobox(); mainbox.additem("processed"); mainbox.additem("pending"); defaulttablemodel listtablemodel; listtablemodel = new defaulttablemodel(rowdata, columnnames); (int = 0; < 1000; i++) { string main = "" + i; listtablemodel.addrow(new object[] { main, "", "" ,"choose"}); jtable listtable; listtable = new jtable(listtablemodel); tablecolumn statuscolumn=listtable.getcolumnmodel().getcolumn(3); statuscolumn.setcelleditor(new defaultcelleditor(mainbox)); mainscroll.add(listtable); } panel.setlayout(null); mainscroll.setbounds(37, 143, 397, 183); // mainbox.additem("processing"); frame.add(panel); panel.add(mainscroll); frame.setsize(600,600); frame.setvisible(true); } public static void main(string arg[]) { new simple(); } }
instead of
mainscroll=new jscrollpane(); ... mainscroll.add(listtable);
you need after loop
mainscroll = new jscrollpane(listtable);
addendum 1:
you need pull creation of jtable outside loop. inside loop add rows table.
the core of solution this:
jtable listtable = new jtable(listtablemodel); (int = 0; < 1000; i++) { // add rows table } mainscroll = new jscrollpane(listtable);
addendum 2 (figured i'd need identifiers)
import javax.swing.table.defaulttablemodel; import javax.swing.table.tablecolumn; import java.awt.*; import javax.swing.*; public class simple { jscrollpane mainscroll; jpanel panel; jframe frame; public simple() { frame=new jframe(); panel=new jpanel(); object[][] rowdata = {}; object[] columnnames = { "sr.no","subject","department","status" }; jcombobox mainbox=new jcombobox(); mainbox.additem("processed"); mainbox.additem("pending"); defaulttablemodel listtablemodel; listtablemodel = new defaulttablemodel(rowdata, columnnames); jtable listtable = new jtable(listtablemodel); (int = 0; < 1000; i++) { string main = "" + i; listtablemodel.addrow(new object[] { main, "", "" ,"choose"}); tablecolumn statuscolumn=listtable.getcolumnmodel().getcolumn(3); statuscolumn.setcelleditor(new defaultcelleditor(mainbox)); } mainscroll = new jscrollpane(listtable); panel.setlayout(null); mainscroll.setbounds(37, 143, 397, 183); frame.add(panel); panel.add(mainscroll); frame.setsize(600,600); frame.setvisible(true); } public static void main(string arg[]) { new simple(); } }
Comments
Post a Comment