我有一个DefaultMutableTreeNodes的JTree,我想过滤它们。
当我执行过滤器时,我希望保留符合我的条件的任何节点或具有符合我标准的子节点。
这里我包含了供您参考的代码。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.border.EmptyBorder;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeModel;
public class FilteredJTreeExample extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField textField;
private JTree tree;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FilteredJTreeExample frame = new FilteredJTreeExample();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public FilteredJTreeExample() {
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.NORTH);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[]{34, 116, 0};
gbl_panel.rowHeights = new int[]{22, 0};
gbl_panel.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
gbl_panel.rowWeights = new double[]{0.0, Double.MIN_VALUE};
panel.setLayout(gbl_panel);
JLabel lblFilter = new JLabel("Search:");
GridBagConstraints gbc_lblFilter = new GridBagConstraints();
gbc_lblFilter.anchor = GridBagConstraints.WEST;
gbc_lblFilter.insets = new Insets(0, 0, 0, 5);
gbc_lblFilter.gridx = 0;
gbc_lblFilter.gridy = 0;
panel.add(lblFilter, gbc_lblFilter);
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
tree = new JTree();
tree.setEditable( true );
tree.setShowsRootHandles( false );
tree.setInvokesStopCellEditing(true);
scrollPane.setViewportView(tree);
textField = new JTextField();
GridBagConstraints gbc_textField = new GridBagConstraints();
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.anchor = GridBagConstraints.NORTH;
gbc_textField.gridx = 1;
gbc_textField.gridy = 0;
panel.add(textField, gbc_textField);
textField.setColumns(10);
textField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
TreeModel model = tree.getModel();
tree.setModel(null);
tree.setModel(model);
}
});
tree.setCellRenderer(new DefaultTreeCellRenderer() {
private JLabel lblNull = new JLabel();
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean arg2, boolean arg3, boolean arg4, int arg5, boolean arg6) {
Component c = super.getTreeCellRendererComponent(tree, value, arg2, arg3, arg4, arg5, arg6);
DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
if (matchesFilter(node)) {
c.setForeground(Color.BLACK);
return c;
}
else if (containsMatchingChild(node)) {
c.setForeground(Color.GRAY);
return c;
}
else {
return lblNull;
}
}
private boolean matchesFilter(DefaultMutableTreeNode node) {
return node.toString().contains(textField.getText());
}
private boolean containsMatchingChild(DefaultMutableTreeNode node) {
Enumeration<DefaultMutableTreeNode> e = node.breadthFirstEnumeration();
boolean isMatched = false;
while (e.hasMoreElements()) {
DefaultMutableTreeNode nextElement = e.nextElement();
if (matchesFilter(nextElement)) {
isMatched = true;
}
}
return isMatched;
}
});
}
}
输出:
当我输入搜索文本为&#34; color&#34;然后单击Enter。它仅显示JTree和Colors节点。
问题:
我想向孩子们展示色彩。即蓝色,紫罗兰等,
我发现的另一个问题是执行搜索操作后,鼠标向上滚动不工作,而向下滚动工作正常。它可以通过搜索&#34; e&#34;来复制。缩小框架并向下滚动鼠标。
注意:我使用的是Ubuntu 14.04 LTS。
有什么想法吗?
答案 0 :(得分:2)
当其中一个父(祖先)包含所需字符串时,您还需要一个包含节点的方法。例如,
private boolean containsMatchingParent(DefaultMutableTreeNode node)
{
DefaultMutableTreeNode current = node;
while (current != null)
{
if (matchesFilter(current))
{
return true;
}
current = (DefaultMutableTreeNode) current.getParent();
}
return false;
}
(以与containsMatchingChild
相同的方式使用)。
答案 1 :(得分:2)
将matchesFilter()
更改为:
private boolean matchesFilter(DefaultMutableTreeNode node)
{
TreeNode parent = node;
while ( parent != null )
{
if ( parent.toString().contains(textField.getText()))
{
return true;
}
parent = parent.getParent();
}
return false;
}
这将返回颜色,以便您可以展开colors
节点。