Swing 如何在JTree中突出显示新创建的文件

Swing 如何在JTree中突出显示新创建的文件,swing,file,jtree,selected,Swing,File,Jtree,Selected,我想让它像当我点击一个按钮,它将创建一个新的文件。然后jTree将突出显示新文件。下面是我的代码。当前我创建新文件,我将显示新文件,但不突出显示文件 class FileTreeModel implements TreeModel { private FileNode root; public FileTreeModel(String directory) { root = new FileNode(directory); } public Object getRoot() {

我想让它像当我点击一个按钮,它将创建一个新的文件。然后jTree将突出显示新文件。下面是我的代码。当前我创建新文件,我将显示新文件,但不突出显示文件

class FileTreeModel implements TreeModel {
private FileNode root;

public FileTreeModel(String directory) {
    root = new FileNode(directory);
}

public Object getRoot() {
    return root;
}

public Object getChild(Object parent, int index) {
    FileNode parentNode = (FileNode) parent;
    return new FileNode(parentNode, parentNode.listFiles()[index].getName());
}

public int getChildCount(Object parent) {
    FileNode parentNode = (FileNode) parent;
    if (parent == null || !parentNode.isDirectory()
            || parentNode.listFiles() == null) {
        return 0;
    }

    return parentNode.listFiles().length;
}

public boolean isLeaf(Object node) {
    return (getChildCount(node) == 0);
}

public int getIndexOfChild(Object parent, Object child) {
    FileNode parentNode = (FileNode) parent;
    FileNode childNode = (FileNode) child;

    return Arrays.asList(parentNode.list()).indexOf(childNode.getName());
}

public void valueForPathChanged(TreePath path, Object newValue) {

}

public void addTreeModelListener(TreeModelListener l) {
}

public void removeTreeModelListener(TreeModelListener l) {
}
}

class FileNode extends java.io.File {

public FileNode(String directory) {
    super(directory);
}

public FileNode(FileNode parent, String child) {
    super(parent, child);
}

@Override
public String toString() {
    return getName();

}
}

jTree = new JTree();
jTree.setBounds(new Rectangle(164, 66, 180, 421));
jTree.setBackground(SystemColor.inactiveCaptionBorder);
jTree.setBorder(BorderFactory.createTitledBorder(null, "",
TitledBorder.LEADING, TitledBorder.TOP, new Font("Arial",
                        Font.BOLD, 12), new Color(0, 0, 0)));
FileTreeModel model = new FileTreeModel(root);
jTree.setRootVisible(false);
jTree.setModel(model);
expandAll(jTree);

public void expandAll(JTree tree) {

    int row = 0;
    while (row < tree.getRowCount()) {
        tree.expandRow(row);
        row++;
    }
}
类FileTreeModel实现TreeModel{
私有文件节点根;
公共文件树模型(字符串目录){
root=新文件节点(目录);
}
公共对象getRoot(){
返回根;
}
公共对象getChild(对象父对象,int索引){
FileNode parentNode=(FileNode)父节点;
返回新的文件节点(parentNode,parentNode.listFiles()[index].getName());
}
public int getChildCount(对象父对象){
FileNode parentNode=(FileNode)父节点;
if(parent==null | |!parentNode.isDirectory()
||parentNode.listFiles()==null){
返回0;
}
返回parentNode.listFiles().length;
}
公共布尔isLeaf(对象节点){
返回(getChildCount(节点)==0);
}
public int getIndexOfChild(对象父对象、对象子对象){
FileNode parentNode=(FileNode)父节点;
FileNode childNode=(FileNode)子节点;
返回Arrays.asList(parentNode.list()).indexOf(childNode.getName());
}
public void valueForPathChanged(树路径,对象newValue){
}
public void addTreeModelListener(TreeModelListener l){
}
公共void removeTreeModelListener(TreeModelListener l){
}
}
类FileNode扩展了java.io.File{
公共文件节点(字符串目录){
超级(目录);
}
公共文件节点(文件节点父节点、字符串子节点){
超级(父母、子女);
}
@凌驾
公共字符串toString(){
返回getName();
}
}
jTree=新的jTree();
jTree.setBounds(新矩形(16466180421));
jTree.setBackground(SystemColor.inactiveCaptionBorder);
jTree.setboorder(BorderFactory.createTitledBorder)(null,“,
TitledBorder.LEADING,TitledBorder.TOP,新字体(“Arial”,
字体粗体,12),新颜色(0,0,0));
FileTreeModel model=新的FileTreeModel(根);
jTree.setRootVisible(false);
jTree.setModel(model);
expandAll(jTree);
公共void expandAll(JTree树){
int行=0;
while(行
我想您可以使用
setSelectedRow
功能

编辑:为解决方案添加草图

您需要有一个
树模型
,用于从文件系统(原始)读取文件:

最后,您将为新创建的文件返回
TreePath

private TreePath getPathFor(FileSystemModel model, File root, File searched)
{
    TreePath path = getPath(model, null, root, searched);

    return path;
}

private TreePath getPath(FileSystemModel model, TreePath path, File parent, File searched)
{
    if (path == null)
    {
        path = new TreePath(parent);
    }
    else if (parent.isDirectory())
    {

        path = path.pathByAddingChild(parent);
    }

    if (parent.getAbsolutePath().equals(searched.getAbsolutePath()))
    {
        return path.pathByAddingChild(parent);
    }

    for (int i = 0; i < model.getChildCount(parent); i++)
    {
        File child = ((File)model.getChild(parent, i)).getAbsoluteFile();
        TreePath found = getPath(model, path, child, searched);

        if (found != null)
        {
            return found;
        }
    }

    return null;
}
private TreePath getPathFor(文件系统模型、文件根目录、搜索的文件)
{
TreePath=getPath(模型,空,根,已搜索);
返回路径;
}
私有树路径getPath(文件系统模型、树路径、文件父级、搜索的文件)
{
if(路径==null)
{
路径=新树路径(父级);
}
else if(parent.isDirectory())
{
path=path.pathByAddingChild(父级);
}
if(parent.getAbsolutePath().equals(searched.getAbsolutePath()))
{
返回path.pathByAddingChild(父级);
}
for(int i=0;i

这只是一个演示,演示了如何做到这一点,尽管它非常不完善,因为它每次都会重新创建模型。我相信你能想出一个更好的解决方案。

如何将新文件设置为第一行,文件按字母排序我将简略地绘制一个示例它工作正常非常感谢我可以只显示文件夹而不是整个路径,例如我的根目录是“src/file”,然后是我的file=“src/file/my Note/new file”我可以只显示我的笔记/文件吗?或者显示整个路径是的,你可以。只需创建一个子类
File
并覆盖
toString()
方法,只返回文件名,而不是文件的完整路径。嗨,我创建了一个类FileNode extensed java.io.File{public FileNode(String directory){super(directory);}public FileNode(FileNode parent,String child){super(parent,child);}@Override public String toString(){return]getName();}}}这是我从网站上得到的代码。但它仍然是一条完整的道路
private void myButtonActionPerformed(java.awt.event.ActionEvent evt) {
    File f = new File(MY_DIR + "/file" + new Random().nextInt());
    try {
        f.createNewFile();

        FileSystemModel model = new FileSystemModel(new File(MY_DIR));
        tree.setModel(model);

        File root = (File) tree.getModel().getRoot();
        TreePath path = getPathFor(model, root, f);

        tree.expandPath(path);
        tree.setSelectionPath(path);
    }
    catch (IOException e)
    {

    }
}
private TreePath getPathFor(FileSystemModel model, File root, File searched)
{
    TreePath path = getPath(model, null, root, searched);

    return path;
}

private TreePath getPath(FileSystemModel model, TreePath path, File parent, File searched)
{
    if (path == null)
    {
        path = new TreePath(parent);
    }
    else if (parent.isDirectory())
    {

        path = path.pathByAddingChild(parent);
    }

    if (parent.getAbsolutePath().equals(searched.getAbsolutePath()))
    {
        return path.pathByAddingChild(parent);
    }

    for (int i = 0; i < model.getChildCount(parent); i++)
    {
        File child = ((File)model.getChild(parent, i)).getAbsoluteFile();
        TreePath found = getPath(model, path, child, searched);

        if (found != null)
        {
            return found;
        }
    }

    return null;
}