当前位置: 技术问答>java相关
怎样在JTree里头加JCheckBox????在线等待。有例子的加100分
来源: 互联网 发布时间:2015-09-28
本文导语: 怎样让JTree中的每一个结点变成一个JCheckBox,而不是原来的文件夹和文件的模样? | 我告诉你大体的框架 class MyCheckBoxTree extends JCheckBox implements TreeCellRenderer { Border e = new EmptyBorder(...
怎样让JTree中的每一个结点变成一个JCheckBox,而不是原来的文件夹和文件的模样?
|
我告诉你大体的框架
class MyCheckBoxTree extends JCheckBox implements TreeCellRenderer {
Border e = new EmptyBorder(1, 1, 1, 1);
public MyCheckBoxTree() {
super();
setOpaque(true);
setBorder(e);
}
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
setText(value.toString());
//this.setBackground(selectesm a d?tree.getSelectionPath():tree.getBackground());
//this.setForeground(selected?tree.getForeground());
setBackground(selected && !hasFocus ?
tree.getBackground() : tree.getBackground());
setForeground(selected && !hasFocus ?
tree.getForeground(): tree.getForeground());
if (value instanceof Boolean) {
Boolean b = (Boolean) value;
setSelected(b.booleanValue());
}
setFont(tree.getFont());
setBorder(hasFocus ? UIManager.getBorder(
"Table.focusCellHighlightBorder") : e);
//this.setFont(tree.getFont());
return this;
}
}
class MyCheckBoxTree extends JCheckBox implements TreeCellRenderer {
Border e = new EmptyBorder(1, 1, 1, 1);
public MyCheckBoxTree() {
super();
setOpaque(true);
setBorder(e);
}
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
setText(value.toString());
//this.setBackground(selectesm a d?tree.getSelectionPath():tree.getBackground());
//this.setForeground(selected?tree.getForeground());
setBackground(selected && !hasFocus ?
tree.getBackground() : tree.getBackground());
setForeground(selected && !hasFocus ?
tree.getForeground(): tree.getForeground());
if (value instanceof Boolean) {
Boolean b = (Boolean) value;
setSelected(b.booleanValue());
}
setFont(tree.getFont());
setBorder(hasFocus ? UIManager.getBorder(
"Table.focusCellHighlightBorder") : e);
//this.setFont(tree.getFont());
return this;
}
}