您的位置:首页 > 编程语言 > Java开发

java swing根据字符串搜索定位到JTree某一个节点上或JTable某一行上

2017-04-07 15:20 330 查看
有时需要根据字符串快速定位到结构树中界面的具体位置,下面在网上找到的一个例子,不错。拿来分享下。

效果图如下:





代码如下:

/*******************************************************************************
* @project: JavaSE
* @package: com.burns.swing
* @file: T.java
* @author: Administrator
* @created: 2017-4-7
* @purpose:
*
* @version: 1.0
*
* Revision History at the end of file.
*
* Copyright 2017 AcconSys All rights reserved.
******************************************************************************/

package com.burns.swing;

import java.awt.BorderLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

/**
* swing根据字符串定位 (1)定位到JTree某一个节点上。 (2)定位到JTable某一行上。
*
* @author WangJian
*
*/
@SuppressWarnings("serial")
public class FindInTableAndTree extends JFrame implements ActionListener {
private JToolBar toolbar = new JToolBar();

private JButton findInTableBtn = new JButton("Find in Table");

private JButton findInTreeBtn = new JButton("Find in Tree");

private JTable table = null;

private JTree tree = null;

private JScrollPane tableSp = new JScrollPane();

private JScrollPane treeSp = new JScrollPane();

private JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
tableSp, treeSp);

public FindInTableAndTree() {
super("FindInTableAndTree");

table = new JTable(createTableModel());
tree = new JTree();

tableSp.setViewportView(table);
treeSp.setViewportView(tree);

toolbar.add(findInTableBtn);
toolbar.add(findInTreeBtn);

findInTableBtn.addActionListener(this);
findInTreeBtn.addActionListener(this);

getContentPane().add(splitPane, BorderLayout.CENTER);
getContentPane().add(toolbar, BorderLayout.NORTH);
}

private TableModel createTableModel() {
String[] columnNames = { "colors", "sports", "food" };
String[][] data = { { "blue", "violet", "red", "yellow" },
{ "basketball", "soccer", "football", "hockey" },
{ "hot dogs", "pizza", "ravioli", "bananas" } };

DefaultTableModel model = new DefaultTableModel(data, columnNames);
return model;
}

public void actionPerformed(ActionEvent e) {
String str = JOptionPane.showInputDialog(this, "Find:", "Find",
JOptionPane.QUESTION_MESSAGE);
if (str != null) {
if (e.getSource() == findInTableBtn) {
findInTable(str);
} else {
findInTree(str);
}
}
}

private void findInTree(String str) {
Object root = tree.getModel().getRoot();
TreePath treePath = new TreePath(root);
treePath = findInPath(treePath, str);
if (treePath != null) {
tree.setSelectionPath(treePath);
tree.scrollPathToVisible(treePath);
}
}

private TreePath findInPath(TreePath treePath, String str) {
Object object = treePath.getLastPathComponent();
if (object == null) {
return null;
}

String value = object.toString();
if (str.equals(value)) {
return treePath;
} else {
TreeModel model = tree.getModel();
int n = model.getChildCount(object);
for (int i = 0; i < n; i++) {
Object child = model.getChild(object, i);
TreePath path = treePath.pathByAddingChild(child);

path = findInPath(path, str);
if (path != null) {
return path;
}
}
return null;
}
}

private void findInTable(String str) {
int rowCount = table.getRowCount();
int columnCount = table.getColumnCount();
for (int i = 0; i < rowCount; i++) {
for (int k = 0; k < columnCount; k++) {
Object value = table.getValueAt(i, k);
if (str.equals(value)) {
table.getSelectionModel().setSelectionInterval(i, i);
Rectangle cellRect = table.getCellRect(i, k, true);
table.scrollRectToVisible(cellRect);
return;
}
}
}
}

public static void main(String[] args) {
FindInTableAndTree f = new FindInTableAndTree();
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.splitPane.setDividerLocation(0.5);
}
}

/*******************************************************************************
* <B>Revision History</B><BR>
* [type 'revision' and press Alt + / to insert revision block]<BR>
*
*
*
* Copyright 2017 AcconSys All rights reserved.
******************************************************************************/

原文链接:http://www.zuidaima.com/code/file/2664499515737088.htm?dir=/2664499515737088.java
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  JTree 定位