我想从保存在数据库中的文件夹路径创建一个jtree TestFolder / ABCD / CDEF / ghml TestFolder / ABCD / THYU / UVWZ iam只保存路径而不是父结构所以当我尝试创建树结构时它适用于单路径(TestFolder / ABCD / CDEF / ghml)我想要建立一个完整的树结构,如果任何人有想法请帮忙
public class MainClass {
public static void main(String[] a) throws ClassNotFoundException, SQLException {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JTreeEvents());
f.setSize(500, 500);
f.setVisible(true);
}
}
enter code here
class JTreeEvents extends JPanel {
private String driver = new String("com.mysql.jdbc.Driver");
private String url = new String("jdbc:mysql:);
private String user = new String("root");
private String password = new String("128");
private Connection dbCon;
List<String> arrayList;
Map<String, Map<Integer, String>> map;
String[] folderArr;
PreparedStatement ps = null;
java.sql.Statement s;
ResultSet r;
DefaultMutableTreeNode top;
DefaultTreeModel dt;
DefaultMutableTreeNode trees;
DefaultTreeModel model;
DefaultMutableTreeNode child, subchild;
//tree
JTree jtree;
JTextField jtf;
public JTreeEvents() throws ClassNotFoundException, SQLException {
//------------------------------------------------------------------
Class.forName(driver);
dbCon = DriverManager.getConnection(url, user, password);
s = dbCon.createStatement();
r = s.executeQuery("SELECT * FROM project p where p.name='MyFolder4'");
arrayList = new ArrayList<String>();
map = new HashMap<String, Map<Integer, String>>();
Hashtable<String, Object> hashtable = new Hashtable<String, Object>();
DefaultMutableTreeNode root;
MutableTreeNode parent;
DefaultMutableTreeNode parentnode;
TreePath path = null;
while (r.next()) {
arrayList.add(r.getString("path"));
}
for (String str : arrayList) {
if (str != null) {
folderArr = str.split("/");
String projName = str.split("/")[0];
root = new DefaultMutableTreeNode(projName);
model = new DefaultTreeModel(root);
jtree = new JTree(model);
for (int i = 0; i < folderArr.length; i++) {
if (i == 0) {
parent = new DefaultMutableTreeNode(folderArr[0]);
} else {
path = jtree.getNextMatch(folderArr[i - 1].toString(), 0, Position.Bias.Forward);
if (path != null) {
System.out.println(path);
}
parent = (DefaultMutableTreeNode) (MutableTreeNode) path.getLastPathComponent();
}
enter code here
model = (DefaultTreeModel) jtree.getModel();
model.insertNodeInto((DefaultMutableTreeNode) new DefaultMutableTreeNode(folderArr[i]), parent, parent.getChildCount());
model.reload();
jtree.expandPath(path);
}
}
}
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp = new JScrollPane(jtree, v, h);
add(jsp, BorderLayout.CENTER);
jtf = new JTextField("", 20);
add(jtf, BorderLayout.SOUTH);
jtree.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
doMouseClicked(me);
}
});
}
enter code here
void doMouseClicked(MouseEvent me) {
TreePath tp = jtree.getPathForLocation(me.getX(), me.getY());
if (tp != null) {
jtf.setText(tp.toString());
} else {
jtf.setText("");
}
}
private TreePath find(DefaultMutableTreeNode root, String s) {
@SuppressWarnings("unchecked")
Enumeration<DefaultMutableTreeNode> e = root.depthFirstEnumeration();
while (e.hasMoreElements()) {
DefaultMutableTreeNode node = e.nextElement();
if (node.toString().equalsIgnoreCase(s)) {
return new TreePath(node.getPath());
}
}
return null;
}
}
答案 0 :(得分:1)
在您的代码中有一些不起作用的东西。首先,构建树的代码可以像这样简化:
public JTreeEvents() throws ClassNotFoundException, SQLException {
// ... fill the array list from the database ..
for (String path: arrayList) {
DefaultMutableTreeNode currentParent = root;
String[] pathComponents = path.split("/");
for (String comp: pathComponents) {
DefaultMutableTreeNode child = findChild(currentParent, comp);
if (child == null) {
child = new DefaultMutableTreeNode(comp);
currentParent.add(child);
}
currentParent = child;
}
}
model = new DefaultTreeModel(root);
jtree = new JTree(model);
// ... build the rest of the GUI components
}
private DefaultMutableTreeNode findChild(DefaultMutableTreeNode parent, String s) {
for (int i=0; i<parent.getChildCount(); i++) {
DefaultMutableTreeNode child = (DefaultMutableTreeNode) parent.getChildAt(i);
if (s.equals(child.getUserObject())) return child;
}
return null;
}
第二件事是JPanel默认没有布局,所以你需要在向其添加任何组件之前明确地添加一个:
this.setLayout(new BorderLayout());
因此,在删除所有未使用的变量声明之后,JTreeEvent的完整代码变为:
public class JTreeEvents extends JPanel {
private String driver = new String("com.mysql.jdbc.Driver");
private String url = new String("jdbc:mysql:");
private String user = new String("root");
private String password = new String("128");
private Connection dbCon;
List<String> arrayList;
PreparedStatement ps = null;
java.sql.Statement s;
ResultSet r;
DefaultMutableTreeNode root = new DefaultMutableTreeNode("/");
DefaultTreeModel model;
JTree jtree;
JTextField jtf;
public JTreeEvents() throws ClassNotFoundException, SQLException {
Class.forName(driver);
dbCon = DriverManager.getConnection(url, user, password);
s = dbCon.createStatement();
r = s.executeQuery("SELECT * FROM project p where p.name='MyFolder4'");
arrayList = new ArrayList<String>();
while (r.next()) {
arrayList.add(r.getString("path"));
}
for (String path: arrayList) {
DefaultMutableTreeNode currentParent = root;
String[] pathComponents = path.split("/");
for (String comp: pathComponents) {
DefaultMutableTreeNode child = findChild(currentParent, comp);
if (child == null) {
child = new DefaultMutableTreeNode(comp);
currentParent.add(child);
}
currentParent = child;
}
}
setLayout(new BorderLayout());
model = new DefaultTreeModel(root);
jtree = new JTree(model);
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp = new JScrollPane(jtree, v, h);
add(jsp, BorderLayout.CENTER);
jtf = new JTextField("", 20);
add(jtf, BorderLayout.SOUTH);
jtree.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
doMouseClicked(me);
}
});
}
void doMouseClicked(MouseEvent me) {
TreePath tp = jtree.getPathForLocation(me.getX(), me.getY());
if (tp != null) {
jtf.setText(tp.toString());
} else {
jtf.setText("");
}
}
private DefaultMutableTreeNode findChild(DefaultMutableTreeNode parent, String s) {
for (int i=0; i<parent.getChildCount(); i++) {
DefaultMutableTreeNode child = (DefaultMutableTreeNode) parent.getChildAt(i);
if (s.equals(child.getUserObject())) return child;
}
return null;
}
}
我在没有数据库的情况下测试它,通过在我的代码中手动添加路径到数组列表,它运行良好: