代码之家  ›  专栏  ›  技术社区  ›  Cheok Yan Cheng

jcombobox水平滚动条的便携式方法

  •  1
  • Cheok Yan Cheng  · 技术社区  · 14 年前

    Coderanch Sun Forum 我知道为jcombobox设置水平滚动条的技术。

    然而,他们建议的解决方案是有界的,看起来很具体。

    如您所见,如果用户使用的是带GTK+look n feel的Linux机器,或者使用Nimbus look n feel的Windows机器,那么下面的关键代码片段将无法正常工作。

    我如何能有一个便携式的方式,使jComboBox能够有一个水平滚动条?

    完整的源代码是 AutoCompleteJComboBox.java

    关键代码段如下:

    package org.yccheok.jstock.gui;
    
    public class AutoCompleteJComboBox extends JComboBox {
    
       @Override
        public void setUI(ComboBoxUI ui)
        {
            if (ui != null)
            {
                // Let's try our own customized UI.
                Class c = ui.getClass();
                final String myClass = "org.yccheok.jstock.gui.AutoCompleteJComboBox$My" + c.getSimpleName();
    
                try {
                    ComboBoxUI myUI = (ComboBoxUI) Class.forName(myClass).newInstance();
                    super.setUI(myUI);
                    return;
                } catch (ClassNotFoundException ex) {
                    log.error(null, ex);
                } catch (InstantiationException ex) {
                    log.error(null, ex);
                } catch (IllegalAccessException ex) {
                    log.error(null, ex);
                }
            }
    
            // Either null, or we fail to use our own customized UI.
            // Fall back to default.
            super.setUI(ui);
        }
    
        // This is a non-portable method to make combo box horizontal scroll bar.
        // Whenever there is a new look-n-feel, we need to manually provide the ComboBoxUI.
        // Any idea on how to make this portable?
        //
        protected static class MyWindowsComboBoxUI extends com.sun.java.swing.plaf.windows.WindowsComboBoxUI
        {
            @Override
            protected ComboPopup createPopup()
            {
                return new MyComboPopup(comboBox);
            }
        }
    
        protected static class MyMotifComboBoxUI extends com.sun.java.swing.plaf.motif.MotifComboBoxUI
        {
            @Override
            protected ComboPopup createPopup()
            {
                return new MyComboPopup(comboBox);
            }
        }
    
        protected static class MyMetalComboBoxUI extends javax.swing.plaf.metal.MetalComboBoxUI
        {
            @Override
            protected ComboPopup createPopup()
            {
                return new MyComboPopup(comboBox);
            }
        }
    
        private static class MyComboPopup extends BasicComboPopup
        {
            public MyComboPopup(JComboBox combo)
            {
                super(combo);
            }
    
            @Override
            public JScrollPane createScroller()
            {
                return new JScrollPane(list,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                        JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            }
        }
    }
    
    1 回复  |  直到 14 年前
        1
  •  0
  •   Cheok Yan Cheng    14 年前

    我通过下面的论坛解决了我的问题 Oracle Java Swing Forum