重绘后java swing tree不刷新

时间:2014-02-26 10:18:45

标签: java swing resize jtree preferredsize

我想在树大小调整后应用斩波树的节点数据。 节点数据始终可见树的70%区域。

以下是在滚动面板中添加树的代码。

    tree = new JTree();     
    scrollPane = new JScrollPane();
    scrollPane.setMinimumSize(new Dimension(711, 313));
    scrollPane.setMaximumSize(Toolkit.getDefaultToolkit().getScreenSize());

    scrollPane.setViewportView(tree);

以下是重新调整大小的事件

@Override
public void componentResized(ComponentEvent arg0) { 
    TreeLeafUtil.setWidth((((int)(((JTree)arg0.getSource()).getWidth())))); 
    ((JTree)arg0.getSource()).repaint();
}

TreeLeafUtil.java

public String applyChooping(String recordValue){
    setNoOfCharecterToDisplay(recordValue);
    int maxLength = (recordValue.length() <= MAX_CHAR)? recordValue.length():(MAX_CHAR + 1);
    if (maxLength > MAX_CHAR) {
        recordValue = recordValue.substring(0, (maxLength - 1)) + "...";
    } else {
        recordValue = recordValue.substring(0, maxLength) ;
    }
    return recordValue;
}
private void setNoOfCharecterToDisplay(String displayStr){
    int actualLength = displayStr.length();
    if (actualLength > 0) {
        int reqWidth = fontMetrics.stringWidth(displayStr);
        //for one char
        int forOnechar = (reqWidth/actualLength);
        if (component == null) {
             MAX_CHAR =  (int) ((TreeLeafUtil.width  * 0.7)/forOnechar);   
        } else {
             MAX_CHAR =  (int) ((component.getWidth() * 0.7)/forOnechar);   
        }
    }

}

我们有custome叶节点并包含toString方法如下

@Override
public String toString() 
     {
        return new TreeLeafUtil().applyChooping(video.getRecord());
     }

0 个答案:

没有答案