高分求救JComboBox下拉树问题(回帖有分)

lhlove271015 2010-05-19 04:57:31
在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();
}
}
}
...全文
253 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
lhlove271015 2010-07-21
  • 打赏
  • 举报
回复
谁能救救我啊
l2008h 2010-07-20
  • 打赏
  • 举报
回复
有大虾么,帮解决下咯,我是来看结果的
lhlove271015 2010-06-14
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 congliu 的回复:]
http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html
参见这个例程
[/Quote]
去看看去。
lhlove271015 2010-06-14
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 huntor 的回复:]
参考SwingHacks 第十个重写一个
[/Quote]
我试试
lhlove271015 2010-06-14
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 east271536394 的回复:]
Java code

看看1.5与1.6 源代码的实现,有什么不同
还是你自己代码有写的有问题
刚看了你代码,有问题。
以后碰到这样的问题,一定要想想是否是自己代码有问题。(最终是代码问题)
[/Quote]
什么问题?麻烦指出。我就是找不到问题才来求救的。
congliu 2010-05-30
  • 打赏
  • 举报
回复
http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html
参见这个例程
izard999 2010-05-30
  • 打赏
  • 举报
回复
很少玩swing,帮你顶下
dyh200896 2010-05-30
  • 打赏
  • 举报
回复
这么庞大,可能LZ才是解决问题的最佳人选哦
huntor 2010-05-29
  • 打赏
  • 举报
回复
参考SwingHacks 第十个重写一个
yueguangkai001 2010-05-29
  • 打赏
  • 举报
回复
悲剧,你搜索以下java树看看,应该有相关的人讨论这个问题
bingchengxueyuan 2010-05-29
  • 打赏
  • 举报
回复
不太懂!
  • 打赏
  • 举报
回复
不懂,顶一下
East271536394 2010-05-28
  • 打赏
  • 举报
回复

看看1.5与1.6 源代码的实现,有什么不同
还是你自己代码有写的有问题
刚看了你代码,有问题。
以后碰到这样的问题,一定要想想是否是自己代码有问题。(最终是代码问题)
lhlove271015 2010-05-28
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 huntor 的回复:]
在1.5下试了。在蜗牛一般的系统下,先弹出的是普通combobox,一闪变成tree
[/Quote]
我写的那个类本来就是覆盖了以前的popup
lhlove271015 2010-05-28
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 ytu2006123 的回复:]
看一下1.5和1.6版本的API分别是如何定义JComboBox 的
[/Quote]
我当然有看,没什么不同。
huntor 2010-05-24
  • 打赏
  • 举报
回复
在1.5下试了。在蜗牛一般的系统下,先弹出的是普通combobox,一闪变成tree
ytu2006123 2010-05-24
  • 打赏
  • 举报
回复
看一下1.5和1.6版本的API分别是如何定义JComboBox 的
lhlove271015 2010-05-24
  • 打赏
  • 举报
回复
100分,竟然都没人回帖,没人帮忙。
lhlove271015 2010-05-20
  • 打赏
  • 举报
回复
群里边这么多高手。
我都弄了好几天了,愣是没找着原因,也没有找着解决办法。
l2008h 2010-05-20
  • 打赏
  • 举报
回复
帮顶,mark
加载更多回复(7)

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧