JComboBox 下拉树问题
在JDK1.5下,点击下拉框可以正常的弹出树,但是当把JDK换成1.6的时候,就弹不出树形结构了
下面是我的源码:
public class MyComboBox extends JComboBox {
private boolean _eventEnabled = true;
private JPopupMenu _popup;
private Component _popupComp;
private Dimension _popupSize;
private ComponentHandler _componentHandler;
private MouseHandler _mouseHandler;
private JList _popupList;
private FocusHander _focusHandler;
public MyComboBox() {
setEditable(true);
setAutoscrolls(true);
Component re = getRenderer().getListCellRendererComponent(getPopupList(),
null, -1, false, false);
_componentHandler = new ComponentHandler();
re.addComponentListener(_componentHandler);
_mouseHandler = new MouseHandler();
_focusHandler = new FocusHander();
getEditor().getEditorComponent().addFocusListener(_focusHandler);
}
public void setPopupComponent(Component comp) {
if (!comp.equals(_popupComp)) {
if (_popupComp != null) {
_popupComp.removeMouseListener(_mouseHandler);
}
_popupComp = comp;
if (_popupComp != null) {
_popupComp.addMouseListener(_mouseHandler);
}
}
convertPopupDataToComboBox();
}
public void convertPopupDataToComboBox() {
AbstractListModel amodel = (AbstractListModel) getModel();
ListDataListener[] lListener = amodel.getListDataListeners();
if (lListener != null) {
for (int i = 0, length = lListener.length; i < length; i++) {
amodel.removeListDataListener(lListener[i]);
}
}
ActionListener[] aListener = getActionListeners();
if (aListener != null) {
for (int i = 0, length = aListener.length; i < length; i++) {
removeActionListener(aListener[i]);
}
}
ItemListener[] iListener = getItemListeners();
if (iListener != null) {
for (int i = 0, length = iListener.length; i < length; i++) {
removeItemListener(iListener[i]);
}
}
PopupMenuListener[] pListener = getPopupMenuListeners();
if (pListener != null) {
for (int i = 0, length = pListener.length; i < length; i++) {
removePopupMenuListener(pListener[i]);
}
}
removeAllItems();
if (_popupComp instanceof JTree) {
JTree tree = (JTree) _popupComp;
//将树里面的数据添加到
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
DefaultMutableTreeNode node = (DefaultMutableTreeNode) model.getRoot();
if (node != null) {
addItem(node.getUserObject());
while ((node = (DefaultMutableTreeNode) node.getNextNode()) != null) {
addItem(node.getUserObject());
}
}
}
setSelectedItem(null);
if (lListener != null) {
for (int i = lListener.length - 1; i >= 0; i--) {
amodel.addListDataListener(lListener[i]);
}
}
if (aListener != null) {
for (int i = aListener.length - 1; i >= 0; i--) {
addActionListener(aListener[i]);
}
}
if (iListener != null) {
for (int i = iListener.length - 1; i >= 0; i--) {
addItemListener(iListener[i]);
}
}
if (pListener != null) {
for (int i = pListener.length - 1; i >= 0; i--) {
addPopupMenuListener(pListener[i]);
}
}
}
public JList getPopupList() {
if (_popupList == null) {
Accessible a = getUI().getAccessibleChild(MyComboBox.this, 0);
if (a instanceof javax.swing.plaf.basic.ComboPopup) {
_popupList = ((javax.swing.plaf.basic.ComboPopup) a).getList();
}
}
return _popupList;
}
private class ComponentHandler extends ComponentAdapter {
public void componentMoved(ComponentEvent e) {
initPopup(e);
}
public void componentResized(ComponentEvent e) {
initPopup(e);
}
public void componentShown(ComponentEvent e) {
initPopup(e);
}
private void initPopup(ComponentEvent e) {
if (_popup != null && _popupComp != null
&& _popup.isAncestorOf(_popupComp)) {
return;
}
Component comp = e.getComponent();
while ((comp = comp.getParent()) != null) {
if (comp instanceof JPopupMenu) {
break;
}
}
if (!(comp instanceof JPopupMenu)) {
return;
}
_popup = (JPopupMenu) comp;
if (_popupComp != null) {
_popup.removeAll();
JScrollPane spn = new JScrollPane(_popupComp) {
public Insets getInsets() {
return new Insets(5, 5, 5, 5);
}
};
if (_popupSize != null) {
spn.setPreferredSize(_popupSize);
}
else {
Dimension size = _popupComp.getPreferredSize();
if (size.getHeight() < 180) {
Dimension screenSize = Toolkit.getDefaultToolkit().
getScreenSize();
Point point = _popup.getLocationOnScreen();
if (point.y > screenSize.getHeight() / 2) {
spn.setPreferredSize(new Dimension(getWidth(),
screenSize.height - point.y - 50));
}
else {
spn.setPreferredSize(new Dimension(getWidth(),
screenSize.height / 2));
}
}
else {
spn.setPreferredSize(new Dimension(getWidth(),
size.height));
}
}
_popup.add(spn);
_popup.revalidate();
_popup.repaint();
}
}
}
private class MouseHandler extends MouseAdapter {
public void mousePressed(MouseEvent e) {
if (e.getClickCount() > 1) {
if (_popupComp instanceof JTree
&& ((JTree) _popupComp).getPathForLocation(e.getX(),
e.getY()) != null) {
TreePath path = ((JTree) _popupComp).getSelectionPath();
if (path == null) {
return;
}
TreeNode node = (TreeNode) path. getLastPathComponent();
if (node == null) {
return;
}
if (!node.isLeaf() &&
((JTree) _popupComp).isCollapsed(path)) {
return;
}
Object object = ((JTree)_popupComp).getSelectionPath().getLastPathComponent();
if (object != null) {
setSelectedItem(object.toString());
}
_popup.setVisible(false);
requestFocus();
}
}
}
}
private Component getEditorComponent() {
return this.getEditor().getEditorComponent();
}
private JComboBox getComboBox() {
return this;
}
private class FocusHander implements FocusListener {
public void focusGained(FocusEvent e) {
if (e.getSource() == getEditorComponent()) {
return;
}
getComboBox().repaint();
if (getComboBox().isEditable() && editor != null) {
getEditorComponent().requestFocus();
}
}
public void focusLost(FocusEvent e) {
if (e.getSource() ==
getComboBox().getEditor().getEditorComponent()) {
ComboBoxEditor editor = getComboBox().getEditor();
Object item = editor.getItem();
if (!e.isTemporary() && item != null &&
!item.equals(getComboBox().getSelectedItem())) {
getComboBox().actionPerformed(new ActionEvent(editor, 0, "",
EventQueue.getMostRecentEventTime(), 0));
}
}
if(e.getOppositeComponent() == _popupComp && null != _popup) {
_popup.setVisible(true);
}
getComboBox().repaint();
}
}
}
调用类:
public class MyComboBox extends JComboBox {
private boolean _eventEnabled = true;
private JPopupMenu _popup;
private Component _popupComp;
private Dimension _popupSize;
private ComponentHandler _componentHandler;
private MouseHandler _mouseHandler;
private JList _popupList;
private FocusHander _focusHandler;
public MyComboBox() {
setEditable(true);
setAutoscrolls(true);
Component re = getRenderer().getListCellRendererComponent(getPopupList(),
null, -1, false, false);
_componentHandler = new ComponentHandler();
re.addComponentListener(_componentHandler);
_mouseHandler = new MouseHandler();
_focusHandler = new FocusHander();
getEditor().getEditorComponent().addFocusListener(_focusHandler);
}
public void setPopupComponent(Component comp) {
if (!comp.equals(_popupComp)) {
if (_popupComp != null) {
_popupComp.removeMouseListener(_mouseHandler);
}
_popupComp = comp;
if (_popupComp != null) {
_popupComp.addMouseListener(_mouseHandler);
}
}
convertPopupDataToComboBox();
}
public void convertPopupDataToComboBox() {
AbstractListModel amodel = (AbstractListModel) getModel();
ListDataListener[] lListener = amodel.getListDataListeners();
if (lListener != null) {
for (int i = 0, length = lListener.length; i < length; i++) {
amodel.removeListDataListener(lListener[i]);
}
}
ActionListener[] aListener = getActionListeners();
if (aListener != null) {
for (int i = 0, length = aListener.length; i < length; i++) {
removeActionListener(aListener[i]);
}
}
ItemListener[] iListener = getItemListeners();
if (iListener != null) {
for (int i = 0, length = iListener.length; i < length; i++) {
removeItemListener(iListener[i]);
}
}
PopupMenuListener[] pListener = getPopupMenuListeners();
if (pListener != null) {
for (int i = 0, length = pListener.length; i < length; i++) {
removePopupMenuListener(pListener[i]);
}
}
removeAllItems();
if (_popupComp instanceof JTree) {
JTree tree = (JTree) _popupComp;
//将树里面的数据添加到
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
DefaultMutableTreeNode node = (DefaultMutableTreeNode) model.getRoot();
if (node != null) {
addItem(node.getUserObject());
while ((node = (DefaultMutableTreeNode) node.getNextNode()) != null) {
addItem(node.getUserObject());
}
}
}
setSelectedItem(null);
if (lListener != null) {
for (int i = lListener.length - 1; i >= 0; i--) {
amodel.addListDataListener(lListener[i]);
}
}
if (aListener != null) {
for (int i = aListener.length - 1; i >= 0; i--) {
addActionListener(aListener[i]);
}
}
if (iListener != null) {
for (int i = iListener.length - 1; i >= 0; i--) {
addItemListener(iListener[i]);
}
}
if (pListener != null) {
for (int i = pListener.length - 1; i >= 0; i--) {
addPopupMenuListener(pListener[i]);
}
}
}
public JList getPopupList() {
if (_popupList == null) {
Accessible a = getUI().getAccessibleChild(MyComboBox.this, 0);
if (a instanceof javax.swing.plaf.basic.ComboPopup) {
_popupList = ((javax.swing.plaf.basic.ComboPopup) a).getList();
}
}
return _popupList;
}
private class ComponentHandler extends ComponentAdapter {
public void componentMoved(ComponentEvent e) {
initPopup(e);
}
public void componentResized(ComponentEvent e) {
initPopup(e);
}
public void componentShown(ComponentEvent e) {
initPopup(e);
}
private void initPopup(ComponentEvent e) {
if (_popup != null && _popupComp != null
&& _popup.isAncestorOf(_popupComp)) {
return;
}
Component comp = e.getComponent();
while ((comp = comp.getParent()) != null) {
if (comp instanceof JPopupMenu) {
break;
}
}
if (!(comp instanceof JPopupMenu)) {
return;
}
_popup = (JPopupMenu) comp;
if (_popupComp != null) {
_popup.removeAll();
JScrollPane spn = new JScrollPane(_popupComp) {
public Insets getInsets() {
return new Insets(5, 5, 5, 5);
}
};
if (_popupSize != null) {
spn.setPreferredSize(_popupSize);
}
else {
Dimension size = _popupComp.getPreferredSize();
if (size.getHeight() < 180) {
Dimension screenSize = Toolkit.getDefaultToolkit().
getScreenSize();
Point point = _popup.getLocationOnScreen();
if (point.y > screenSize.getHeight() / 2) {
spn.setPreferredSize(new Dimension(getWidth(),
screenSize.height - point.y - 50));
}
else {
spn.setPreferredSize(new Dimension(getWidth(),
screenSize.height / 2));
}
}
else {
spn.setPreferredSize(new Dimension(getWidth(),
size.height));
}
}
_popup.add(spn);
_popup.revalidate();
_popup.repaint();
}
}
}
private class MouseHandler extends MouseAdapter {
public void mousePressed(MouseEvent e) {
if (e.getClickCount() > 1) {
if (_popupComp instanceof JTree
&& ((JTree) _popupComp).getPathForLocation(e.getX(),
e.getY()) != null) {
TreePath path = ((JTree) _popupComp).getSelectionPath();
if (path == null) {
return;
}
TreeNode node = (TreeNode) path. getLastPathComponent();
if (node == null) {
return;
}
if (!node.isLeaf() &&
((JTree) _popupComp).isCollapsed(path)) {
return;
}
Object object = ((JTree)_popupComp).getSelectionPath().getLastPathComponent();
if (object != null) {
setSelectedItem(object.toString());
}
_popup.setVisible(false);
requestFocus();
}
}
}
}
private Component getEditorComponent() {
return this.getEditor().getEditorComponent();
}
private JComboBox getComboBox() {
return this;
}
private class FocusHander implements FocusListener {
public void focusGained(FocusEvent e) {
if (e.getSource() == getEditorComponent()) {
return;
}
getComboBox().repaint();
if (getComboBox().isEditable() && editor != null) {
getEditorComponent().requestFocus();
}
}
public void focusLost(FocusEvent e) {
if (e.getSource() ==
getComboBox().getEditor().getEditorComponent()) {
ComboBoxEditor editor = getComboBox().getEditor();
Object item = editor.getItem();
if (!e.isTemporary() && item != null &&
!item.equals(getComboBox().getSelectedItem())) {
getComboBox().actionPerformed(new ActionEvent(editor, 0, "",
EventQueue.getMostRecentEventTime(), 0));
}
}
if(e.getOppositeComponent() == _popupComp && null != _popup) {
_popup.setVisible(true);
}
getComboBox().repaint();
}
}
}
调用类:
public class Frame1 extends JFrame {
private MyComboBox _cmb = new MyComboBox();
private JLabel Label1 = new JLabel();
public Frame1() { try {
jbInit();
init();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void jbInit() throws Exception {
this.getContentPane().setLayout(null);
_cmb.setBounds(new Rectangle(99, 31, 113, 30));
this.getContentPane().add(_cmb);
Label1.setText("树形测试:");
Label1.setBounds(new Rectangle(21, 33, 71, 29));
this.getContentPane().add(Label1);
this.setSize(300,800);
}
private void init() {
_cmb.removeAllItems();
JTree supplierTree = new JTree(); ;
_cmb.setPopupComponent(supplierTree);
_cmb.convertPopupDataToComboBox();
}
public static void main(String[] args) {
Frame1 frame = new Frame1();
frame.setVisible(true);
}
}
哪位高手帮我看看。
private MyComboBox _cmb = new MyComboBox();
private JLabel Label1 = new JLabel();
public Frame1() { try {
jbInit();
init();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void jbInit() throws Exception {
this.getContentPane().setLayout(null);
_cmb.setBounds(new Rectangle(99, 31, 113, 30));
this.getContentPane().add(_cmb);
Label1.setText("树形测试:");
Label1.setBounds(new Rectangle(21, 33, 71, 29));
this.getContentPane().add(Label1);
this.setSize(300,800);
}
private void init() {
_cmb.removeAllItems();
JTree supplierTree = new JTree(); ;
_cmb.setPopupComponent(supplierTree);
_cmb.convertPopupDataToComboBox();
}
public static void main(String[] args) {
Frame1 frame = new Frame1();
frame.setVisible(true);
}
}
哪位高手帮我看看。