1 //2 //3 //title : TestJTreeComboBox.java4 //5 //Description : 树型下拉框6 //7 //authot : java&he8 //9 //date : 2006 -1010 //11 /12 importjava.awt.*;13 importjava.awt.event.*;14 importjavax.swing.*;15 importjavax.swing.plaf.*;16 importjavax.swing.plaf.basic.*;17 importjavax.swing.plaf.metal.*;18 importjavax.swing.tree.*;19 20 importcom.sun.java.swing.plaf.motif.*;21 importcom.sun.java.swing.plaf.windows.*;22 23 /**24 *
Title: OpenSwing
25 *Description: 树形下拉列表框
26 *Copyright: Copyright (c) 2004
27 *Company:
28 *@author zhanglei29 * && SunKing30 *@version1.031 */32 publicclassTestJTreeComboBoxextendsJComboBox33 {34 /**35 * 显示用的树36 */37 privateJTree tree;38 39 publicTestJTreeComboBox ()40 {41 this(newJTree ());42 }43 44 publicTestJTreeComboBox (JTree tree)45 {46 47 this.setTree (tree);48 }49 50 /**51 * 设置树52 *@paramtree JTree53 */54 publicvoidsetTree (JTree tree)55 {56 this.tree=tree;57 if(tree!=null)58 {59 this.setSelectedItem (tree.getSelectionPath ());60 this.setRenderer (newJTreeComboBoxRenderer ());61 }62 this.updateUI ();63 }64 65 /**66 * 取得树67 *@returnJTree68 */69 publicJTree getTree ()70 {71 returntree;72 }73 74 /**75 * 设置当前选择的树路径76 *@paramo Object77 */78 publicvoidsetSelectedItem (Object o)79 {80 tree.setSelectionPath ((TreePath)o);81 getModel ().setSelectedItem (o);82 }83 84 publicvoidupdateUI ()85 {86 ComboBoxUI cui=(ComboBoxUI)UIManager.getUI (this);87 if(cuiinstanceofMetalComboBoxUI)88 {89 cui=newMetalJTreeComboBoxUI ();90 }91 elseif(cuiinstanceofMotifComboBoxUI)92 {93 cui=newMotifJTreeComboBoxUI ();94 }95 else96 {97 cui=newWindowsJTreeComboBoxUI ();98 }99 setUI (cui);100 }101 102 //UI Inner classes -- one for each supported Look and Feel103 classMetalJTreeComboBoxUIextendsMetalComboBoxUI104 {105 protectedComboPopup createPopup ()106 {107 returnnewTreePopup (comboBox);108 }109 }110 111 classWindowsJTreeComboBoxUIextendsWindowsComboBoxUI112 {113 protectedComboPopup createPopup ()114 {115 returnnewTreePopup (comboBox);116 }117 }118 119 classMotifJTreeComboBoxUIextendsMotifComboBoxUI120 {121 protectedComboPopup createPopup ()122 {123 returnnewTreePopup (comboBox);124 }125 }126 /**127 *Title: OpenSwing
128 *Description: 树形结构而来的DefaultListCellRenderer
129 *Copyright: Copyright (c) 2004
130 *Company:
131 *@author SunKing132 *@version1.0133 */134 classJTreeComboBoxRendererextendsDefaultListCellRenderer135 {136 publicComponent getListCellRendererComponent (JList list, Object value,137 intindex,booleanisSelected,booleancellHasFocus)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 returnlb;149 }150 returnsuper.getListCellRendererComponent (list, value, index,151 isSelected, cellHasFocus);152 }153 }154 155 /**156 * 测试157 */158 publicstaticvoidmain (String args[])159 {160 JFrame frame=newJFrame ("JTreeComboBox Demo");161 frame.getContentPane ().setLayout (newFlowLayout ());162 163 TestJTreeComboBox box=newTestJTreeComboBox (newJTree ());164 //box.setEditable(true);165 166 box.setPreferredSize (newDimension (300,21));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 *Title: JTreeComboBox
183 *Description: TreePopup
184 *Copyright: Copyright (c) 2004
185 *Company:
186 *@author zhanglei187 * && SunKing188 *@version1.0189 */190 classTreePopupextendsJPopupMenuimplementsComboPopup191 {192 protectedTestJTreeComboBox comboBox;193 protectedJScrollPane scrollPane;194 195 protectedMouseMotionListener mouseMotionListener;196 protectedMouseListener mouseListener;197 privateMouseListener treeSelectListener=newMouseAdapter ()198 {199 200 publicvoidmouseReleased (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 publicTreePopup (JComboBox comboBox)216 {217 this.comboBox=(TestJTreeComboBox)comboBox;218 setBorder (BorderFactory.createLineBorder (Color.black));219 setLayout (newBorderLayout ());220 setLightWeightPopupEnabled (comboBox.isLightWeightPopupEnabled ());221 JTree tree=this.comboBox.getTree ();222 if(tree!=null)223 {224 scrollPane=newJScrollPane (tree);225 scrollPane.setBorder (null);226 add (scrollPane, BorderLayout.CENTER);227 tree.addMouseListener (treeSelectListener);228 }229 }230 231 publicvoidshow ()232 {233 updatePopup ();234 show (comboBox,0, comboBox.getHeight ());235 comboBox.getTree ().requestFocus ();236 }237 238 publicvoidhide ()239 {240 setVisible (false);241 comboBox.firePropertyChange ("popupVisible",true,false);242 }243 244 protectedJList list=newJList ();245 publicJList getList ()246 {247 returnlist;248 }249 250 publicMouseMotionListener getMouseMotionListener ()251 {252 if(mouseMotionListener==null)253 {254 mouseMotionListener=newMouseMotionAdapter ()255 {};256 }257 returnmouseMotionListener;258 }259 260 publicKeyListener getKeyListener ()261 {262 returnnull;263 }264 265 publicvoiduninstallingUI ()266 {}267 268 /**269 * Implementation of ComboPopup.getMouseListener().270 *271 *@returnaMouseListener
or null272 *@seeComboPopup#getMouseListener273 */274 publicMouseListener getMouseListener ()275 {276 if(mouseListener==null)277 {278 mouseListener=newInvocationMouseHandler ();279 }280 returnmouseListener;281 }282 283 protectedvoidtogglePopup ()284 {285 if(isVisible ())286 {287 hide ();288 }289 else290 {291 show ();292 }293 }294 295 protectedvoidupdatePopup ()296 {297 setPreferredSize (newDimension (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 protectedclassInvocationMouseHandlerextendsMouseAdapter307 {308 publicvoidmousePressed (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((!(compinstanceofJComponent))||318 ((JComponent)comp).isRequestFocusEnabled ())319 {320 comp.requestFocus ();321 }322 }323 elseif(comboBox.isRequestFocusEnabled ())324 {325 comboBox.requestFocus ();326 }327 togglePopup ();
分享到:
2007-02-07 09:37
浏览 1089
评论