<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

     
      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 閱讀(2769) 評論(2)  編輯  收藏 所屬分類: JAVA


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

    常用鏈接

    留言簿(21)

    隨筆分類(265)

    隨筆檔案(242)

    相冊

    JAVA網站

    關注的Blog

    搜索

    •  

    積分與排名

    • 積分 - 914507
    • 排名 - 40

    最新評論

    主站蜘蛛池模板: 国产色爽免费视频| 中文字幕视频免费在线观看 | 久久精品亚洲AV久久久无码| 久久性生大片免费观看性| 日本不卡免费新一二三区| xxx毛茸茸的亚洲| 91香蕉国产线在线观看免费| 久久亚洲AV永久无码精品| 免费看黄网站在线看| 日韩免费一级毛片| 亚洲精品无码专区| AV免费网址在线观看| 亚洲国产片在线观看| 99精品国产成人a∨免费看| 亚洲av无码一区二区三区乱子伦| 福利免费在线观看| 亚洲综合无码AV一区二区| 本道天堂成在人线av无码免费| 四虎影视永久免费观看网址| 亚洲av无码专区在线电影天堂| 在线看片无码永久免费aⅴ| 亚洲成a∧人片在线观看无码| 免费看美女被靠到爽的视频| 亚洲日本一线产区和二线 | 污污网站免费观看| 亚洲制服中文字幕第一区| 99视频在线精品免费| 亚洲国产韩国一区二区| 国产成人午夜精品免费视频| 亚洲熟妇成人精品一区| 国产一级淫片视频免费看| 国产精品亚洲一区二区三区| 亚洲第一永久AV网站久久精品男人的天堂AV| 亚洲AV无码成人精品区狼人影院| 国产无遮挡吃胸膜奶免费看| 免费看内射乌克兰女| 亚洲AV永久无码精品一百度影院| 日韩精品无码专区免费播放| 亚洲人和日本人jizz| 国产猛烈高潮尖叫视频免费| 国产免费福利体检区久久|