有可能的!!这是我写的一个小程序来展示我的解决方案:
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.plaf.basic.BasicComboBoxEditor;
public class TestComboScroll {
public static void main(String[] args) {
JFrame frame = new JFrame("Scroll inside Combo");
JComboBox combobox = new JComboBox();
combobox.addItem("");
combobox.addItem("Item1");
combobox.addItem("Item2 Item2 Item2 Item2 Item2 Item2 Item2 Item2 Item2 Item2 Item2 Item2 Item2 Item2");
combobox.addItem("Item3");
combobox.setEditor(new MyEditor());
combobox.setEditable(true);
combobox.setPreferredSize(new Dimension(200, 50));
frame.add(combobox);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
static class MyEditor extends BasicComboBoxEditor{
JScrollPane scroller = new JScrollPane();
//NOTE: editor is a JTextField defined in BasicComboBoxEditor
public MyEditor(){
super();
scroller.setViewportView(editor);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
}
/** Return a JScrollPane containing the JTextField instead of the JTextField **/
@Override
public Component getEditorComponent() {
return scroller;
}
/** Override to create your own JTextField. **/
@Override
protected JTextField createEditorComponent() {
JTextField editor = new JTextField();
editor.setBorder(null);
/*editor.setEditable(false); //If you want it not to be editable */
return editor;
}
}
}
方法是创建一个自定义 ComboBoxEditor,在滚动窗格中显示 JTextField 编辑器(更容易扩展 BasicComboBoxEditor)。然后重写该getEditorComponent()
函数以返回滚动窗格而不是文本字段。
这两个覆盖函数在您调用时在内部调用combobox.setEditor(new MyEditor())
,因此如果您看不到它正在使用,请不要担心。
这是程序的屏幕截图,显示了比组合框更宽的元素:
祝你好运!!:-)