java - Multiple editable rows in JTable header -
i need have multiple editable rows in jtable header, same header column contain both jtextfield , jbutton.
i have found how implement editable header cells , how split header cells. turned out these approaches different , need fuse them one.
is there multiple editable jtable header rows solutions developed , wouldn't need reinvent bicycle?
what have found far:
editable header row code taken from: http://www.java2s.com/code/java/swing-components/editableheadertableexample2.htm
code:
import java.awt.component; import java.awt.point; import java.awt.rectangle; import java.awt.event.mouseevent; import java.awt.event.windowadapter; import java.awt.event.windowevent; import java.util.eventobject; import javax.swing.defaultcelleditor; import javax.swing.jcombobox; import javax.swing.jcomponent; import javax.swing.jframe; import javax.swing.jscrollpane; import javax.swing.jtable import javax.swing.jtextfield; import javax.swing.swingutilities; import javax.swing.uimanager; import javax.swing.event.celleditorlistener; import javax.swing.event.changeevent; import javax.swing.event.mouseinputlistener; import javax.swing.plaf.basic.basictableheaderui; import javax.swing.table.jtableheader; import javax.swing.table.tablecelleditor; import javax.swing.table.tablecellrenderer; import javax.swing.table.tablecolumn; import javax.swing.table.tablecolumnmodel; /** * @version 1.0 08/22/99 */ public class editableheadertableexample2 extends jframe { public editableheadertableexample2() { super("editableheader example"); jtable table = new jtable(7, 5); tablecolumnmodel columnmodel = table.getcolumnmodel(); table.settableheader(new editableheader(columnmodel)); string[] items = { "dog", "cat" }; jcombobox combo = new jcombobox(); (int = 0; < items.length; i++) { combo.additem(items[i]); } comborenderer renderer = new comborenderer(items); editableheadertablecolumn col; // column 1 col = (editableheadertablecolumn) table.getcolumnmodel().getcolumn(1); col.setheadervalue(combo.getitemat(0)); col.setheaderrenderer(renderer); col.setheadereditor(new defaultcelleditor(combo)); // column 3 col = (editableheadertablecolumn) table.getcolumnmodel().getcolumn(3); col.setheadervalue(combo.getitemat(0)); //col.setheaderrenderer(renderer); col.setheadereditor(new defaultcelleditor(combo)); jscrollpane pane = new jscrollpane(table); getcontentpane().add(pane); } class comborenderer extends jcombobox implements tablecellrenderer { comborenderer(string[] items) { (int = 0; < items.length; i++) { additem(items[i]); } } public component gettablecellrenderercomponent(jtable table, object value, boolean isselected, boolean hasfocus, int row, int column) { setselecteditem(value); return this; } } public static void main(string[] args) { editableheadertableexample2 frame = new editableheadertableexample2(); frame.addwindowlistener(new windowadapter() { public void windowclosing(windowevent e) { system.exit(0); } }); frame.setsize(300, 100); frame.setvisible(true); } } class editableheader extends jtableheader implements celleditorlistener { public final int header_row = -10; transient protected int editingcolumn; transient protected tablecelleditor celleditor; transient protected component editorcomp; public editableheader(tablecolumnmodel columnmodel) { super(columnmodel); setreorderingallowed(false); celleditor = null; recreatetablecolumn(columnmodel); } public void updateui() { setui(new editableheaderui()); resizeandrepaint(); invalidate(); } protected void recreatetablecolumn(tablecolumnmodel columnmodel) { int n = columnmodel.getcolumncount(); editableheadertablecolumn[] newcols = new editableheadertablecolumn[n]; tablecolumn[] oldcols = new tablecolumn[n]; (int = 0; < n; i++) { oldcols[i] = columnmodel.getcolumn(i); newcols[i] = new editableheadertablecolumn(); newcols[i].copyvalues(oldcols[i]); } (int = 0; < n; i++) { columnmodel.removecolumn(oldcols[i]); } (int = 0; < n; i++) { columnmodel.addcolumn(newcols[i]); } } public boolean editcellat(int index) { return editcellat(index); } public boolean editcellat(int index, eventobject e) { if (celleditor != null && !celleditor.stopcellediting()) { return false; } if (!iscelleditable(index)) { return false; } tablecelleditor editor = getcelleditor(index); if (editor != null && editor.iscelleditable(e)) { editorcomp = prepareeditor(editor, index); editorcomp.setbounds(getheaderrect(index)); add(editorcomp); editorcomp.validate(); setcelleditor(editor); seteditingcolumn(index); editor.addcelleditorlistener(this); return true; } return false; } public boolean iscelleditable(int index) { if (getreorderingallowed()) { return false; } int columnindex = columnmodel.getcolumn(index).getmodelindex(); editableheadertablecolumn col = (editableheadertablecolumn) columnmodel .getcolumn(columnindex); return col.isheadereditable(); } public tablecelleditor getcelleditor(int index) { int columnindex = columnmodel.getcolumn(index).getmodelindex(); editableheadertablecolumn col = (editableheadertablecolumn) columnmodel .getcolumn(columnindex); return col.getheadereditor(); } public void setcelleditor(tablecelleditor neweditor) { tablecelleditor oldeditor = celleditor; celleditor = neweditor; // firepropertychange if (oldeditor != null && oldeditor instanceof tablecelleditor) { ((tablecelleditor) oldeditor) .removecelleditorlistener((celleditorlistener) this); } if (neweditor != null && neweditor instanceof tablecelleditor) { ((tablecelleditor) neweditor) .addcelleditorlistener((celleditorlistener) this); } } public component prepareeditor(tablecelleditor editor, int index) { object value = columnmodel.getcolumn(index).getheadervalue(); boolean isselected = true; int row = header_row; jtable table = gettable(); component comp = editor.gettablecelleditorcomponent(table, value, isselected, row, index); if (comp instanceof jcomponent) { ((jcomponent) comp).setnextfocusablecomponent(this); } return comp; } public tablecelleditor getcelleditor() { return celleditor; } public component geteditorcomponent() { return editorcomp; } public void seteditingcolumn(int acolumn) { editingcolumn = acolumn; } public int geteditingcolumn() { return editingcolumn; } public void removeeditor() { tablecelleditor editor = getcelleditor(); if (editor != null) { editor.removecelleditorlistener(this); requestfocus(); remove(editorcomp); int index = geteditingcolumn(); rectangle cellrect = getheaderrect(index); setcelleditor(null); seteditingcolumn(-1); editorcomp = null; repaint(cellrect); } } public boolean isediting() { return (celleditor == null) ? false : true; } // // celleditorlistener // public void editingstopped(changeevent e) { tablecelleditor editor = getcelleditor(); if (editor != null) { object value = editor.getcelleditorvalue(); int index = geteditingcolumn(); columnmodel.getcolumn(index).setheadervalue(value); removeeditor(); } } public void editingcanceled(changeevent e) { removeeditor(); } // // public void setreorderingallowed(boolean b) { // reorderingallowed = false; // } } class editableheaderui extends basictableheaderui { protected mouseinputlistener createmouseinputlistener() { return new mouseinputhandler((editableheader) header); } public class mouseinputhandler extends basictableheaderui.mouseinputhandler { private component dispatchcomponent; protected editableheader header; public mouseinputhandler(editableheader header) { this.header = header; } private void setdispatchcomponent(mouseevent e) { component editorcomponent = header.geteditorcomponent(); point p = e.getpoint(); point p2 = swingutilities.convertpoint(header, p, editorcomponent); dispatchcomponent = swingutilities.getdeepestcomponentat( editorcomponent, p2.x, p2.y); } private boolean repostevent(mouseevent e) { if (dispatchcomponent == null) { return false; } mouseevent e2 = swingutilities.convertmouseevent(header, e, dispatchcomponent); dispatchcomponent.dispatchevent(e2); return true; } public void mousepressed(mouseevent e) { if (!swingutilities.isleftmousebutton(e)) { return; } super.mousepressed(e); if (header.getresizingcolumn() == null) { point p = e.getpoint(); tablecolumnmodel columnmodel = header.getcolumnmodel(); int index = columnmodel.getcolumnindexatx(p.x); if (index != -1) { if (header.editcellat(index, e)) { setdispatchcomponent(e); repostevent(e); } } } } public void mousereleased(mouseevent e) { super.mousereleased(e); if (!swingutilities.isleftmousebutton(e)) { return; } repostevent(e); dispatchcomponent = null; } } } class editableheadertablecolumn extends tablecolumn { protected tablecelleditor headereditor; protected boolean isheadereditable; public editableheadertablecolumn() { setheadereditor(createdefaultheadereditor()); isheadereditable = true; } public void setheadereditor(tablecelleditor headereditor) { this.headereditor = headereditor; } public tablecelleditor getheadereditor() { return headereditor; } public void setheadereditable(boolean iseditable) { isheadereditable = iseditable; } public boolean isheadereditable() { return isheadereditable; } public void copyvalues(tablecolumn base) { modelindex = base.getmodelindex(); identifier = base.getidentifier(); width = base.getwidth(); minwidth = base.getminwidth(); setpreferredwidth(base.getpreferredwidth()); maxwidth = base.getmaxwidth(); headerrenderer = base.getheaderrenderer(); headervalue = base.getheadervalue(); cellrenderer = base.getcellrenderer(); celleditor = base.getcelleditor(); isresizable = base.getresizable(); } protected tablecelleditor createdefaultheadereditor() { return new defaultcelleditor(new jtextfield()); } }
multiple rows in same jtable header column code (fixed java 7) taken from: http://www.java2s.com/code/java/swing-components/groupablegroupheaderexample.htm
code: column group class:
import java.util.*; import java.awt.*; import javax.swing.*; import javax.swing.table.*; /** * columngroup * * @version 1.0 10/20/98 * @author nobuo tamemasa */ class columngroup { protected tablecellrenderer renderer; protected vector v; protected string text; protected int margin=0; public columngroup(string text) { this(null,text); } public columngroup(tablecellrenderer renderer,string text) { if (renderer == null) { this.renderer = new defaulttablecellrenderer() { public component gettablecellrenderercomponent(jtable table, object value, boolean isselected, boolean hasfocus, int row, int column) { jtableheader header = table.gettableheader(); if (header != null) { setforeground(header.getforeground()); setbackground(header.getbackground()); setfont(header.getfont()); } sethorizontalalignment(jlabel.center); settext((value == null) ? "" : value.tostring()); setborder(uimanager.getborder("tableheader.cellborder")); return this; } }; } else { this.renderer = renderer; } this.text = text; v = new vector(); } /** * @param obj tablecolumn or columngroup */ public void add(object obj) { if (obj == null) { return; } v.addelement(obj); /*if(obj instanceof jtextfield) { this.setheaderrenderer(renderer) }*/ } /** * @param c tablecolumn * @param v columngroups */ public vector getcolumngroups(tablecolumn c, vector g) { g.addelement(this); if (v.contains(c)) return g; enumeration e = v.elements(); while (e.hasmoreelements()) { object obj = e.nextelement(); if (obj instanceof columngroup) { vector groups = (vector)((columngroup)obj).getcolumngroups(c,(vector)g.clone()); if (groups != null) return groups; } } return null; } public tablecellrenderer getheaderrenderer() { return renderer; } public void setheaderrenderer(tablecellrenderer renderer) { if (renderer != null) { this.renderer = renderer; } } public object getheadervalue() { return text; } public dimension getsize(jtable table) { component comp = renderer.gettablecellrenderercomponent( table, getheadervalue(), false, false,-1, -1); int height = comp.getpreferredsize().height; int width = 0; enumeration e = v.elements(); while (e.hasmoreelements()) { object obj = e.nextelement(); if (obj instanceof tablecolumn) { tablecolumn acolumn = (tablecolumn)obj; width += acolumn.getwidth(); //width += margin; } else { width += ((columngroup)obj).getsize(table).width; } } return new dimension(width, height); } public void setcolumnmargin(int margin) { this.margin = margin; enumeration e = v.elements(); while (e.hasmoreelements()) { object obj = e.nextelement(); if (obj instanceof columngroup) { ((columngroup)obj).setcolumnmargin(margin); } } }
groupableheaderexample
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; /** * @version 1.0 11/09/98 */ public class groupableheaderexample extends jframe { groupableheaderexample() { super( "groupable header example" ); string[] petstrings = { "bird", "cat", "dog", "rabbit", "pig" }; jcombobox petlist = new jcombobox(petstrings); defaulttablemodel dm = new defaulttablemodel(); dm.setdatavector(new object[][]{ {"foo","ja"}, {"bar","en"}}, new object[]{new jtextfield(), petlist});//"input box","combo box"}); jtable table = new jtable( dm ) { protected jtableheader createdefaulttableheader() { return new groupabletableheader(columnmodel); } }; tablecolumnmodel cm = table.getcolumnmodel(); columngroup g_name = new columngroup("title1"); g_name.add(cm.getcolumn(0)); //g_name.add(cm.getcolumn(1)); columngroup g_lang = new columngroup("title2"); g_lang.add(cm.getcolumn(1)); /*columngroup g_other = new columngroup("others"); g_other.add(cm.getcolumn(4)); g_other.add(cm.getcolumn(5)); g_lang.add(g_other);*/ groupabletableheader header = (groupabletableheader)table.gettableheader(); header.addcolumngroup(g_name); header.addcolumngroup(g_lang); jscrollpane scroll = new jscrollpane( table ); getcontentpane().add( scroll ); setsize( 400, 120 ); } public static void main(string[] args) { groupableheaderexample frame = new groupableheaderexample(); frame.addwindowlistener( new windowadapter() { public void windowclosing( windowevent e ) { system.exit(0); } }); frame.setvisible(true); } }
groupabletableheader class
import java.util.*; import java.awt.*; import javax.swing.*; import javax.swing.table.*; /** * groupabletableheader * * @version 1.0 10/20/98 * @author nobuo tamemasa */ public class groupabletableheader extends jtableheader { private static final string uiclassid = "groupabletableheaderui"; protected vector columngroups = null; public groupabletableheader(tablecolumnmodel model) { super(model); setui(new groupabletableheaderui()); setreorderingallowed(false); } public void updateui(){ setui(new groupabletableheaderui()); } public void setreorderingallowed(boolean b) { reorderingallowed = false; } public void addcolumngroup(columngroup g) { if (columngroups == null) { columngroups = new vector(); } columngroups.addelement(g); } public enumeration getcolumngroups(tablecolumn col) { if (columngroups == null) return null; enumeration e = columngroups.elements(); while (e.hasmoreelements()) { columngroup cgroup = (columngroup)e.nextelement(); vector v_ret = (vector)cgroup.getcolumngroups(col,new vector()); if (v_ret != null) { return v_ret.elements(); } } return null; } public void setcolumnmargin() { if (columngroups == null) return; int columnmargin = getcolumnmodel().getcolumnmargin(); enumeration e = columngroups.elements(); while (e.hasmoreelements()) { columngroup cgroup = (columngroup)e.nextelement(); cgroup.setcolumnmargin(columnmargin); } } }
groupabletableheaderui class
import java.util.*; import java.awt.*; import javax.swing.*; import javax.swing.border.*; import javax.swing.table.*; import javax.swing.plaf.basic.*; public class groupabletableheaderui extends basictableheaderui { public void paint(graphics g, jcomponent c) { rectangle clipbounds = g.getclipbounds(); if (header.getcolumnmodel() == null) return; ((groupabletableheader)header).setcolumnmargin(); int column = 0; dimension size = header.getsize(); rectangle cellrect = new rectangle(0, 0, size.width, size.height); hashtable h = new hashtable(); int columnmargin = header.getcolumnmodel().getcolumnmargin(); enumeration enumeration = header.getcolumnmodel().getcolumns(); while (enumeration.hasmoreelements()) { cellrect.height = size.height; cellrect.y = 0; tablecolumn acolumn = (tablecolumn)enumeration.nextelement(); enumeration cgroups = ((groupabletableheader)header).getcolumngroups(acolumn); if (cgroups != null) { int groupheight = 0; while (cgroups.hasmoreelements()) { columngroup cgroup = (columngroup)cgroups.nextelement(); rectangle grouprect = (rectangle)h.get(cgroup); if (grouprect == null) { grouprect = new rectangle(cellrect); dimension d = cgroup.getsize(header.gettable()); grouprect.width = d.width; grouprect.height = d.height; h.put(cgroup, grouprect); } paintcell(g, grouprect, cgroup); groupheight += grouprect.height; cellrect.height = size.height - groupheight; cellrect.y = groupheight; } } cellrect.width = acolumn.getwidth();// + columnmargin; if (cellrect.intersects(clipbounds)) { paintcell(g, cellrect, column); } cellrect.x += cellrect.width; column++; } } private void paintcell(graphics g, rectangle cellrect, int columnindex) { tablecolumn acolumn = header.getcolumnmodel().getcolumn(columnindex); tablecellrenderer renderer = acolumn.getheaderrenderer(); //revised java2s.com renderer = new defaulttablecellrenderer(){ public component gettablecellrenderercomponent(jtable table, object value, boolean isselected, boolean hasfocus, int row, int column) { jlabel header = new jlabel(); header.setforeground(table.gettableheader().getforeground()); header.setbackground(table.gettableheader().getbackground()); header.setfont(table.gettableheader().getfont()); header.sethorizontalalignment(jlabel.center); header.settext(value.tostring()); header.setborder(uimanager.getborder("tableheader.cellborder")); return header; } }; component c = renderer.gettablecellrenderercomponent( header.gettable(), acolumn.getheadervalue(),false, false, -1, columnindex); c.setbackground(uimanager.getcolor("control")); rendererpane.add(c); rendererpane.paintcomponent(g, c, header, cellrect.x, cellrect.y, cellrect.width, cellrect.height, true); } private void paintcell(graphics g, rectangle cellrect,columngroup cgroup) { tablecellrenderer renderer = cgroup.getheaderrenderer(); //revised java2s.com // if(renderer == null){ // return ; // } component component = renderer.gettablecellrenderercomponent( header.gettable(), cgroup.getheadervalue(),false, false, -1, -1); rendererpane.add(component); rendererpane.paintcomponent(g, component, header, cellrect.x, cellrect.y, cellrect.width, cellrect.height, true); } private int getheaderheight() { int height = 0; tablecolumnmodel columnmodel = header.getcolumnmodel(); for(int column = 0; column < columnmodel.getcolumncount(); column++) { tablecolumn acolumn = columnmodel.getcolumn(column); tablecellrenderer renderer = acolumn.getheaderrenderer(); //revised java2s.com if(renderer == null){ return 40; } component comp = renderer.gettablecellrenderercomponent( header.gettable(), acolumn.getheadervalue(), false, false,-1, column); int cheight = comp.getpreferredsize().height; enumeration e = ((groupabletableheader)header).getcolumngroups(acolumn); if (e != null) { while (e.hasmoreelements()) { columngroup cgroup = (columngroup)e.nextelement(); cheight += cgroup.getsize(header.gettable()).height; } } height = math.max(height, cheight); } return height; } private dimension createheadersize(long width) { tablecolumnmodel columnmodel = header.getcolumnmodel(); width += columnmodel.getcolumnmargin() * columnmodel.getcolumncount(); if (width > integer.max_value) { width = integer.max_value; } return new dimension((int)width, getheaderheight()); } public dimension getpreferredsize(jcomponent c) { long width = 0; enumeration enumeration = header.getcolumnmodel().getcolumns(); while (enumeration.hasmoreelements()) { tablecolumn acolumn = (tablecolumn)enumeration.nextelement(); width = width + acolumn.getpreferredwidth(); } return createheadersize(width); } }
just idea use tablecelleditor in jtableheader wrong, nor multi_line_header, buy jide (support)
simpler possible use rowfilter jtextfield,
note don't forget convert indexes view coordinates model coordinates
.
.
import java.awt.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.table.*; //code based on posts @camickr public class tablefilterrow implements tablecolumnmodellistener { private static final long serialversionuid = 1l; private jframe frame = new jframe(); private jtable table = new jtable(3, 5); private jscrollpane scrollpane = new jscrollpane(table); // panel text fields private jpanel filterrow = new jpanel(new flowlayout(flowlayout.center, 0, 0)); public tablefilterrow() { table.setpreferredscrollableviewportsize(table.getpreferredsize()); table.getcolumnmodel().addcolumnmodellistener(this); filterrow = new jpanel(new flowlayout(flowlayout.center, 0, 0)); (int = 0; < table.getcolumncount(); i++) { filterrow.add(new jtextfield(" sum @ - " + i)); } columnmarginchanged(new changeevent(table.getcolumnmodel())); frame.add(scrollpane); frame.add(filterrow, borderlayout.south); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } // 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(); (int = 0; < columns; i++) { jtextfield textfield = (jtextfield) filterrow.getcomponent(i); dimension d = textfield.getpreferredsize(); d.width = tcm.getcolumn(i).getwidth(); textfield.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) { } public static void main(string[] args) { swingutilities.invokelater(new runnable() { @override public void run() { tablefilterrow frame = new tablefilterrow(); } }); } }
Meanwhile, you can also tour through some great stuff on Small Business Accounting Software
ReplyDelete