JComboBox上的JScrollPane [英] JScrollPane on JComboBox

查看:138
本文介绍了JComboBox上的JScrollPane的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个组合框,但是某些元素比我的框的宽度还要宽.因此,我尝试添加一个水平的滚动窗格,唯一能看到的是内部带有组合框的滚动窗格! 自发地我做错了.所以这是我的没有jscrolpane的代码.

I made a combobox but some of the elements are wider than the width of my box. So I tried to add a horizontal scrolpane and the only I could see is a scrolpane with a combobox inside! Certanly something I do wrong. So here is my code without the jscrolpane.

    issuerdocumentJComboBox=new JComboBox<>(issuermodel);//the compo box
    issuerdocumentJComboBox.setBounds(430, 120, 100, 30);
    searchDocumentesJPanel.add(issuerdocumentJComboBox);   

如何向组合框添加水平滚动窗格? 谢谢!

How can I add to the combobox a horizontal scrollpane? Thank you!

推荐答案

有可能!这是我编写的一个小程序来显示我的解决方案:

It is possible!! Here's a little program I wrote to show my solution:

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,该ComboBoxEditor在滚动窗格中显示JTextField编辑器(更容易扩展BasicComboBoxEditor). 然后,将覆盖getEditorComponent()函数以返回滚动窗格,而不是文本字段.

The approach is to create a custom ComboBoxEditor that displays the JTextField editor in a scroll pane (easier to just extend BasicComboBoxEditor). The getEditorComponent() function is then overridden to return the scroll pane instead of the text field.

当您调用combobox.setEditor(new MyEditor())时,这两个隐藏的函数在内部被调用,因此如果您看不到它正在被使用,请不要担心.

These two overidden functions are called internally when you call combobox.setEditor(new MyEditor()) so don't worry if you can't see it being used.

这是程序的截屏,其中显示的元素比组合框还宽:

Here's a screenshot of the program showing an element wider than combobox:

好运!! :-)

这篇关于JComboBox上的JScrollPane的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆