我正在尝试使应用程序从xml加载用户数据以创建树模型,然后用户可以选择从树中选择一段数据并为其设置图标。
我可以从xml文件成功加载数据,但问题是我无法为树的每个节点设置不同的图标
这是我的表现
public class DefaultTreeRenderer extends DefaultTreeCellRenderer {
private static final long serialVersionUID = 1L;
private Icon userIcon;
private int getSelecteRow;
public DefaultTreeRenderer(String IconPath,int selected) throws IOException {
Image img = ImageIO.read(new File(IconPath)) ;
Image newimg = img.getScaledInstance( 28, 28, java.awt.Image.SCALE_SMOOTH ) ;
Icon x = new ImageIcon(newimg);
userIcon= x;
getSelecteRow = selected;
}
@Override
public Component getTreeCellRendererComponent(
JTree tree,
Object value,
boolean sel,
boolean expanded,
boolean leaf,
int row,
boolean hasFocus) {
JLabel label = (JLabel) super.getTreeCellRendererComponent(
tree, value, sel,
expanded, leaf, row,
hasFocus);
if (?!!!) {
// This's the missing part, what should i state to set per row icon ?
label.setIcon(tutorialIcon);
label.setText(label.getText()+" √");
} else{
}
return label;
}
}
测试代码:
try {
jTree1.setCellRenderer(new DefaultTreeRenderer(iconPathTextBox.getText(),jTree1.getMinSelectionRow()));
} catch (IOException ex) {
Logger.getLogger(frame.class.getName()).log(Level.SEVERE , null , ex);
}
答案 0 :(得分:0)
public class DefaultTreeRenderer extends DefaultTreeCellRenderer {
private static final long serialVersionUID = 1L;
Map<String,Icon> amap = new HashMap<>();
ArrayList<String > drawList = new ArrayList<>();
public DefaultTreeRenderer(ArrayList<String > list,Map<String,Icon> amap1) throws IOException {
setData(list,amap1);
}
private void setData(ArrayList<String > lists,Map<String,Icon> amap2){
amap = amap2;
drawList = lists;
}
@Override
public Component getTreeCellRendererComponent(
JTree tree,
Object value,
boolean sel,
boolean expanded,
boolean leaf,
int row,
boolean hasFocus) {
super.getTreeCellRendererComponent(
tree, value, sel,
expanded, leaf, row,
hasFocus);
Object nodeInfo = ((DefaultMutableTreeNode) value).getUserObject();
Icon bn = this.leafIcon;
if (getDrawable(nodeInfo)) {
setIcon(amap.get(nodeInfo.toString()));
}
if(this.getIcon()!=null && leaf) {
setText(nodeInfo.toString()+" √");
}
if(this.getIcon()==null && leaf){
setIcon(bn);
}
return this;
}
protected boolean getDrawable(Object value) {
String title = value.toString();
for(String c : drawList){
if (title.equals(c)) {
return true;
}
}
return false;
}
}
测试代码:
ArrayList<String> xmllist= new ArrayList<>();
Map<String,Icon> amap = new HashMap<>();
if(g.getName().endsWith("png") ||g.getName().endsWith("jpg") ) {
try {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)
jTree1.getLastSelectedPathComponent();
Object nodeInfo = node.getUserObject();
String hn = imagePathTextBox.getText();
Image img = ImageIO.read(new File(hn)) ;
Image newimg = img.getScaledInstance( 28, 28, java.awt.Image.SCALE_SMOOTH ) ;
Icon x = new ImageIcon(newimg);
amap.put(nodeInfo.toString() , x);
jTree1.setCellRenderer(new DefaultTreeRenderer(xmllist,amap));
} catch (IOException ex) {
Logger.getLogger(frame.class.getName()).log(Level.SEVERE , null , ex);
}
}