0

我正在尝试让应用程序从 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);
    }
4

1 回答 1

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);
    }
  }
于 2013-07-14T01:11:23.590 回答