java获取jtree节点字段_java swing根据字符串搜索定位到JTree某一个节点上或JTable某一行上...

这个Java程序演示了如何在Swing应用中实现根据输入字符串定位JTree的特定节点和JTable的特定行。通过JOptionPane显示对话框获取搜索字符串,然后在JTable和JTree中遍历并高亮显示匹配项。
摘要由CSDN通过智能技术生成

package demo;

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);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值