我创建了一个树单元渲染器/编辑器框架,虽然它确实有点 hacky,但它在 Windows 和 Linux 上完美运行。下图说明了一个示例设置。
目标是,如果用户单击图像(数字)1 或 2,则应用程序响应该单击但不选择树行。如果用户单击文本一或二,应用程序会响应该单击并选择树行。我实现这一点的方式再次有点骇人听闻。基本上,当用户单击树行时,会显示编辑器组件(看起来与渲染器组件相同),并且编辑器组件有一个鼠标侦听器,可以确定用户在行中单击的位置。
这在 Windows/Linux 上工作的事实依赖于我一直认为依赖的东西似乎很脆弱。基本上,如果您单次单击该行,则单击 (a) 会调出编辑器并 (b) 激活编辑器组件上的鼠标侦听器。这就是我想要的方式!但是,当您尝试在 Mac OSX(10.6.2 如果重要)上运行该应用程序时,上述脆弱的假设不再适用。任何时候你想与树交互,你必须点击两次(一次激活编辑器,再次激活鼠标监听器)。
下面的 SSCCE 可以重现该行为。当然,如果您没有 OSX,则无法重现不良行为,但也许您仍然可以推荐一种更聪明的方法来实现我的目标。sysout
当您单击树的各个部分时,观察控制台中指示正在发生的事情的消息。
哦,SSCCE 引用了这两个图像:
package TreeTest;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;
import java.util.EventObject;
import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.event.CellEditorListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeCellEditor;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
@SuppressWarnings("serial")
public class TreeTest extends JComponent {
private JFrame frame;
private DefaultTreeModel treeModel;
private DefaultMutableTreeNode root;
private JTree tree;
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch (Throwable e) {
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TreeTest window = new TreeTest();
window.frame.setVisible(true);
window.frame.requestFocusInWindow();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public TreeTest() {
initialize();
}
private void initialize() {
frame = new JFrame("Tree Test");
frame.setBounds(400, 400, 250, 150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
root = new DefaultMutableTreeNode("root");
treeModel = new DefaultTreeModel(root);
tree = new JTree(treeModel);
tree.setEditable(true);
tree.getSelectionModel().setSelectionMode(
TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.setRootVisible(false);
tree.setShowsRootHandles(false);
tree.setCellRenderer(new TreeRenderer());
tree.setCellEditor(new TreeEditor());
tree.putClientProperty("JTree.lineStyle", "None");
tree.setBackground(Color.white);
treeModel.insertNodeInto(new DefaultMutableTreeNode("two"), root, 0);
treeModel.insertNodeInto(new DefaultMutableTreeNode("one"), root, 0);
TreeNode[] nodes = treeModel.getPathToRoot(root);
tree.expandPath(new TreePath(nodes));
tree.addTreeSelectionListener(new TreeSelectionListener() {
@Override
public void valueChanged(TreeSelectionEvent e) {
System.out.println("SELECTION CHANGED!");
}
});
frame.getContentPane().add(tree);
}
public class TreeComponent extends JPanel {
public TreeComponent(JLabel numIcon, JLabel numText) {
this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
this.add(numIcon);
this.add(numText);
}
}
public class TreeRenderer implements TreeCellRenderer {
private ImageIcon oneIcon;
private ImageIcon twoIcon;
public TreeRenderer() {
try {
oneIcon = new ImageIcon(ImageIO.read(
new URL("http://i.imgur.com/HtHJkfI.png")));
twoIcon = new ImageIcon(ImageIO.read(
new URL("http://i.imgur.com/w5jAp5c.png")));
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf, int row,
boolean hasFocus) {
JLabel numIcon = new JLabel();
numIcon.setAlignmentX(JLabel.CENTER_ALIGNMENT);
numIcon.setAlignmentY(JLabel.CENTER_ALIGNMENT);
numIcon.setBorder(new EmptyBorder(0, 0, 0, 4));
JLabel numText = new JLabel();
TreeComponent comp = new TreeComponent(numIcon, numText);
String str = (String) ((DefaultMutableTreeNode) value).getUserObject();
if (str.equals("one")) {
numIcon.setIcon(oneIcon);
numText.setText("one");
} else if (str.equals("two")) {
numIcon.setIcon(twoIcon);
numText.setText("two");
}
numText.setOpaque(true);
if (selected) {
numText.setBackground(new Color(209, 230, 255));
numText.setBorder(new LineBorder(
new Color(132, 172, 221), 1, false));
} else {
numText.setBackground(Color.white);
numText.setBorder(new LineBorder(Color.white, 1, false));
}
comp.setFocusable(false);
comp.setBackground(Color.white);
return comp;
}
}
public class TreeEditor implements TreeCellEditor {
private TreeRenderer rend;
private TreeComponent editorComponent;
private JTree tree;
private DefaultTreeModel treeModel;
private DefaultMutableTreeNode node;
private String str;
public TreeEditor() {
rend = new TreeRenderer();
}
@Override
public Component getTreeCellEditorComponent(
final JTree tree, final Object value, boolean isSelected,
boolean expanded, boolean leaf, int row) {
this.tree = tree;
treeModel = (DefaultTreeModel) tree.getModel();
node = (DefaultMutableTreeNode) value;
Object userObject = node.getUserObject();
this.str = (String) userObject;
TreeNode[] nodes = treeModel.getPathToRoot(node);
final TreePath path = new TreePath(nodes);
editorComponent = (TreeComponent) rend.getTreeCellRendererComponent(
tree, value, isSelected, expanded, leaf, row, false);
editorComponent.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
TreeEditor.this.stopCellEditing();
int x = e.getX();
if (x >= 0 && x <= 16) {
System.out.println(
"you clicked the image for row " + str);
} else if (x > 16) {
System.out.println(
"you clicked the text for row " + str);
tree.setSelectionPath(path);
}
}
});
return editorComponent;
}
@Override
public boolean isCellEditable(EventObject anEvent) {
return true;
}
@Override
public boolean shouldSelectCell(EventObject anEvent) {
return false;
}
@Override
public boolean stopCellEditing() {
tree.cancelEditing();
return false;
}
@Override
public Object getCellEditorValue() {
return null;
}
@Override
public void cancelCellEditing() {
}
@Override
public void addCellEditorListener(CellEditorListener l) {
}
@Override
public void removeCellEditorListener(CellEditorListener l) {
}
}
}