java - TableColumnModelListener. JPanel revalidate() method ruins swing elements composition -
problem description:
originally there 2 tables:
at top of left table there 2 elements: jtextfield , jcombobox, set 1 separate jpanel. these 2 elementes needed table filtering purposes, is, changing selected combo box element or typing in jtextfield table on left filtered out.
i tried implement tablecolumnmodellistener interface in order connect jtextfield , jcombobox elements table. result not expected: revalidate() method called uppon jpanel container object ruined whole swing elements composition. outcome of experiment showed in next picture:
source code:
tablestest class:
import java.awt.dimension; import javax.swing.jframe; import javax.swing.swingutilities; public class tablestest { private jframe frame = new jframe(); private javax.swing.jpanel commontablespanel; private javax.swing.jpanel datatable1panel; private javax.swing.jpanel datatable2panel; private javax.swing.jtable datatable1table; private javax.swing.jtable datatable2table; private javax.swing.jcombobox filtercombobox; private javax.swing.jpanel filterpanel; private javax.swing.jtextfield filtertextfield; private javax.swing.jscrollpane jscrollpane3; private javax.swing.jscrollpane jscrollpane4; private tableresizelistener tableresizelistener1; public tablestest() { java.awt.gridbagconstraints gridbagconstraints; commontablespanel = new javax.swing.jpanel(); datatable1panel = new javax.swing.jpanel(); filterpanel = new javax.swing.jpanel(); filtertextfield = new javax.swing.jtextfield(); filtercombobox = new javax.swing.jcombobox(); jscrollpane3 = new javax.swing.jscrollpane(); datatable1table = new javax.swing.jtable(); datatable2panel = new javax.swing.jpanel(); jscrollpane4 = new javax.swing.jscrollpane(); datatable2table = new javax.swing.jtable(); frame.setlayout(new java.awt.gridbaglayout()); commontablespanel.setminimumsize(new java.awt.dimension(30, 30)); commontablespanel.setpreferredsize(new java.awt.dimension(30, 30)); commontablespanel.setlayout(new java.awt.gridbaglayout()); datatable1panel.setlayout(new java.awt.gridbaglayout()); filterpanel.setlayout(new java.awt.gridbaglayout()); filtertextfield.setpreferredsize(new java.awt.dimension(30, 20)); gridbagconstraints = new java.awt.gridbagconstraints(); gridbagconstraints.gridx = 0; gridbagconstraints.gridy = 0; gridbagconstraints.anchor = java.awt.gridbagconstraints.northwest; filterpanel.add(filtertextfield, gridbagconstraints); filtercombobox.setmodel(new javax.swing.defaultcomboboxmodel(new string[] { "item 1", "item 2", "item 3", "item 4" })); filtercombobox.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { //todo } }); gridbagconstraints = new java.awt.gridbagconstraints(); gridbagconstraints.gridx = 1; gridbagconstraints.gridy = 0; gridbagconstraints.anchor = java.awt.gridbagconstraints.northwest; filterpanel.add(filtercombobox, gridbagconstraints); gridbagconstraints = new java.awt.gridbagconstraints(); gridbagconstraints.gridx = 0; gridbagconstraints.gridy = 1; gridbagconstraints.anchor = java.awt.gridbagconstraints.northwest; datatable1panel.add(filterpanel, gridbagconstraints); jscrollpane3.setpreferredsize(new java.awt.dimension(30, 30)); datatable1table.setmodel(new javax.swing.table.defaulttablemodel( new object [][] { {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null} }, new string [] { "title 1", "titile 2" } )); datatable1table.setminimumsize(new java.awt.dimension(30, 30)); jscrollpane3.setviewportview(datatable1table); gridbagconstraints = new java.awt.gridbagconstraints(); gridbagconstraints.gridx = 0; gridbagconstraints.gridy = 2; gridbagconstraints.fill = java.awt.gridbagconstraints.both; gridbagconstraints.weightx = 0.1; gridbagconstraints.weighty = 0.1; datatable1panel.add(jscrollpane3, gridbagconstraints); gridbagconstraints = new java.awt.gridbagconstraints(); gridbagconstraints.gridx = 0; gridbagconstraints.gridy = 0; gridbagconstraints.fill = java.awt.gridbagconstraints.both; gridbagconstraints.weightx = 0.1; gridbagconstraints.weighty = 0.1; commontablespanel.add(datatable1panel, gridbagconstraints); datatable2panel.setlayout(new java.awt.gridbaglayout()); jscrollpane4.setpreferredsize(new java.awt.dimension(30, 30)); datatable2table.setmodel(new javax.swing.table.defaulttablemodel( new object [][] { {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null}, {null, null} }, new string [] { "titile 1", "title 2" } ) { boolean[] canedit = new boolean [] { false, false }; public boolean iscelleditable(int rowindex, int columnindex) { homecoming canedit [columnindex]; } }); datatable2table.setminimumsize(new java.awt.dimension(30, 30)); jscrollpane4.setviewportview(datatable2table); gridbagconstraints = new java.awt.gridbagconstraints(); gridbagconstraints.gridx = 0; gridbagconstraints.gridy = 1; gridbagconstraints.fill = java.awt.gridbagconstraints.both; gridbagconstraints.weightx = 0.1; gridbagconstraints.weighty = 0.1; datatable2panel.add(jscrollpane4, gridbagconstraints); gridbagconstraints = new java.awt.gridbagconstraints(); gridbagconstraints.gridx = 1; gridbagconstraints.gridy = 0; gridbagconstraints.fill = java.awt.gridbagconstraints.both; gridbagconstraints.weightx = 0.1; gridbagconstraints.weighty = 0.1; commontablespanel.add(datatable2panel, gridbagconstraints); gridbagconstraints = new java.awt.gridbagconstraints(); gridbagconstraints.gridx = 0; gridbagconstraints.gridy = 1; gridbagconstraints.fill = java.awt.gridbagconstraints.both; gridbagconstraints.anchor = java.awt.gridbagconstraints.northwest; gridbagconstraints.weightx = 0.1; gridbagconstraints.weighty = 0.1; gridbagconstraints.insets = new java.awt.insets(4, 4, 0, 4); frame.add(commontablespanel, gridbagconstraints); tableresizelistener1 = new tableresizelistener(datatable1table, filterpanel); frame.setpreferredsize(new dimension(400,150)); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { @override public void run() { tablestest tabletest = new tablestest(); } }); } }
notice
tableresizelistener1 = new tableresizelistener(datatable1table, filterpanel);
part. table columns connected corresponding jpanel elements (jtextfield , jcombobox in example). can comment line out if want observe how composition without connected elements.
tableresizelistener class. problem lurks.
import java.awt.component; import java.awt.dimension; import javax.swing.jpanel; import javax.swing.jtable; import javax.swing.swingutilities; import javax.swing.event.changeevent; import javax.swing.event.listselectionevent; import javax.swing.event.tablecolumnmodelevent; import javax.swing.event.tablecolumnmodellistener; import javax.swing.table.tablecolumnmodel; public class tableresizelistener implements tablecolumnmodellistener { private static final long serialversionuid = 1l; private jtable table = null; private jpanel filterrow = null; public tableresizelistener(jtable table, jpanel filterrow) { this.table = table; this.filterrow = filterrow; this.table.getcolumnmodel().addcolumnmodellistener(this); columnmarginchanged(new changeevent(table.getcolumnmodel())); } // implement tablecolumnmodellistener methods // (note: instead of implementing listener should able // override columnmarginchanged , colummoved methods of jtable) @override public void columnmarginchanged(changeevent e) { tablecolumnmodel tcm = table.getcolumnmodel(); int columns = tcm.getcolumncount(); if(columns == filterrow.getcomponentcount()) { (int = 0; < columns; i++) { component comp = filterrow.getcomponent(i); dimension d = comp.getpreferredsize(); d.width = tcm.getcolumn(i).getwidth(); comp.setpreferredsize(d); } swingutilities.invokelater(new runnable() { @override public void run() { filterrow.revalidate(); } }); } } @override public void columnmoved(tablecolumnmodelevent e) { component moved = filterrow.getcomponent(e.getfromindex()); filterrow.remove(e.getfromindex()); filterrow.add(moved, e.gettoindex()); filterrow.validate(); } @override public void columnadded(tablecolumnmodelevent e) { } @override public void columnremoved(tablecolumnmodelevent e) { } @override public void columnselectionchanged(listselectionevent e) { } }
the line
filterrow.revalidate();
is responsible bug. tried phone call repaint(), validate(), resulted in not such smooth visual experience.
how can connect elements outside of jtable without ruining visual composition?
comment line out prepare problem.
jscrollpane4.setpreferredsize(new java.awt.dimension(30, 30));
java swing jtable listener
No comments:
Post a Comment