[java]代码库
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package yan.t4;
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellEditor;
import javax.swing.tree.DefaultTreeCellRenderer;
public class ComboTreeItem extends JFrame {
private static final long serialVersionUID = 1L;
private JTree tree;
public ComboTreeItem() {
super();
initUI();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(800, 600);
setVisible(true);
}
private void initUI() {
Container c = getContentPane();
c.setLayout(new BorderLayout());
tree = new JTree();
c.add(tree);
tree.setEditable(true);
final JComboBox combobox = new JComboBox();
combobox.setEditable(true);
combobox.addItem("选择表达式");
tree.setCellEditor(new DefaultTreeCellEditor(tree,
(DefaultTreeCellRenderer) tree.getCellRenderer(),
new DefaultCellEditor(combobox) {
private static final long serialVersionUID = 1L;
@Override
public boolean stopCellEditing() {
// 假设不可含有逗号
String forbid = ",";
String value = getCellEditorValue().toString();
boolean rs = value.indexOf(forbid) >= 0;
if (rs) {
JOptionPane.showMessageDialog(ComboTreeItem.this, "不允许包含逗号!");
combobox.getEditor().selectAll();
} else {
super.stopCellEditing();
}
return !rs;
}
@Override
public Object getCellEditorValue() {
return combobox.getEditor().getItem();
}
}));
}
public static void main(String args[]) {
new ComboTreeItem();
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package yan.t4;
import java.awt.*;
import java.awt.event.*;
import java.util.EventObject;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
public class TreeEditDemo extends JPanel {
private JTree tree;
private DefaultMutableTreeNode root;
private DefaultTreeCellEditor editor;
private JLabel label = new JLabel(" ", JLabel.CENTER);
public TreeEditDemo() {
super(new BorderLayout());
root = new DefaultMutableTreeNode("Nodes");
root.add(new DefaultMutableTreeNode(new Resource("one")));
root.add(new DefaultMutableTreeNode(new Resource("two")));
root.add(new DefaultMutableTreeNode(new Resource("three")));
final DefaultTreeModel treeModel = new DefaultTreeModel(root);
tree = new JTree(treeModel);
tree.setEditable(true);
editor = new MyTreeCellEditor(tree,(DefaultTreeCellRenderer) tree.getCellRenderer());
tree.setCellEditor(editor);
tree.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "startEditing");
this.add(new JScrollPane(tree));
this.add(label, BorderLayout.SOUTH);
tree.addTreeSelectionListener(new TreeSelectionListener() {
@Override
public void valueChanged(TreeSelectionEvent e) {
TreePath path = e.getNewLeadSelectionPath();
if (path != null) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
if (node.isLeaf()) {
Resource user = (Resource) node.getUserObject();
label.setText(user.toString());
} else {
label.setText(" ");
}
}
}
});
editor.addCellEditorListener(new CellEditorListener() {
@Override
public void editingStopped(ChangeEvent e) {
label.setText(editor.getCellEditorValue().toString());
}
@Override
public void editingCanceled(ChangeEvent e) { }
});
}
private static class MyTreeCellEditor extends DefaultTreeCellEditor {
public MyTreeCellEditor(JTree tree, DefaultTreeCellRenderer renderer) {
super(tree, renderer);
}
@Override
public Object getCellEditorValue() {
String value = (String) super.getCellEditorValue();
return new Resource(value);
}
@Override
public boolean isCellEditable(EventObject e) {
return super.isCellEditable(e) && ((TreeNode) lastPath.getLastPathComponent()).isLeaf();
}
}
private static class Resource {
String name;
public Resource(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return getName();
}
}
private void display() {
JFrame f = new JFrame("TreeEditorDemo");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new TreeEditDemo().display();
}
});
}
}
[源代码打包下载]
初级程序员
by: 墨冷 发表于:2018-01-03 21:26:56 顶(0) | 踩(0) 回复
这是文件浏览器的基础么?
回复评论