I know this question has been asked couple of times but all of the provided solutions were either incomplete for my requirements or I could not get them working. I need a JTree for a given directory path that all files and folders inside it can get selected. So then later the program can search through selected items and so on. For example, this webpage provides such a solution, but I could not get it working, there are other classes missing in this solution. Here is its code:
class CheckBoxNodeEditor extends TriStateCheckBox implements TreeCellEditor {
private DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
private final JPanel panel = new JPanel(new BorderLayout());
private String str = null;
public CheckBoxNodeEditor() {
super();
this.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
//System.out.println("actionPerformed: stopCellEditing");
stopCellEditing();
}
});
panel.setFocusable(false);
panel.setRequestFocusEnabled(false);
panel.setOpaque(false);
panel.add(this, BorderLayout.WEST);
this.setOpaque(false);
}
@Override public Component getTreeCellEditorComponent(
JTree tree, Object value, boolean isSelected,
boolean expanded, boolean leaf, int row) {
JLabel l = (JLabel)renderer.getTreeCellRendererComponent(
tree, value, true, expanded, leaf, row, true);
l.setFont(tree.getFont());
if(value != null && value instanceof DefaultMutableTreeNode) {
this.setEnabled(tree.isEnabled());
this.setFont(tree.getFont());
Object userObject = ((DefaultMutableTreeNode)value).getUserObject();
if(userObject!=null && userObject instanceof CheckBoxNode) {
CheckBoxNode node = (CheckBoxNode)userObject;
if(node.status==Status.INDETERMINATE) {
setIcon(new IndeterminateIcon());
}else{
setIcon(null);
}
l.setText(node.label);
setSelected(node.status==Status.SELECTED);
str = node.label;
}
//panel.add(this, BorderLayout.WEST);
panel.add(l);
return panel;
}
return l;
}
@Override public Object getCellEditorValue() {
return new CheckBoxNode(str, isSelected()?Status.SELECTED:Status.DESELECTED);
}
@Override public boolean isCellEditable(EventObject e) {
if(e != null && e instanceof MouseEvent && e.getSource() instanceof JTree) {
MouseEvent me = (MouseEvent)e;
JTree tree = (JTree)e.getSource();
TreePath path = tree.getPathForLocation(me.getX(), me.getY());
Rectangle r = tree.getPathBounds(path);
if(r==null) return false;
Dimension d = getPreferredSize();
r.setSize(new Dimension(d.width, r.height));
if(r.contains(me.getX(), me.getY())) {
if(str==null && System.getProperty("java.version").startsWith("1.7.0")) {
System.out.println("XXX: Java 7, only on first run\n"+getBounds());
setBounds(new Rectangle(0,0,d.width,r.height));
}
//System.out.println(getBounds());
return true;
}
}
return false;
}
@Override public void updateUI() {
super.updateUI();
setName("Tree.cellEditor");
if(panel!=null) {
//panel.removeAll(); //??? Change to Nimbus LnF, JDK 1.6.0
panel.updateUI();
//panel.add(this, BorderLayout.WEST);
}
//???#1: JDK 1.6.0 bug??? @see 1.7.0 DefaultTreeCellRenderer#updateUI()
//if(System.getProperty("java.version").startsWith("1.6.0")) {
// renderer = new DefaultTreeCellRenderer();
//}
}
However I need a tutorial or something to start from beginning and impeliment this "Check Tree".
Any idea?
Aucun commentaire:
Enregistrer un commentaire