i want defaulttablemodel jtable , add column that. search in jtable find getmodel() method.then can't add column that. in end use binding method set date in jtable haven't defaulttablemodel , want add column content jbutton add jtable.
again, sure can't add new tablecolumn jtable based on defaulttablemodel initalized
mytable.getmodel()
edit can see without casting
(defaulttablemodel model = (defaulttablemodel) table.getmodel();)
jtable
can returns tablemodel, , there isn'taddcolumn()
import java.awt.borderlayout; import java.awt.eventqueue; import java.awt.font; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.util.stack; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.jtable; import javax.swing.windowconstants; import javax.swing.table.defaulttablemodel; import javax.swing.table.tablecolumn; import javax.swing.table.tablecolumnmodel; public class tablerowheight { private static final long serialversionuid = 1l; private jframe frame = new jframe("p*s*s*s*s*t*t"); private string[] columnnames = {"one", "two", "playing with", "four", "five",}; private string[][] data = { {"aaaaaa", "bbbbbb", "cccccc", "dddddd", "eeeeeee",}, {"bbbbbb", "cccccc", "dddddd", "eeeeeee", "aaaaaa",}, {"cccccc", "dddddd", "eeeeeee", "aaaaaa", "bbbbbb",}, {"dddddd", "eeeeeee", "aaaaaa", "bbbbbb", "cccccc",}, {"eeeeeee", "aaaaaa", "bbbbbb", "cccccc", "dddddd",}}; private jtable table = new jtable(new defaulttablemodel(data, columnnames)); private tablecolumnmodel tcm = table.getcolumnmodel(); private stack<tablecolumn> coldeleted = new stack<tablecolumn>(); private jbutton restorebutton = new jbutton("restore column size"); private jbutton hidebutton = new jbutton("set column size zero"); private jbutton deletebutton = new jbutton("delete column"); private jbutton addbutton = new jbutton("restore column"); private jbutton addnewbutton = new jbutton("add new column"); private int = 1; public tablerowheight() { table.setrowmargin(4); table.setrowheight(30); table.setfont(new font("sansserif", font.bold + font.plain, 20)); jscrollpane scrollpane = new jscrollpane(table); (int = 0; < (tcm.getcolumncount()); i++) { tcm.getcolumn(i).setpreferredwidth(150); } table.setpreferredscrollableviewportsize(table.getpreferredsize()); restorebutton.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { tcm.getcolumn(2).setpreferredwidth(100); } }); hidebutton.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { tcm.getcolumn(2).setpreferredwidth(000); } }); deletebutton.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { if (table.getcolumncount() > 0) { tablecolumn coltodelete = table.getcolumnmodel().getcolumn(table.getcolumncount() - 1); table.removecolumn(coltodelete); table.validate(); coldeleted.push(coltodelete); addbutton.setenabled(true); } else { deletebutton.setenabled(false); } } }); addbutton.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { if (coldeleted.size() > 0) { table.addcolumn(coldeleted.pop()); table.validate(); deletebutton.setenabled(true); } else { addbutton.setenabled(false); } } }); addnewbutton.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { defaulttablemodel model = (defaulttablemodel) table.getmodel(); model.addcolumn("new column " + i); i++; } }); jpanel btnpanel = new jpanel(); btnpanel.add(hidebutton); btnpanel.add(restorebutton); btnpanel.add(deletebutton); btnpanel.add(addbutton); btnpanel.add(addnewbutton); frame.setdefaultcloseoperation(windowconstants.exit_on_close); frame.add(scrollpane, borderlayout.center); frame.add(btnpanel, borderlayout.south); frame.pack(); frame.setlocation(150, 150); frame.setvisible(true); } public static void main(string[] args) { eventqueue.invokelater(new runnable() { @override public void run() { tablerowheight frame = new tablerowheight(); } }); } }
Comments
Post a Comment