The checkboxes appear to be normal JCheckBox and not the TristateCheckBox:
- All checkboxes in the tree show either "selected" (white background, with "V") or "unselected" (white background only),
even when they should show "mixed" because only a descendant was selected. - Once a node is selected using the mouse, none of its descendants can be unselected; i.e. when you click
on the checkbox of a descendant, the checkbox remains selected.
Note that FileSystemModel never changes, so valueForPathChanged, addTreeModelListener, removeTreeModelListener
are just stubs.
I link with Common Layer source code that I got recently from the subversion repository; it's revision 2138 .
Am I missing something simple here? Any help is appreciated.
Paul van Bemmelen.
- Code: Select all
import java.io.*;
import javax.swing.*;
import com.jidesoft.swing.*;
@SuppressWarnings("serial")
public class FileTreeFrame extends JFrame {
private CheckBoxTree fileTree;
private FileSystemModel fileSystemModel;
public FileTreeFrame(String directory) {
super("JTree FileSystem Viewer");
fileSystemModel = new FileSystemModel(new File(directory));
fileTree = new CheckBoxTree(fileSystemModel) {
public String convertValueToText(Object value, boolean selected,
boolean expanded, boolean leaf, int row, boolean hasFocus) {
return ((File)value).getName();
}
};
fileTree.setEditable(false);
fileTree.getCheckBoxTreeSelectionModel().setDigIn(true);
getContentPane().add(new JScrollPane(fileTree));
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(640, 480);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
e.printStackTrace();
}
new FileTreeFrame("c:\\");
}
});
}
}
- Code: Select all
import java.io.*;
import javax.swing.event.*;
import javax.swing.tree.*;
public class FileSystemModel implements TreeModel {
private File root;
public FileSystemModel(File rootDirectory) {
root = rootDirectory;
}
public Object getRoot() {
return root;
}
public Object getChild(Object parent, int index) {
File directory = (File) parent;
String[] children = directory.list();
return new File(directory, children[index]);
}
public int getChildCount(Object parent) {
File file = (File) parent;
if (file.isDirectory()) {
String[] fileList = file.list();
if (fileList != null)
return file.list().length;
}
return 0;
}
public boolean isLeaf(Object node) {
File file = (File) node;
return file.isFile();
}
public int getIndexOfChild(Object parent, Object child) {
File directory = (File) parent;
File file = (File) child;
String[] children = directory.list();
for (int i = 0; i < children.length; i++) {
if (file.getName().equals(children[i])) {
return i;
}
}
return -1;
}
public void valueForPathChanged(TreePath path, Object value) {
System.out.println("valueForPathChanged(...)");
}
public void addTreeModelListener(TreeModelListener listener) {
// do nothing: model never changes.
}
public void removeTreeModelListener(TreeModelListener listener) {
// do nothing: model never changes.
}
}