⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cascadecombobox.java

📁 树型选择Java组合框
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        }
        private String getText(TreeNode o)
        {
            StringBuffer sb = new StringBuffer(o.toString());
            if (CascadeComboBox.this.showPathOnSelected)
            {
                Object root = treeModel.getRoot();
                if (!root.equals(o))
                {
                    TreeNode node = o.getParent();
                    while (node != null){
                        sb.insert(0, node.toString() + SEPARATOR);
                        if (node.equals(root)) break;
                        node = node.getParent();
                        
                    }
                }
            }
            return sb.toString();
        }
    }

    /**
     * 测试CascadeComboBox
     */
    public static void main(String args[]) throws Exception{
      //  UIStyleManager.loadUIStyleName();
        JFrame f = new JFrame("CascadeComboBox Demo");
        Container c = f.getContentPane();
        c.setLayout(new FlowLayout());
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
        DefaultMutableTreeNode folder = new DefaultMutableTreeNode("folder1");
        DefaultMutableTreeNode leaf1 = new DefaultMutableTreeNode("Leaf1");
        DefaultMutableTreeNode leaf2 = new DefaultMutableTreeNode("Leaf2");
        root.add(leaf1);
        root.add(folder);
        folder.add(leaf2);
        JComboBox cb = new JComboBox(new Object[]{"abc"});
        c.add(cb);
        DefaultTreeModel t = new DefaultTreeModel(root);
        CascadeComboBox testBox = new CascadeComboBox(t);
        testBox.setTreeModel(t);
        testBox.showPathOnSelected(true);
        testBox.setPreferredSize(new Dimension(200, 21));
        testBox.setSelectedItem(leaf2);
        c.add(testBox);
        JButton btn = new JButton(new AbstractAction("Enabled"){
           public void actionPerformed(ActionEvent e)
           {
               JButton btn = (JButton)e.getSource();
               Container c = (Container)btn.getParent();
               c.getComponent(0).setEnabled(!c.getComponent(0).isEnabled());
           }
        });
        c.add(btn);
        f.setSize(300, 150);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        f.setLocation((screenSize.width - f.getWidth()) / 2,
                      (screenSize.height - f.getHeight()) / 2);
        f.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });
        f.show();
    }
}
/**
 * <p>Title: CascadeComboBox</p>
 * <p>Description: TreePopup 选择框弹出的树选择面板</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author <a href="mailto:rockis@msn.com"'>Stone</a>
 * @version 1.0
 */
class TreePopup extends JPopupMenu implements ComboPopup,
    PopupMenuListener, PropertyChangeListener{
    protected JComboBox comboBox;
//    protected JPopupMenu popup;
    JTree tree = new JTree();
    JScrollPane scrollPane = new JScrollPane(tree);

    protected Color selectedBackground;
    protected Color selectedForeground;
    protected Color background;
    protected Color foreground;

    protected MouseMotionListener      mouseMotionListener;
    protected MouseListener            mouseListener;
    
    protected PropertyChangeListener propertyChangeListener;

    public TreePopup(JComboBox comboBox){
        this.comboBox = comboBox;
        background = UIManager.getColor("ComboBox.background");
        foreground = UIManager.getColor("ComboBox.foreground");
        selectedBackground = UIManager.getColor(
            "ComboBox.selectionBackground");
        selectedForeground = UIManager.getColor(
            "ComboBox.selectionForeground");
        initializePopup();
    }

    public void show(){
        //选中树节点
        updatePopup();
        show(comboBox, 0, comboBox.getHeight());
    }

    public void hide(){
        setVisible(false);
        comboBox.firePropertyChange("popupVisible", true, false);
    }

    protected JList list = new JList();
    public JList getList(){
        return list;
    }

    public MouseMotionListener getMouseMotionListener(){
        return createMouseMotionListener();
    }

    public KeyListener getKeyListener(){
        return createKeyListener();
    }

    public JTree getTree(){
        return tree;
    }
    
    public void uninstallingUI(){
        removePopupMenuListener(this);
    }

    /**
     * Creates the key listener that will be added to the combo box. If 
     * this method returns null then it will not be added to the combo box.
     *
     * @return a <code>KeyListener</code> or null
     */
    protected KeyListener createKeyListener() {
        return null;
    }
    
    /**
     * Implementation of ComboPopup.getMouseListener().
     * 
     * @return a <code>MouseListener</code> or null
     * @see ComboPopup#getMouseListener
     */
    public MouseListener getMouseListener() {
        if (mouseListener == null) {
            mouseListener = createMouseListener();
        }
        return mouseListener;
    }

    /**
     * Creates the mouse motion listener which will be added to the combo
     * box.
     *
     * <strong>Warning:</strong>
     * When overriding this method, make sure to maintain the existing
     * behavior.
     *
     * @return a <code>MouseMotionListener</code> which will be added to
     *         the combo box or null
     */
    protected MouseMotionListener createMouseMotionListener() {
        return new MouseMotionAdapter(){};
    }

    protected MouseListener createMouseListener() {
        return new InvocationMouseHandler();
    }


    public void propertyChange(PropertyChangeEvent e) {
        String propertyName = e.getPropertyName();
        CascadeComboBox comboBox = (CascadeComboBox)e.getSource();
        if (propertyName.equals("treeModel"))
        {
            tree.setModel(comboBox.getTreeModel());
        }
        if (propertyName.equals("cellRenderer"))
        {
            tree.setCellRenderer(comboBox.getCellRenderer());
        }
        if (propertyName.equals("showPathOnSelected"))
        {
        }
    }

    public void popupMenuCanceled(PopupMenuEvent e){}

    public void popupMenuWillBecomeInvisible(PopupMenuEvent e){
    }

    public void popupMenuWillBecomeVisible(PopupMenuEvent e){}

    protected void togglePopup(){
        if(isVisible()){
            hide();
        } else{
            show();
        }
    }

    /**
     * 初始化弹出面板
     */
    protected void initializePopup(){
        scrollPane.setBorder(null);
        setForeground(foreground);
        setOpaque(true);
        setBorder(BorderFactory.createLineBorder(Color.black));
        setLayout(new BorderLayout());
        setLightWeightPopupEnabled( comboBox.isLightWeightPopupEnabled() );
        setBackground(background);
//        setFocusable( false );
//        addPopupMenuListener(this);
        BasicMenuItemUI ui;
        add(scrollPane, BorderLayout.CENTER);
        tree.addMouseListener(new MouseAdapter(){
            public void mouseReleased(MouseEvent e)
            {
                TreePath tp = tree.getPathForLocation(e.getPoint().x, e.getPoint().y);
                if (tp == null) 
                {
                    return;
                };
                Object newValue = tp.getLastPathComponent();
                comboBox.setSelectedItem(newValue);
                togglePopup();
                MenuSelectionManager.defaultManager().clearSelectedPath();
            }
        });
    }
    
    /**
     * 更新所选节点
     */
    protected void updatePopup(){
        setPreferredSize(new Dimension(comboBox.getSize().width, 200));
        Object selectedObj = comboBox.getSelectedItem();
        if (selectedObj != null)
        {
            TreePath tp = new TreePath((TreeNode)selectedObj);
            tree.setSelectionPath(tp);
        }
    }
    
    protected class InvocationMouseHandler extends MouseAdapter {
        /**
         * Responds to mouse-pressed events on the combo box.
         *
         * @param e the mouse-press event to be handled
         */
        public void mousePressed( MouseEvent e ) {
            if (!SwingUtilities.isLeftMouseButton(e) || !comboBox.isEnabled())
                return;
            
            if ( comboBox.isEditable() ) {
                Component comp = comboBox.getEditor().getEditorComponent();
                if ((!(comp instanceof JComponent)) || ((JComponent)comp).isRequestFocusEnabled()) {
                    comp.requestFocus();
                }
            }
            else if (comboBox.isRequestFocusEnabled()) {
                comboBox.requestFocus();
            }
            togglePopup();
        }
    }
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -