/*********************************************************/
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.text.*;
import javax.swing.plaf.basic.*;
import javax.swing.plaf.metal.MetalComboBoxUI;
import com.sun.java.swing.plaf.motif.MotifComboBoxUI;
import com.sun.java.swing.plaf.windows.WindowsComboBoxUI;
import javax.swing.plaf.*;
import java.awt.*;public class JTableComboBox extends JComboBox {    //main函数
    public static void main(String[] args) throws Exception{
        UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        JFrame f=new JFrame();
        JTableComboBox jtablecombobox = new JTableComboBox();
        f.getContentPane().add(BorderLayout.NORTH,jtablecombobox);
        f.setSize(400,250);
        f.setVisible(true);
    }
    
    //刷新界面,重写父类方法
    public void updateUI() {
        ComboBoxUI u=(ComboBoxUI)UIManager.getUI(this);
        if(u instanceof MetalComboBoxUI)
        {
            this.setUI(new MyMetalComboBoxUI());
        }
        else if(u instanceof BasicComboBoxUI)
        {
            this.setUI(new BasicComboBoxUI());
        }
        this.repaint();    }    //不同操作系统下的界面
    
    class MyMetalComboBoxUI extends MetalComboBoxUI {
        protected ComboPopup createPopup() {
            return new JTablePopup(comboBox);
        }
    }
    class MyWindowComboBoxUI extends WindowsComboBoxUI {
        protected ComboPopup createPopup() {
            return new JTablePopup(comboBox);
        }
    }
    class MyMotifComboBoxUI extends MotifComboBoxUI
    {
        protected ComboPopup createPopup() {
            return new JTablePopup(comboBox);
        }    
    }    //自定义弹出面板
    class JTablePopup extends BasicComboPopup {
        Object[][] data = new Object[][] { {"a", "b", "c"}, {"a", "b", "c"},
                          {"a", "b", "c"}, {"a", "b", "c"}, {"a", "b", "c"},
                          {"a", "b", "c"}, {"a", "b", "c"}, {"a", "b", "c"},
                          {"a", "b", "c"}, {"a", "b", "c"}
        };
        Object[] col = new Object[] {"a", "b", "c"};
        protected JTable table = new JTable(data, col);
        public JTablePopup(JComboBox combo) {
            super(combo);
            this.setOpaque(true);
            this.scroller.setViewportView(table);
            this.add(scroller);
            this.setOpaque(true);
            table.setOpaque(false);
        }        //因为想固定面版的宽度,所以重写了这个方法,
        //不知道大家有没有更好的方法
        protected Rectangle computePopupBounds(int px,int py,int pw,int ph) {
            Toolkit toolkit = Toolkit.getDefaultToolkit();
            Rectangle screenBounds;
            
            // Calculate the desktop dimensions relative to the combo box.
            GraphicsConfiguration gc = comboBox.getGraphicsConfiguration();
            Point p = new Point();
            SwingUtilities.convertPointFromScreen(p, comboBox);
            if (gc != null) {
                Insets screenInsets = toolkit.getScreenInsets(gc);
                screenBounds = gc.getBounds();
                screenBounds.width -= (screenInsets.left + screenInsets.right);
                screenBounds.height -= (screenInsets.top + screenInsets.bottom);
                screenBounds.x += (p.x + screenInsets.left);
                screenBounds.y += (p.y + screenInsets.top);
            }
            else {
                screenBounds = new Rectangle(p, toolkit.getScreenSize());
            }
            
            Rectangle rect = new Rectangle(px,py,200,ph);
            if (py+ph > screenBounds.y+screenBounds.height
                && ph < screenBounds.height) {
                rect.y = -rect.height;
            }
            return rect;
        }
            }
}/**********************************************************************/
以上是源码,正常情况下是Popup响应鼠标释放事件时隐藏,但我这个鼠标一按下去就隐藏了
并且不能响应鼠标的点击事件,鼠标的移动可以响应
我已经郁闷了好几天了,请高手指点一下迷津!!

解决方案 »

  1.   

    在这一句 table.setOpaque(false); 后面加上
    table.setFocusable(false);
      

  2.   

    combox的popup有焦点监听器,如果失去焦点就会自动隐藏起来,所以不能让popup里面加的组件获得焦点
      

  3.   

    怎么让编辑框里显示选中的行呢
    我用 super.comboBox.getModel().setSelectedItem(obj);
    只能显示一次,并且可编辑为false的时候显示不出
    是否要实现ListCellRenderer接口,但是我的数据是存在表格中
    并没有存到父类的JList实例中
    抓狂中`~~~~~~