java - Change/Update JTable content with JComboBox(category) -
i have problem jtable. jtable displays content of database. 1 database table has name category. every category displayed in jcombobox. if click on category should update table content.
here short snipped of code you, easier me. code should runable:
(testclass - main)
package test; import java.awt.borderlayout; import java.awt.gridlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.util.linkedlist; import javax.swing.jcombobox; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.jtable; public class testclass implements actionlistener{ string[] header = {"head", "head", "head"}; object[][] data = {{boolean.false, "text", "text"}, {boolean.false, "text", "text"}, {boolean.false, "text", "text"}}; linkedlist<string> newdata = new linkedlist<string>(); string[] combolist = {"apple", "banana", "cranberry"}; jcombobox<string> combobox = new jcombobox<string>(combolist); jtable table = new jtable(new testtablemodel(data, header)); jframe frame = new jframe(); jpanel panel = new jpanel(new gridlayout(1, 0, 1, 0)); public testclass() { combobox.addactionlistener(this); panel.add(combobox); frame.add(panel, borderlayout.north); frame.add(new jscrollpane(table), borderlayout.center); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlocationbyplatform(true); frame.setvisible(true); frame.pack(); } @override public void actionperformed(actionevent e) { if (e.getsource() == this.combobox) { jcombobox<string> combobox = this.combobox; newdata.add("test1"); newdata.add("test2"); testtablemodel model = (testtablemodel) table.getmodel(); int = 0; (string text : newdata) { data[i][0] = boolean.true; data[i][1] = text; data[i][2] = text; i++; } model.setdata(data); } } public static void main(string[] args) { new testclass(); } }
(testtablemodel - abstracttablemodel if required (you need execute code!))
package test; import javax.swing.table.abstracttablemodel; public class testtablemodel extends abstracttablemodel { private static final long serialversionuid = 5044877015250409328l; private string[] header; private object[][] data; public testtablemodel(object[][] data, string[] header) { this.header = header; this.data = data; } public void setdata(object[][] data) { this.data = data; firetabledatachanged(); } @override public class<?> getcolumnclass(int column) { if (column == 0) { return boolean.class; } return super.getcolumnclass(column); } @override public int getcolumncount() { return header.length; } @override public string getcolumnname(int column) { return header[column]; } @override public int getrowcount() { return data.length; } @override public object getvalueat(int row, int column) { return data[row][column]; } @override public boolean iscelleditable(int row, int column) { return column == 0; } @override public void setvalueat(object value, int row, int column) { data[row][column] = value; } }
with short code table freeze if change category. @ whole code,t freeze too, able see updated table in background when resize window (table resize same size frame). don't know why isn't @ snipped.
edit: problem change content has been solved. source has been updated. problem right table size hasent been solved yet. in source first use 3 row array , after 2 row array. want delete old table , create new one, row size right.
basically want update table new content.
- thank help!
the code buggy because every time actionperformed()
called, creating new component:
table = new jtable(new testtablemodel(data, header)); frame.add(new jscrollpane( table )); // <-- btw: here need put table otherwise adding empty jscrollpane frame.validate();
(note: there additional bug, @mkorbel mentioned it).
however have added jscrollpane
jtable
frame , continue exist. (if try resize window see new table beneath old one).
the correct way update table data tablemodel
make modifications required in model , depending on changed fire apporpriate firexxx()
method notify table redraw itself.
as crude example code be:
@override public void actionperformed(actionevent e) { if (e.getsource() == this.combobox) { jcombobox<string> combobox = this.combobox; newdata.clear(); // clear list or create new 1 otherwise data keep piling up. newdata.add("test1"); newdata.add("test2"); testtablemodel model = (testtablemodel) table.getmodel(); // since need replace whole data create new object[][] each time object[][] newmodeldata = new object[newdata.size()][3]; // set whatever data new array int = 0; (string text : newdata) { newmodeldata[i][0] = boolean.true; newmodeldata[i][1] = text; newmodeldata[i][2] = text; i++; } // replace data of current model model.setdata(newmodeldata); } } .... // inside table model: ... @override public class<?> getcolumnclass(int column) { // if (column == 0) { // return boolean.class; // <-- produces classcastexception data aretrying set // } return super.getcolumnclass(column); } public void setdata(object[][] data) { this.data = data; // <-- update data firetabledatachanged(); // <-- fire event table notified. if change 1 cell need call appropriate fire event } ...
update 1: problem new code has fixed way update data in model. have logical flaw when update data
structure. variable starts array of 3 rows. in actionperformed()
method perform loop length of newdata
list has 2 entries. update first 2 rows of model.
update 2: seems missing point. how update model matters here. table display whatever data model has. if update 2 rows leave 3rd 1 unmodified table display 3 rows (2 new ones , old one). since need change each time data need completely replace data in model. data need re-created each time not table. see update code example. have added actionperformed()
method re-initializes data current source code. please read inline comments.
Comments
Post a Comment