我有一个简单的Java swing应用程序。所以我有一个带有JXTreeTable的JPanel。我可以插入这个带有子节点的TreeTable,soma行,这没关系。
现在,如果我尝试运行我的应用程序,我可以看到:
这也没关系。所以,现在如果我尝试开发一个或多个NODE,我可以看到:
我想知道是否可以更改渲染(为NODE孩子设置其他背景或其他字体颜色。
这是我用来创建JXTreeTable的代码。
private void createTable(List<Entrate> v,String tipoConto){
tableCatEntrate = new CustomTreeTableEntrate(v,tipoConto);
daiProprietaJTableCatEntrate();
jscrollPaneCatEntrate.setViewportView(tableCatEntrate);
}
这是CustomTreeTableEntralte类:
public class CustomTreeTableEntrate extends JXTreeTable{
public CustomTreeTableEntrate(List<Entrate> listaConti,String tipoConto){
setTreeTableModel(new RandomTextTreeTableModel(listaConti,tipoConto));
setRowSelectionAllowed(false);
this.setDefaultRenderer(Object.class, new CustomTreeTableSpeseXCategoriaSpese());
}
class RandomTextTreeTableModel extends DefaultTreeTableModel {
public final NumberFormat decimalFormatter2 = new DecimalFormat("#,##0.00");
DefaultMutableTreeTableNode Root = null;
final String[] ColName = {"","Acquisti/Vendite","Uscite","Entrate","Saldo"};
//public LinkedHashMap<String, Entrate> mappaConti = new LinkedHashMap<String, Entrate>();
String tipoConto;
RandomTextTreeTableModel(List<Entrate>listaConti,String tipoConto) {
Root = new DefaultMutableTreeTableNode();
this.setRoot(Root);
for (Entrate conti : listaConti) {
DefaultMutableTreeTableNode node = new DefaultMutableTreeTableNode(conti);
Root.add(node);
if(conti.getListaContiFigli()!=null){
for (Entrate contoFiglio : conti.getListaContiFigli()) {
node.add(new DefaultMutableTreeTableNode(contoFiglio));
mappaConti.put(contoFiglio.getDescrizione(), contoFiglio);
}
}
contoTotale.setTotale(conti.getTotale()+contoTotale.getTotale());
contoTotale.setTotalePubblico(conti.getTotalePubblico()+contoTotale.getTotalePubblico());
contoTotale.setEntrate(conti.getEntrate()+contoTotale.getEntrate());
contoTotale.setUscite(conti.getUscite()+contoTotale.getUscite());
}
DefaultMutableTreeTableNode node = new DefaultMutableTreeTableNode(contoTotale);
Root.add(node);
}
@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 "";
}
Entrate c = (Entrate)((DefaultMutableTreeTableNode)arg0).getUserObject();
if(c==null)
return null;
if (arg1 == 0) {
return "";
}else if (arg1 == 1) {
return c.getDescrizione();
}else if (arg1 == 2) {
return decimalFormatter2.format(c.getUscite());
}else if (arg1 == 3) {
return decimalFormatter2.format(c.getEntrate());
}else if (arg1 == 4 && tipoConto!=null && tipoConto.equalsIgnoreCase(WFConst.TIPO_CONTO_DEFAULT_AMMINISTRAZIONE_PRIVATO)) {
return decimalFormatter2.format(c.getTotale());
}else if (arg1 == 4 && tipoConto!=null && tipoConto.equalsIgnoreCase(WFConst.TIPO_CONTO_DEFAULT_AMMINISTRAZIONE_PUBBLICO)) {
return decimalFormatter2.format(c.getTotalePubblico());
}
}catch(Exception e){
e.printStackTrace();
}
return arg0;
}
}
}
这是TreeTableRenderer,我认为问题出现在这个类中,因为如果我尝试执行Debug,我就不能进入getTreeCellRendererComponent。
public class CustomTreeTableSpeseXCategoriaSpese extends JLabel implements TreeCellRenderer, TableCellRenderer {
/**
*
*/
DefaultTreeCellRenderer defaultRenderer = new DefaultTreeCellRenderer();
private static final long serialVersionUID = 4842418316518803090L;
private Font fontTotale = new Font("Verdana", Font.BOLD, 12);
private Font fontNegativo = new Font("Verdana", Font.BOLD, 12);
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
setOpaque(true);
if(column ==1){
setHorizontalAlignment(SwingConstants.LEFT);
}else{
setHorizontalAlignment(SwingConstants.RIGHT);
}
if (row== table.getRowCount()-1) {
setForeground(Color.BLACK);
setBackground( Color.RED );
setFont(fontTotale);
}else if(row != table.getRowCount() && column !=4){
setForeground( Color.BLACK );
setBackground(new Color(200, 200, 200));
setFont(UtilitySwing.getTableFont());
}else if(row != table.getRowCount()-1 && column ==4){
String valore = value.toString();
if(valore.startsWith("-")){
setForeground(Color.red);
setFont(fontNegativo);
}else{
setForeground(Color.blue);
setFont(fontNegativo);
}
}
setText(value != null ? value.toString() : "<null>");
return this;
}
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf, int row,
boolean hasFocus) {
setOpaque(true);
if(expanded){
setForeground(Color.PINK);
setBackground( Color.PINK );
setFont(fontTotale);
}
return this;
}
}
答案 0 :(得分:1)
您已使用TreeCellRenderer
,所以您已经关闭了。
您需要做的就是访问模型(类似DefaultTreeModel
)并使用getPathToRoot()
来查看树的深度并相应地更改渲染器。
一个例子:
DefaultTreeCellRenderer defaultTreeCellRenderer = new DefaultTreeCellRenderer() {
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf, int row,
boolean hasFocus) {
setOpaque(true);
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
if (model.getPathToRoot((TreeNode) value).length > 2) {
//Do your rendering goodness here
} else if (expanded) {
setForeground(Color.PINK);
setBackground(Color.PINK);
setFont(fontTotale);
}
return this;
}
};
注意我做了一些假设,所以请相应地纠正您的需求。
DefaultTreeModel
TreeNode
另外,请查看此功能的JavaDoc以确保它符合您的需要。