Swing Problem: Horizontaler Scrollbalken in JComboBox hinzufügen

Bexan

Mitglied
Guten Morgen zusammen,

ich komm bei meinem Projekt gerade nicht weiter. Ich möchte gerne in einer JComboBox eine horizontalen Scrollbalken hinzufügen. Es gibt auch einige Threads dafür, aber weitergebracht hat mich da noch nichts. Vielleicht ist es daher einfacher, mal kurz geschriebenen Testcode, der das Problem auf jeden Fall innehat, zu kopieren. Kategorie Einsteiger / Newbie zähl ich mich direkt dazu.

Java:
import javax.swing.*;
import java.awt.event.*;
public class ComboBox extends JFrame
{
    public ComboBox()
    {
        JComboBox JCB = new JComboBox();
        JCB.setBounds(57,20,186,20);
        JCB.setMaximumRowCount(4);
        JCB.addItem("passt");
        JCB.addItem("passt noch");
        JCB.addItem("passt noch so grad");
        JCB.addItem("passt noch ganz knapp");
        JCB.addItem("passt nicht mehr wirklich");
        JCB.addItem("natürlich viel zu viel Inhalt");
        JCB.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e) {
                    JComboBox jcmbType = (JComboBox) e.getSource();
                    System.out.println("Changed To: " + (String) jcmbType.getSelectedItem());
                }
            });
            
        //Hier ist der Fehler. Die horizontale ScrollBar wird nicht in der ComboBox angezeigt.
        //Im Gegensatz, dass die vertikale ScrollBar angezeigt wird, ohne Manuelles hinzufügen.
        JScrollPane scroller = new JScrollPane(JCB);
        scroller.setHorizontalScrollBar(new JScrollBar(JScrollBar.HORIZONTAL));
        scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

        JFrame jf = new JFrame();
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setBounds(100,50,300,100);
        JPanel jp = new JPanel();
        jp.setLayout(null);
        jp.add(JCB);
        jf.add(jp);
        jf.setVisible(true);
    }

    public static void main(String[] args)
    {
        new ComboBox();
    }
}

Ich hoffe, alles entspricht den Regeln. Sonst bitte klagen, dass ich es nächste Mal besser machen kann. Auf jeden Fall nicht nichts schreiben 🙂
Grüße,
Bex
 
Wenn du einen horizontalen Scrollbalken benötigst, dann musst du dir dafür eine eigene ComboBoxUI schreiben. In dieser überschreibst du die Methode createScroller() und gibst als Wert eine neue JScrollPane mit horizontaler und vertikaler Scrollleiste zurück.

Die neue UI-Klasse übergibst du an deine JComboBox mit der Methode setUI();
Übrigens kannst du auch alternativ oder zusätzlich die Popupliste verbreitern.

Solltest du nicht klar kommen, einfach fragen.

Gruß
JP

----
If this post was helpful, please donate by pressing the "thanks"-button.
 
Zuletzt bearbeitet:
Hört sich gut an. Ist nur alles ein bisschen zu hoch für mich. :rtfm:
Aber jetzt werde ich auf jeden Fall mit anderen Begriffen weitergoogeln. Da kommt bestimmt was bei rum!
Danke!! Evtl meld ich mich nochmal...
mfg
Bex
 
Ich habe dir hier mal eine modifizierte JComboBox programmiert.
Sie kann sowohl eine breitere Popup-Liste anzeigen als auch einen horizontalen Scrollbalken.
An statt einer JComboBox kannst du jetzt die JFlexComboBox verwenden.


Hier der Code für die neue ComboBoxUI
Java:
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.plaf.basic.BasicComboBoxUI;
import javax.swing.plaf.basic.BasicComboPopup;
import javax.swing.plaf.basic.ComboPopup;

public class ScrollComboUI extends BasicComboBoxUI {  
	protected ComboPopup createPopup() {  
		return new ScrollComboPopup(comboBox);  
	}  
	
	private class ScrollComboPopup extends BasicComboPopup {  
		public ScrollComboPopup(JComboBox combo){  
			super(combo);
		}  
		
		protected JScrollPane createScroller() {  
			return new JScrollPane(list,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);  
		}  
	}
}

Hier der Code für die modifizierte JComboBox, die ich mal "JFlexComboBox" getauft habe
Java:
import java.awt.Dimension;
import javax.swing.JComboBox;

public class JFlexComboBox extends JComboBox{

	private static final long serialVersionUID = 1L;

	private boolean layingOut;
	private int popupWidth = 0;
	
	public JFlexComboBox(){
		super();
		super.setUI(new ScrollComboUI());
		
	}
	
	public void setPopupWidth(int width){
		this.popupWidth = width;
	}
	
	public void setMaximumRowCount(int count){
		super.setMaximumRowCount(count + 1);
	}
	
	public Dimension getSize() {  
		Dimension dim = super.getSize();  
		if (!layingOut && popupWidth != 0)  
			dim.width = popupWidth;		
		return dim;  
	}  
	
	public void doLayout() {  
		try {  
			layingOut = true;  
			super.doLayout();  
		} 
		finally {  
			layingOut = false;  
		}  
	}  
}

Ist zwar mal so eben "rapid prototyped", aber ich hoffe es erklärt dir so ungefähr was du machen kannst.

Gruß
JP

---
If this was helpful please donate by pressing the "thanks"-button
 
Zuletzt bearbeitet:
Hey, ich seh gerade dein "rapid prototyped".
Ich hatte zwischenzeitlich folgende Zeile im Code eingesetzt....

Java:
        JCB.setUI((ComboBoxUI) MyComboBoxUI.createUI(JCB));
... und noch eine Klasse hinzugefügt, die folgendermaßen aussieht.
Java:
class MyComboBoxUI extends BasicComboBoxUI {
    public static ComponentUI createUI(JComboBox c) {
        c.setMaximumRowCount(4);
        return new MyComboBoxUI();
    }

    protected JScrollPane createScroller() {
        JScrollPane scroller = new JScrollPane();
        scroller.setVerticalScrollBar(new JScrollBar(JScrollBar.VERTICAL));
        scroller.setHorizontalScrollBar(new JScrollBar(JScrollBar.HORIZONTAL));
        scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        return scroller;
    }
}

Ich seh Parallelen. Aber auch der Unterschiede. Und die machen es bestimmt aus, warum es bei meinem nicht funzt....
 

Zurück
Oben