java - Why don't revalidate() & repaint() work like I expect? -
i expect once combobox has been selected, jtable change.
here part of code:
…… chooseaccoutingitemcombobox.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { changetable(); jscrollpane.revalidate(); jscrollpane. repaint(); } private void changetable() { jtable accounttable2 = new jtable(accountbook.getrowdata(startyear, startmonth, endyear, endmonth, (accountingitem) chooseaccoutingitemcombobox.getselecteditem()), accountbook.getcolumnnames()); accounttable = accounttable2; } }); accounttable = new jtable(accountbook.getrowdata(startyear, startmonth, endyear, endmonth, accountintitem), accountbook.getcolumnnames()); jscrollpane = new jscrollpane(accounttable); add(jscrollpane, borderlayout.center); ……
and when selected item in combobox, jtable didn't change. why?
yours basic core java mistake, 1 has nothing swing, revalidate or repaint, , core distinction of what difference between java reference variable , reference (or object):
changing object referenced variable have no effect on original object. example, original displayed jtable object, 1 referenced accounttable variable unchanged changing reference accounttable variable holds, , reason gui not change. again understand it's not variable that's displayed, rather object
to achieve goal instead want change state of displayed jtable. changing model.
i.e., doing like:
private void changetable() { // create new table model mytablemodel newmodel = new mytablemodel(pertinentparameters); // use new model set model of displayed jtable accounttable.setmodel(newmodel); }
use parameters you're passing new jtable:
accountbook.getrowdata(startyear, startmonth, endyear, endmonth, (accountingitem) chooseaccoutingitemcombobox.getselecteditem()), accountbook.getcolumnnames()
to create new tablemodel instead.
in fact might able create defaulttablemodel directly data, like:
defaulttablemodel model = new defaulttablemodel(accountbook.getrowdata( startyear, startmonth, endyear, endmonth, (accountingitem) chooseaccoutingitemcombobox.getselecteditem()), accountbook.getcolumnnames()); accounttable.setmodel(model);
Comments
Post a Comment