gpt4 book ai didi

Java Swing JTree : how to change the focus on the last created node

转载 作者:行者123 更新时间:2023-12-01 16:36:35 27 4
gpt4 key购买 nike

我的项目中有一个 Swing Jtree。我有一个 addNewItem 方法,它允许创建一个新节点。问题是如何在创建新节点后自动将焦点从当前元素更改为最后创建的节点,如图所示?

enter image description here

public void addNewItem(){
DefaultMutableTreeNode node = ((DefaultMutableTreeNode)getTree().getLastSelectedPathComponent());
if(node==null) return;
Holder h_node = (Holder) node.getUserObject();
String key = h_node.getData().getClass().getSimpleName();
BaseNodeAdapter adp = BaseNodeAdapter.of(key, h_node.getData());
while(adp==null && node!=null){
node = (DefaultMutableTreeNode) node.getParent();
h_node = (Holder) node.getUserObject();
key = h_node.getData().getClass().getSimpleName();
adp = BaseNodeAdapter.of(key, h_node.getData());
}
if(adp==null) return; // no adapter

System.out.println("addNewItem: adapter "+adp.getClass().getSimpleName());

String[] selectable = adp.getSelectable();
if(selectable.length==0) return; // no selectable
String strPath = Tools.getPath(node);
String type = (String)JOptionPane.showInputDialog(this,L("Create_a_new_item_for")+" "+key+":",strPath,JOptionPane.QUESTION_MESSAGE,null,selectable,null);
if(type!=null){
int res = adp.addItem(type);
System.out.println("add item : "+res);
if(res==BaseNodeAdapter.FAILED) return; // nothing
if(res==BaseNodeAdapter.PARENT) rebuildNode((DefaultMutableTreeNode) node.getParent());
else rebuildNode(node);
}

}

最佳答案

类似于:

TreePath current = new TreePath(nodeYouJustCreated.getPath());
tree.setSelectionPath( current );

编辑:

只需单击按钮即可添加更多节点:

import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.tree.*;

public class TreeInsertMRE extends JPanel
{
private JTree tree;
private Random random = new Random();
private int tableNumber = 20;

public TreeInsertMRE()
{
super(new BorderLayout());

//Create the nodes.
DefaultMutableTreeNode top = new DefaultMutableTreeNode("Database");
getDatabases(top);

tree = new JTree(top);
JScrollPane scrollPane = new JScrollPane(tree);
add(scrollPane);

JButton button = new JButton("Add Table to Database 1");
add(button, BorderLayout.PAGE_END);

button.addActionListener(e ->
{
DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
DefaultMutableTreeNode child = (DefaultMutableTreeNode)model.getChild(root, 1);

String tableText = "Table " + tableNumber++;
DefaultMutableTreeNode tableNode = new DefaultMutableTreeNode( tableText );
child.add(tableNode);
model.reload(child);

TreePath current = new TreePath(tableNode.getPath());
tree.setSelectionPath( current );
});
}

private void getDatabases(DefaultMutableTreeNode top)
{
for (int i = 0; i < 5; i++)
{
String databaseName = "Database" + i;
DefaultMutableTreeNode databaseNode = new DefaultMutableTreeNode(databaseName);
top.add(databaseNode);
getTables(databaseName, databaseNode);
}
}

private void getTables(String databaseName, DefaultMutableTreeNode databaseNode)
{
for (int i = 0; i < 5; i++)
{
String tableName = "Table" + i;
DefaultMutableTreeNode tableNode = new DefaultMutableTreeNode(tableName);
databaseNode.add(tableNode);
getColumns(tableName, tableNode);
}
}

private void getColumns(String tableName, DefaultMutableTreeNode tableNode)
{
for (int i = 0; i < 5; i++)
{
String columnName = "Column" + i;
DefaultMutableTreeNode columnNode = new DefaultMutableTreeNode(columnName);
tableNode.add(columnNode);
}
}

private static void createAndShowGUI()
{
JFrame frame = new JFrame("TreeInsertMRE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TreeInsertMRE());
frame.setSize(300, 300);
frame.setVisible(true);
}

public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

关于Java Swing JTree : how to change the focus on the last created node,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61936598/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com