我遇到了自定义TreeCellRenderer的问题。 当我点击一个节点时,可覆盖的方法getTreeCellRendererComponent执行大约4次。有人对此有所了解吗? 先谢谢。
西蒙
我的代码:
@Override
public Component getTreeCellRendererComponent(JTree tree, Object p_value,
boolean selected, boolean expanded, boolean leaf, int row,
boolean hasFocus) {
super.getTreeCellRendererComponent(tree, p_value, selected, expanded,
leaf, row, hasFocus);
if (row != -1) {
DefaultMutableTreeNode aNode = (DefaultMutableTreeNode) p_value;
TreePath treePath;
treePath = tree.getPathForRow(row);
if (treePath == null) {
return this;
}
// int pathCount = treePath.getPathCount();
JLabel label = (JLabel) this;
if (aNode.getUserObject() instanceof MlCompteMail) {
traiteNomCompte((MlCompteMail) aNode.getUserObject(), label);
// JTreeFactory treeFact = new JTreeFactory();
// treeFact.refreshNode(treePath);
return this;
} else if (aNode.getUserObject() instanceof MlDossier) {
traiteNomDossier((MlDossier) aNode.getUserObject(), label, leaf);
// JTreeFactory treeFact = new JTreeFactory();
// treeFact.refreshNode(treePath);
return this;
}
}
return this;
}
/**
* @param value
* @param treePath
* @param label
* @param p_leaf
*/
private void traiteNomDossier(MlDossier p_dossier, JLabel label,
boolean p_leaf) {
String nomDossier = p_dossier.getNomDossier();
if (nomDossier.equals(EnDossierBase.BROUILLON.getLib())) {
label.setIcon(IconeTreeFactory.getBrouillon());
} else if (nomDossier.equals(EnDossierBase.CORBEILLE.getLib())) {
label.setIcon(IconeTreeFactory.getCorbeille());
} else if (nomDossier.equals(EnDossierBase.ENVOYES.getLib())) {
label.setIcon(IconeTreeFactory.getEnvoye());
} else if (nomDossier.equals(EnDossierBase.RECEPTION.getLib())) {
label.setIcon(IconeTreeFactory.getReception());
} else if (nomDossier.equals(EnDossierBase.SPAM.getLib())) {
label.setIcon(IconeTreeFactory.getSpam());
} else if (p_leaf) {
label.setIcon(IconeTreeFactory.getDossierFerme());
} else {
label.setIcon(IconeTreeFactory.getDossierOuvert());
}
int unreadMess = p_dossier.getUnreadMessCount();
if (unreadMess > 0) {
label.setText(nomDossier + " (" + unreadMess + ")");
label.setFont(Fontfactory.getTREE_FONT_GRAS());
} else {
label.setText(nomDossier);
label.setFont(Fontfactory.getTREE_FONT_PLAIN());
}
return;
}
// }
// }
/**
* @param value
* @param label
*/
private void traiteNomCompte(MlCompteMail p_compteMail, JLabel label) {
int unreadMess = p_compteMail.getUreadMessCount();
if (unreadMess > 0) {
label
.setText(p_compteMail.getNomCompte() + " (" + unreadMess
+ ")");
label.setFont(Fontfactory.getTREE_FONT_GRAS());
} else {
label.setText(p_compteMail.getNomCompte());
label.setFont(Fontfactory.getTREE_FONT_PLAIN());
}
label.setIcon(IconeTreeFactory.getDossierOuvert());
}
答案 0 :(得分:3)
这可能不是问题;只要需要绘制单元格,就会调用该方法。你没有控制它的奢侈。
因此,设计方法非常有效,并确保它没有副作用,你就没有问题。
答案 1 :(得分:0)
我找到了加快jtree装饰的方法, jtree使用方法“toString()”来绘制一个节点,所以我已经覆盖了我的两种节点中的方法,因此,在我的customTreeCellRenderer中,我只是在我的userObject上调用toString方法并且工作正常。 谢谢你的帮助。