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

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

效果图如下:



代码如下:

/*******************************************************************************
 * @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




  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值