i newbie of swing.
i want update table after click button (done button). think data correct screen not work.
the followings explanation of program
- check checkboxes , click done button
- the bottom layer should change.
- there no main
this code:
import javax.swing.jbutton; import javax.swing.jcheckbox; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.jsplitpane; import javax.swing.jtable; import javax.swing.jtextfield; import javax.swing.swingconstants; import javax.swing.table.defaulttablemodel; import net.miginfocom.swing.miglayout; import java.awt.borderlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; public class gui extends jframe { class table extends jtable{ public table(defaulttablemodel model) { super(model); } public class getcolumnclass(int column) { switch (column) { case 1: return boolean.class; default: return string.class; } } } private final int bandnum = 43; int[] checkbands; object[][] data; table table; jscrollpane upperscrollpane; jscrollpane downscrollpane; jsplitpane splitpane; public gui() { // base setting setsize(900, 800); jpanel mainpanel = new jpanel(); mainpanel.setlayout(new borderlayout(5, 5)); jpanel northpanel = new jpanel(new miglayout()); // northpanel setting jlabel labelip = new jlabel("ip"); jtextfield tfip = new jtextfield(20); jlabel labelmask = new jlabel("subnet mask"); jtextfield tfmask = new jtextfield(20); jlabel labelping = new jlabel("ping"); jtextfield tfping = new jtextfield(10); jbutton btnready = new jbutton("ready"); jlabel labelready = new jlabel("not ready"); northpanel.add(labelip); northpanel.add(tfip); northpanel.add(labelmask); northpanel.add(tfmask); northpanel.add(labelping); northpanel.add(tfping); northpanel.add(btnready); northpanel.add(labelready, "wrap"); // upper scrollpane -> included in jsplitpane upper side jpanel checkpanel = new jpanel(new miglayout()); jcheckbox[] checkboxes = new jcheckbox[bandnum]; jbutton donebutton = new jbutton("done"); (int = 0; < bandnum; i++) { checkboxes[i] = new jcheckbox("" + (i + 1)); checkboxes[i].sethorizontaltextposition(swingconstants.center); checkboxes[i].setverticaltextposition(swingconstants.bottom); if (i == 32) { checkpanel.add(checkboxes[i], "wrap"); } else if (i == 42) { checkpanel.add(checkboxes[i], "wrap"); } else { checkpanel.add(checkboxes[i]); } } checkpanel.add(donebutton, "span 3"); // startbutton action ///////////////////////////////////////// //i think should watch line!!! ///////////////////////////////////////// donebutton.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { // 1. check clicked int length = 0; (int = 0; < bandnum; i++) if (checkboxes[i].isselected()) length++; checkbands = new int[length]; system.out.println(length); int k = 0; (int = 0; < bandnum; i++) { if (checkboxes[i].isselected()) { checkbands[k++] = + 1; } } // 2. ready display data = new object[length][6]; (int = 0; < length; i++) { data[i][0] = checkbands[i]; data[i][1] = true; data[i][2] = 1; data[i][3] = 2; data[i][4] = 3; data[i][5] = 4; } // 3. display string[] colnames = { "band", "test1", "test2", "test3", "test4", "test5" }; defaulttablemodel model = new defaulttablemodel(data, colnames); table = new table(model); setvisible(true); table.repaint(); downscrollpane.repaint(); splitpane.repaint(); } }); // down scrollpane -> included in jsplitpane down side string[] colnames = { "band", "test1", "test2", "test3", "test4", "test5" }; object[][] data = { { null, null, null, null, null, null }}; defaulttablemodel model = new defaulttablemodel(data, colnames); table = new table(model); // include upperscrollpane = new jscrollpane(checkpanel); downscrollpane = new jscrollpane(table); splitpane = new jsplitpane(jsplitpane.vertical_split, upperscrollpane, downscrollpane); mainpanel.add(northpanel, borderlayout.north); mainpanel.add(splitpane, borderlayout.center); getcontentpane().add(mainpanel); setvisible(true); } }
instead of doing this...
string[] colnames = { "band", "test1", "test2", "test3", "test4", "test5" }; defaulttablemodel model = new defaulttablemodel(data, colnames); table = new table(model);
simply update existing model
defaulttablemodel model = (defaulttablemodel)table.getmodel(); (object[] row : data) { model.addrow(row); }
or simply
defaulttablemodel model = (defaulttablemodel)table.getmodel(); (int = 0; < length; i++) { data = new object[6]; data[0] = checkbands[i]; data[1] = true; data[2] = 1; data[3] = 2; data[4] = 3; data[5] = 4; model.addrow(data); }
this assumes want keep adding new rows table. can use model.setrowcount(0)
clear table first , add new rows it, if that's want to.
swing works principle of mvc (model-view-controller) separates view (the jtable
) data/model (tablemodel
), means jtable
not bound data , can changed or modified changing or modifying table's model. important concept understand, swing makes great deal of use of methodology
Comments
Post a Comment