I want to create a simple JXTreeTable, so I have built this code:
public static void main(String[] args){
JTreeTable table = new JTreeTable();
JFrame frame=new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(table));
frame.pack();
frame.setVisible(true);
}
public JTreeTable(){
List<Conti> listaConti = new ArrayList<Conti>();
Conti c = new Conti();
c.setDescrizione("LOCALE");
c.setIva(10);
c.setImponibile(100);
c.setTotale(c.getImponibile()-c.getIva());
List<Conti> listaFigli = new ArrayList<Conti>();
Conti figlio = new Conti();
figlio.setDescrizione("FINESTRE");
figlio.setIva(5);
figlio.setImponibile(50);
figlio.setTotale(figlio.getImponibile()-figlio.getIva());
listaFigli.add(figlio);
figlio = new Conti();
figlio.setDescrizione("IMPIANTO ELETRTICO");
figlio.setIva(5);
figlio.setImponibile(50);
figlio.setTotale(figlio.getImponibile()-figlio.getIva());
listaFigli.add(figlio);
c.setListaContiFigli(listaFigli);
listaConti.add(c);
setTreeTableModel(new RandomTextTreeTableModel(listaConti));
setRowSelectionAllowed(true);
setDragEnabled(true);
setEditable(true);
setTreeCellRenderer(new DefaultTreeRenderer());
setSelectionBackground(new Color(176, 197, 227));
setSelectionForeground(new Color(0, 0, 128));
BorderHighlighter border = new BorderHighlighter(new ColumnHighlightPredicate(1, 2), BorderFactory.createEmptyBorder(0, 6, 0, 6), false, false);
addHighlighter(border);
}
class RandomTextTreeTableModel extends DefaultTreeTableModel {
DefaultMutableTreeTableNode Root = null;
final String[] ColName = {"Acquisti/Vendite","Uscite","Entrate","Saldo"};
RandomTextTreeTableModel(List<Conti>listaConti) {
Root = new DefaultMutableTreeTableNode();
this.setRoot(Root);
for (Conti conti : listaConti) {
DefaultMutableTreeTableNode node = new DefaultMutableTreeTableNode(conti);
Root.add(node);
for (Conti contoFiglio : conti.getListaContiFigli()) {
node.add(new DefaultMutableTreeTableNode(contoFiglio));
}
}
}
@Override
public String getColumnName(int column){
return ColName[column];
}
@Override
public int getColumnCount() {
return ColName.length;
}
@Override
public Object getValueAt(Object arg0, int arg1) {
try{
if(arg1==0){
int numberElement = ((DefaultMutableTreeTableNode)arg0).getChildCount();
if(numberElement<=0)
return null;
Conti c = (Conti)((DefaultMutableTreeTableNode)arg0).getChildAt(0).getUserObject();
return new String(c.getDescrizione());
}
Conti c = (Conti)((DefaultMutableTreeTableNode)arg0).getUserObject();
if(c==null)
return null;
if (arg1 == 0) {
return new String(c.getDescrizione());
}else if (arg1 == 1) {
return new String(c.getIva()+"");
}else if (arg1 == 2) {
return new String(c.getImponibile()+"");
}else if (arg1 == 3) {
return new String(c.getTotale()+"");
}
}catch(Exception e){
e.printStackTrace();
}
return arg0;
}
}
This code not working yet, because in the first Column I can't show the description "Locale or Finestre...." But I can see the object like this photo
How can I fixed this problem?
com.mcsolution.easyMgmt.beans.Conti@19faa8c
is the default output of the toString()
method of the Conti
class (inherited from Object
).
Try overriding toString()
in your Conti
class. For example:
@Override
public String toString(){
return getDescrizione();
}
However, given your code I am not sure why this happens. Your getValueAt()
method never returns a Conti
object...
Other suggestions:
JTreeTable
extends JXTreeTable
, don't forget to call super()
.return new String(c.getTotale()+"");
. Why not just return c.getTotale()+"";
?return arg0;
. You should never be in this situation, so just throw an IllegalArgumentException.