java swing根据字符串搜索定位到JTree某一个节点上或JTable某一行上
2017-04-07 15:20
330 查看
有时需要根据字符串快速定位到结构树中界面的具体位置,下面在网上找到的一个例子,不错。拿来分享下。
效果图如下:
代码如下:
原文链接:http://www.zuidaima.com/code/file/2664499515737088.htm?dir=/2664499515737088.java
效果图如下:
代码如下:
/******************************************************************************* * @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
相关文章推荐
- Eclipse java 文件编辑器 根据字符串定位光标位置
- 根据字符串定位JTree节点和JTable行
- Java Swing JTable
- JAVA jtable 问题,如何建立简单的表格+得到被选中的那一行!
- java从入门到精髓 - Swing表格自定义JTable 选中行及取消选中
- 转载:Java 将一行字符串追加至文件末尾的三种方法
- 树的节点搜索定位及变色显示
- 在某个文件中搜索指定的字符串的java方法
- Java Swing写的支持合并单元格的JTable
- java从入门到精髓 - Swing表格JTable不支持滚动
- java从入门到精髓 - Swing表格JTable支持滚动
- 根据Key定位ViewObject中的一行的两种方法
- 疯狂JAVA讲义---第十二章:Swing编程(八)JTable表格
- 根据字节数截取字符串... 分类: java 2009-11-05 19:21 235人阅读 评论(0) 收藏
- 在Java Swing的JTable显示数据库中的记录
- java从入门到精髓 - Swing表格自定义JTable
- java swing 导出Jtable里面的数据到excel
- 回复:String中如果有中文,如何保证length()返回的长度 在 Java 中字符串是以 Unicode 方式编码的,所以,无论中英文都算的长度为 1。根据你的描述,估计是想要取得英文长度为 1 中文长度为 2 的总长度吧。这个,理论上可以构造 An
- java swing sql->jtable表格 查询
- JAVA判断中英文,并根据中英文自动截取字符串