3

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!

Ondrej Peterka
  • 3,349
  • 4
  • 35
  • 49
Vagelism
  • 601
  • 1
  • 16
  • 27
  • In your code there is no mention of a scrollpane. What did you try exactly? – Marko Topolnik Apr 22 '12 at 11:10
  • I removed it from my code since is not working! I made a scrollpane and add the combobox but then it appears a scrollpane with the combobox inside. – Vagelism Apr 22 '12 at 11:50
  • Well, if you add a combo to a scrollpane, that's exactly what you are supposed to get. But you want to add a scrollpane to a combo box's list component, if I understand correctly. If it's doable at all, it will probably involve some hacking. – Marko Topolnik Apr 22 '12 at 11:54
  • 2
    If you thoroughly read [this OTN Discussion thread](https://forums.oracle.com/forums/thread.jspa?threadID=1773495), you'll find what you need. Hacking it is, alright. – Marko Topolnik Apr 22 '12 at 11:57
  • Thank you!I saw this approach as I was searching for the solution but seems beyond my skills! – Vagelism Apr 22 '12 at 12:35

1 Answers1

4

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;
        }
    }
}

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.

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: screenshot

GOODLUCK!! :-)

davidXYZ
  • 719
  • 1
  • 8
  • 16