gpt4 book ai didi

java - 如何在 JXTreeTable 上显示字符串

转载 作者:行者123 更新时间:2023-11-30 08:01:33 25 4
gpt4 key购买 nike

我想创建一个简单的 JXTreeTable,所以我构建了以下代码:

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;
}
}

此代码尚未工作,因为在第一列中我无法显示描述“Locale or Finestre....”但我可以看到像这张照片这样的物体

enter image description here

如何解决这个问题?

最佳答案

com.mcsolution.easyMgmt.beans.Conti@19faa8cConti 类的 toString() 方法的默认输出(继承自Object)。

尝试重写 Conti 类中的 toString() 。例如:

@Override
public String toString(){
return getDescrizione();
}

但是,鉴于您的代码,我不确定为什么会发生这种情况。您的 getValueAt() 方法永远不会返回 Conti 对象...

其他建议:

  • 如果 JTreeTable 扩展 JXTreeTable,请不要忘记调用 super()
  • 避免返回 new String(c.getTotale()+"");。为什么不直接返回 c.getTotale()+"";
  • 避免返回arg0;。您永远不应该处于这种情况,因此只需抛出 IllegalArgumentException。

关于java - 如何在 JXTreeTable 上显示字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31851041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com