[英]Edit JTable cell, as a JTree node
My goal is to use a JTable as a JTree node, and be able to edit the cells in the JTable by double clicking them. 我的目标是使用JTable作为JTree节点,并能够通过双击它们来编辑JTable中的单元格。 The rendering works well ( Jtable as a Jtree Node ), but I have no idea how to edit a single cell in the table.
呈现效果很好(将Jtable作为Jtree节点 ),但是我不知道如何在表中编辑单个单元格。 If I set the tree to be editable, I can edit the nodes using double click, but I want to edit cells by themselves, because the user might seem reluctant to maintaining the "%" in front of the numbers I use for rendering.
如果将树设置为可编辑状态,则可以通过双击来编辑节点,但是我想自己编辑单元格,因为用户似乎不愿意在用于渲染的数字前保留“%”。 If the tree is not set to be editable double click does nothing.
如果树未设置为可编辑,则双击将不执行任何操作。 Is there an easy way to accomplish this?
有没有简单的方法可以做到这一点?
public class TreeWithCellRenderer {
static class MyCellRenderer implements TreeCellRenderer {
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
boolean leaf, int row, boolean hasFocus) {
String command = (String) ((DefaultMutableTreeNode) value).getUserObject();
final String[] params = command.split("%");
JTable table = new JTable();
table.setModel(new DefaultTableModel() {
private static final long serialVersionUID = 1L;
@Override
public int getRowCount() {
return 1;
}
@Override
public int getColumnCount() {
return params.length;
}
@Override
public Object getValueAt(int row, int column) {
return params[column];
}
});
return table;
}
}
private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("JTreeTutorial");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Tree components
DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
DefaultMutableTreeNode cmd1 = new DefaultMutableTreeNode("name %1");
DefaultMutableTreeNode cmd2 = new DefaultMutableTreeNode("name %1 %2");
root.add(cmd1);
root.add(cmd2);
JTree jTree = new JTree(root);
//Don't like it too much, as it makes you edit the whole node, not cells
//jTree.setEditable(true);
jTree.setCellRenderer(new MyCellRenderer());
JScrollPane scroolPane = new JScrollPane(jTree);
frame.add(scroolPane);
// Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
createAndShowGUI();
}
} }
create own TreeCellEditor , editors and renderers concept is same, similair for JCombobBox, JList(by default non_editable JComponents), JTable and JTree 创建自己的TreeCellEditor , 编辑器和渲染器的概念是相同的,对于JCombobBox,JList(默认情况下是不可编辑的JComponents),JTable和JTree都是类似的
note TreeCellEditor is invoked at 3rd. 注意TreeCellEditor在第3次被调用。 MouseClick
鼠标点击
your linked code is based on 您的链接代码基于
You should add method isCellEditable for your data model. 您应该为数据模型添加方法isCellEditable。 Otherwise your table not editable.
否则,您的表不可编辑。
It may looks like this: 它可能看起来像这样:
@Override
public boolean isCellEditable(int row, int col)
{
return col >= 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.