1 //////////////////////////////////////////////////////////////////////////////////
            2 //
            3 // title : TestJTreeComboBox.java
            4 //
            5 // Description : 樹型下拉框
            6 //
            7 // authot : java&he
            8 //
            9 // date : 2006 -10
           10 //
           11 /////////////////////////////////////////////////////////////////////////////////
           12 import java.awt.*;
           13 import java.awt.event.*;
           14 import javax.swing.*;
           15 import javax.swing.plaf.*;
           16 import javax.swing.plaf.basic.*;
           17 import javax.swing.plaf.metal.*;
           18 import javax.swing.tree.*;
           19 
           20 import com.sun.java.swing.plaf.motif.*;
           21 import com.sun.java.swing.plaf.windows.*;
           22 
           23 /**
           24  * <p>Title: OpenSwing</p>
           25  * <p>Description: 樹形下拉列表框</p>
           26  * <p>Copyright: Copyright (c) 2004</p>
           27  * <p>Company: </p>
           28  * @author  <a href="mailto:rockis@msn.com">zhanglei</a>
           29  *  && <a href="mailto:sunkingxie@hotmail.com">SunKing</a>
           30  * @version 1.0
           31  */
           32 public class TestJTreeComboBox extends JComboBox
           33 {
           34     /**
           35      * 顯示用的樹
           36      */
           37     private JTree tree;
           38     
           39     public TestJTreeComboBox ()
           40     {
           41         this (new JTree ());
           42     }
           43     
           44     public TestJTreeComboBox (JTree tree)
           45     {
           46         
           47         this.setTree (tree);
           48     }
           49     
           50     /**
           51      * 設置樹
           52      * @param tree JTree
           53      */
           54     public void setTree (JTree tree)
           55     {
           56         this.tree = tree;
           57         if(tree != null)
           58         {
           59             this.setSelectedItem (tree.getSelectionPath ());
           60             this.setRenderer (new JTreeComboBoxRenderer ());
           61         }
           62         this.updateUI ();
           63     }
           64     
           65     /**
           66      * 取得樹
           67      * @return JTree
           68      */
           69     public JTree getTree ()
           70     {
           71         return tree;
           72     }
           73     
           74     /**
           75      * 設置當前選擇的樹路徑
           76      * @param o Object
           77      */
           78     public void setSelectedItem (Object o)
           79     {
           80         tree.setSelectionPath ((TreePath)o);
           81         getModel ().setSelectedItem (o);
           82     }
           83     
           84     public void updateUI ()
           85     {
           86         ComboBoxUI cui = (ComboBoxUI)UIManager.getUI (this);
           87         if(cui instanceof MetalComboBoxUI)
           88         {
           89             cui = new MetalJTreeComboBoxUI ();
           90         }
           91         else if(cui instanceof MotifComboBoxUI)
           92         {
           93             cui = new MotifJTreeComboBoxUI ();
           94         }
           95         else
           96         {
           97             cui = new WindowsJTreeComboBoxUI ();
           98         }
           99         setUI (cui);
          100     }
          101     
          102     // UI Inner classes -- one for each supported Look and Feel
          103     class MetalJTreeComboBoxUI extends MetalComboBoxUI
          104     {
          105         protected ComboPopup createPopup ()
          106         {
          107             return new TreePopup (comboBox);
          108         }
          109     }
          110     
          111     class WindowsJTreeComboBoxUI extends WindowsComboBoxUI
          112     {
          113         protected ComboPopup createPopup ()
          114         {
          115             return new TreePopup (comboBox);
          116         }
          117     }
          118     
          119     class MotifJTreeComboBoxUI extends MotifComboBoxUI
          120     {
          121         protected ComboPopup createPopup ()
          122         {
          123             return new TreePopup (comboBox);
          124         }
          125     }
          126     /**
          127      * <p>Title: OpenSwing</p>
          128      * <p>Description: 樹形結構而來的DefaultListCellRenderer </p>
          129      * <p>Copyright: Copyright (c) 2004</p>
          130      * <p>Company: </p>
          131      * @author <a href="mailto:sunkingxie@hotmail.com">SunKing</a>
          132      * @version 1.0
          133      */
          134     class JTreeComboBoxRenderer extends DefaultListCellRenderer
          135     {
          136         public Component getListCellRendererComponent (JList list, Object value,
          137                 int index, boolean isSelected, boolean cellHasFocus)
          138         {
          139             if(value != null)
          140             {
          141                 TreePath path = (TreePath)value;
          142                 TreeNode node = (TreeNode)path.getLastPathComponent ();
          143                 value = node;
          144                 TreeCellRenderer r = tree.getCellRenderer ();
          145                 JLabel lb = (JLabel)r.getTreeCellRendererComponent (
          146                         tree, value, isSelected, false, node.isLeaf (), index,
          147                         cellHasFocus);
          148                 return lb;
          149             }
          150             return super.getListCellRendererComponent (list, value, index,
          151                     isSelected, cellHasFocus);
          152         }
          153     }
          154     
          155     /**
          156      * 測試
          157      */
          158     public static void main (String args[])
          159     {
          160         JFrame frame = new JFrame ("JTreeComboBox Demo");
          161         frame.getContentPane ().setLayout (new FlowLayout ());
          162         
          163         TestJTreeComboBox box = new TestJTreeComboBox (new JTree ());
          164         // box.setEditable(true);
          165         
          166         box.setPreferredSize (new Dimension (30021));
          167         frame.getContentPane ().add (box);
          168         //  final JButton btt = new JButton("Set As JFileTree");
          169         // btt.addActionListener(new ActionListener(){
          170         //  public void actionPerformed(ActionEvent e){
          171         //      box.setTree(new JFileTree());
          172         //     btt.setEnabled(false);
          173         //   }
          174         //   });
          175         //   frame.getContentPane().add(btt);
          176         frame.setVisible (true);
          177         frame.setDefaultCloseOperation (3);
          178     }
          179 }
          180 
          181 /**
          182  * <p>Title: JTreeComboBox</p>
          183  * <p>Description: TreePopup</p>
          184  * <p>Copyright: Copyright (c) 2004</p>
          185  * <p>Company: </p>
          186  * @author  <a href="mailto:rockis@msn.com">zhanglei</a>
          187  *  && <a href="mailto:sunkingxie@hotmail.com">SunKing</a>
          188  * @version 1.0
          189  */
          190 class TreePopup extends JPopupMenu implements ComboPopup
          191 {
          192     protected TestJTreeComboBox comboBox;
          193     protected JScrollPane scrollPane;
          194     
          195     protected MouseMotionListener mouseMotionListener;
          196     protected MouseListener mouseListener;
          197     private MouseListener treeSelectListener = new MouseAdapter ()
          198     {
          199         
          200         public void mouseReleased (MouseEvent e)
          201         {
          202             JTree tree = (JTree)e.getSource ();
          203             TreePath tp = tree.getPathForLocation (e.getPoint ().x,
          204                     e.getPoint ().y);
          205             if(tp == null)
          206             {
          207                 return;
          208             }
          209             comboBox.setSelectedItem (tp);
          210             togglePopup ();
          211             MenuSelectionManager.defaultManager ().clearSelectedPath ();
          212         }
          213     };
          214     
          215     public TreePopup (JComboBox comboBox)
          216     {
          217         this.comboBox = (TestJTreeComboBox)comboBox;
          218         setBorder (BorderFactory.createLineBorder (Color.black));
          219         setLayout (new BorderLayout ());
          220         setLightWeightPopupEnabled (comboBox.isLightWeightPopupEnabled ());
          221         JTree tree = this.comboBox.getTree ();
          222         if(tree != null)
          223         {
          224             scrollPane = new JScrollPane (tree);
          225             scrollPane.setBorder (null);
          226             add (scrollPane, BorderLayout.CENTER);
          227             tree.addMouseListener (treeSelectListener);
          228         }
          229     }
          230     
          231     public void show ()
          232     {
          233         updatePopup ();
          234         show (comboBox, 0, comboBox.getHeight ());
          235         comboBox.getTree ().requestFocus ();
          236     }
          237     
          238     public void hide ()
          239     {
          240         setVisible (false);
          241         comboBox.firePropertyChange ("popupVisible"truefalse);
          242     }
          243     
          244     protected JList list = new JList ();
          245     public JList getList ()
          246     {
          247         return list;
          248     }
          249     
          250     public MouseMotionListener getMouseMotionListener ()
          251     {
          252         if(mouseMotionListener == null)
          253         {
          254             mouseMotionListener = new MouseMotionAdapter ()
          255             {};
          256         }
          257         return mouseMotionListener;
          258     }
          259     
          260     public KeyListener getKeyListener ()
          261     {
          262         return null;
          263     }
          264     
          265     public void uninstallingUI ()
          266     {}
          267     
          268     /**
          269      * Implementation of ComboPopup.getMouseListener().
          270      *
          271      * @return a <code>MouseListener</code> or null
          272      * @see ComboPopup#getMouseListener
          273      */
          274     public MouseListener getMouseListener ()
          275     {
          276         if(mouseListener == null)
          277         {
          278             mouseListener = new InvocationMouseHandler ();
          279         }
          280         return mouseListener;
          281     }
          282     
          283     protected void togglePopup ()
          284     {
          285         if(isVisible ())
          286         {
          287             hide ();
          288         }
          289         else
          290         {
          291             show ();
          292         }
          293     }
          294     
          295     protected void updatePopup ()
          296     {
          297         setPreferredSize (new Dimension (comboBox.getSize ().width, 200));
          298         Object selectedObj = comboBox.getSelectedItem ();
          299         if(selectedObj != null)
          300         {
          301             TreePath tp = (TreePath)selectedObj;
          302             ((TestJTreeComboBox)comboBox).getTree ().setSelectionPath (tp);
          303         }
          304     }
          305     
          306     protected class InvocationMouseHandler extends MouseAdapter
          307     {
          308         public void mousePressed (MouseEvent e)
          309         {
          310             if(!SwingUtilities.isLeftMouseButton (e) || !comboBox.isEnabled ())
          311             {
          312                 return;
          313             }
          314             if(comboBox.isEditable ())
          315             {
          316                 Component comp = comboBox.getEditor ().getEditorComponent ();
          317                 if((!(comp instanceof JComponent)) ||
          318                         ((JComponent)comp).isRequestFocusEnabled ())
          319                 {
          320                     comp.requestFocus ();
          321                 }
          322             }
          323             else if(comboBox.isRequestFocusEnabled ())
          324             {
          325                 comboBox.requestFocus ();
          326             }
          327             togglePopup ();
          328         }
          329     }
          330 }
          331 

           
          posted on 2007-02-07 09:37 -274°C 閱讀(2768) 評論(2)  編輯  收藏 所屬分類: JAVA


          FeedBack:
          # re: 樹型下拉框
          2007-08-20 19:21 | hlh
          好文章 非常感謝! 可以認真的學習學習  回復  更多評論
            
          # re: 樹型下拉框
          2007-08-21 19:26 | JAVA-HE
          老文了,謝謝夸獎!  回復  更多評論
            

          常用鏈接

          留言簿(21)

          隨筆分類(265)

          隨筆檔案(242)

          相冊

          JAVA網站

          關注的Blog

          搜索

          •  

          積分與排名

          • 積分 - 914107
          • 排名 - 40

          最新評論

          主站蜘蛛池模板: 得荣县| 新泰市| 白水县| 绥中县| 临洮县| 博客| 铜梁县| 泰来县| 光泽县| 横山县| 万安县| 沂水县| 洞口县| 璧山县| 古交市| 宁晋县| 曲周县| 安吉县| 嫩江县| 临沂市| 肥东县| 库伦旗| 康马县| 孝感市| 宁武县| 遂宁市| 明水县| 万州区| 大洼县| 乌苏市| 丹寨县| 娄烦县| 龙里县| 大名县| 上虞市| 西平县| 泽库县| 鄂托克旗| 北安市| 五莲县| 汶上县|