java - Overriding JTable's DefaultTableCellRenderer to center all the cells in a JTable -



java - Overriding JTable's DefaultTableCellRenderer to center all the cells in a JTable -

i've got problem can't rid of.

just know, i'm new using jtables, reply might simple, can't find solution :/

so, i've got jtable using abstracttablemodel, overrides

public class<?> getcolumnclass(int columnindex_p)

method, tell type of each column displayed. 1 of them boolean.

when create simple jtable, using

table_l = new jtable(new mytablemodel());

everything fine, , boolean values correctly displayed using checkboxes (on/off).

now, i'd center text on each cell (and, perchance more options later).

so define new defaulttablecellrenderer each column, :

defaulttablecellrenderer cellrenderer_l = new defaulttablecellrenderer() { public component gettablecellrenderercomponent(jtable table, object value, boolean isselected, boolean hasfocus, int row, int column) { // delegate rendering part default renderer (am right ???) component comp = super.gettablecellrenderercomponent(table, value, isselected, hasfocus, row, column); homecoming comp; } }

and set horizontal alignment of cellrender :

cellrenderer_l.sethorizontalalignment(jlabel.center);

then install new cellrenderer on each column of jtable :

(int = 0; < table_l.getcolumncount(); ++i) { table_l.getcolumnmodel().getcolumn(i).setcellrenderer(cellrenderer_l); }

but, new cellrenderer, displayed jtable isn't using getcolumnclass() method of tablemodel anymore, , display "true/false" string on boolean values.

i don't know how still utilize getcolumnclass() before.

if has answer... give thanks you

edit: clarifications made. in fact, real question : "how impact defaultrenderer of jtable create them center result in jtable's cells"

for applying same visual decoration rendering components (if that's really want, careful, might have usability penalty!) can override jtable's preparedrenderer method:

@override public component preparerenderer(tablecellrenderer renderer, int row, int column) { component comp = super.preparerenderer(...); if (comp instanceof jlabel) { ((jlabel) comp).sethorizontalalignment(...); } homecoming comp; }

btw: approach violating rule not subclass jsomething application needs. might consider using swingx formally supports visually decorating rendering components. instead of subclassing, register highlighter table:

jxtable table = ... table.addhighlighter(new alignmenthighlighter(center), highlightpredicate.always);

java swing jtable renderer

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -