java - How do I render a JTable inside of a JTable? -
i trying nest jtable within of jtable's column (using cellrenderer).
example (erroneous) output:
why next illustration not output table within table?
import java.awt.component; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.jtable; import javax.swing.table.abstracttablemodel; import javax.swing.table.tablecellrenderer; public class test { public static void main(string[] args) { jtable table = new jtable(new customtablemodel()); table.setdefaultrenderer(person.class, new customrenderer()); jpanel panel = new jpanel(); panel.add(new jscrollpane(table)); jframe frame = new jframe(); frame.setcontentpane(panel); frame.pack(); frame.setvisible(true); } } @suppresswarnings("serial") class customtablemodel extends abstracttablemodel { @override public int getcolumncount() { homecoming 2; } @override public int getrowcount() { homecoming 1; } @override public object getvalueat(int row, int col) { if (col == 0) { homecoming new person("bob"); } else { homecoming "is awesome!"; } } } @suppresswarnings("serial") class customrenderer extends jpanel implements tablecellrenderer { @override public component gettablecellrenderercomponent(jtable table, object value, boolean isselected, boolean hasfocus, int row, int col) { jtable t = new jtable(new customtablemodel()); add(new jscrollpane(t)); homecoming this; } } class person { public string name = ""; public person(string name) { this.name = name; } }
don't add together sub table scroll pane. instead, seek adding table center position of jpanel borderlayout, add together tables header north position
the main reason scroll pane not interactive , may hide data
updated
i'm on phone, it's hard read code :p
in main table model, need override getcolumnclass method , create sure retuning person.class required column (and right class types other columns to)
java swing user-interface jtable nested
No comments:
Post a Comment