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 阅读(2751) 评论(2)  编辑  收藏 所属分类: JAVA


FeedBack:
# re: 树型下拉框
2007-08-20 19:21 | hlh
好文章 非常感谢! 可以认真的学习学习  回复  更多评论
  
# re: 树型下拉框
2007-08-21 19:26 | JAVA-HE
老文了,谢谢夸奖!  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 

常用链接

留言簿(21)

随笔分类(265)

随笔档案(242)

相册

JAVA网站

关注的Blog

搜索

  •  

积分与排名

  • 积分 - 909070
  • 排名 - 40

最新评论