a stepped combobox useful make drop-down pop-up wider text field. when new content added list, pop-up gets initial width back.
by default

after refresh (new element)

sscce
import java.awt.dimension; import java.awt.flowlayout; import java.awt.rectangle; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.windowadapter; import java.awt.event.windowevent; import java.util.arraylist; import java.util.arrays; import java.util.list; import java.util.vector; import javax.swing.comboboxmodel; import javax.swing.defaultcomboboxmodel; import javax.swing.jbutton; import javax.swing.jcombobox; import javax.swing.jframe; import javax.swing.plaf.basic.basiccombopopup; import javax.swing.plaf.basic.combopopup; import javax.swing.plaf.metal.metalcomboboxui; public class steppedcomboboxrefresh extends jframe { private list<string> list; private final steppedcombobox combo; public steppedcomboboxrefresh() { super("steppedcombobox refresh"); this.list = new arraylist<string>(arrays.aslist(new string[]{ "aaa", "aaaaaa" })); this.combo = new steppedcombobox(this.list.toarray()); this.combo.setdimensions(50); jbutton addbutton = new jbutton("add longer string"); addbutton.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { list.add(list.get(list.size()-1) + "aaa"); combo.setmodel(new defaultcomboboxmodel(list.toarray())); combo.setdimensions(50); } }); getcontentpane().setlayout(new flowlayout()); getcontentpane().add(this.combo); getcontentpane().add(addbutton); } public static void main (string args[]) { steppedcomboboxrefresh f = new steppedcomboboxrefresh(); f.addwindowlistener(new windowadapter() { @override public void windowclosing(windowevent e) { system.exit(0); } }); f.setsize (300, 100); f.setvisible(true); } } class steppedcomboboxui extends metalcomboboxui { @override protected combopopup createpopup() { basiccombopopup popup = new basiccombopopup( this.combobox ) { @override public void show() { dimension popupsize = ((steppedcombobox)this.combobox).getpopupsize(); popupsize.setsize( popupsize.width, getpopupheightforrowcount( this.combobox.getmaximumrowcount() ) ); rectangle popupbounds = computepopupbounds( 0, this.combobox.getbounds().height, popupsize.width, popupsize.height); this.scroller.setmaximumsize( popupbounds.getsize() ); this.scroller.setpreferredsize( popupbounds.getsize() ); this.scroller.setminimumsize( popupbounds.getsize() ); this.list.invalidate(); int selectedindex = this.combobox.getselectedindex(); if ( selectedindex == -1 ) { this.list.clearselection(); } else { this.list.setselectedindex( selectedindex ); } this.list.ensureindexisvisible( this.list.getselectedindex() ); setlightweightpopupenabled( this.combobox.islightweightpopupenabled() ); show( this.combobox, popupbounds.x, popupbounds.y ); } }; popup.getaccessiblecontext().setaccessibleparent(this.combobox); return popup; } } class steppedcombobox extends jcombobox { protected int popupwidth; public steppedcombobox(comboboxmodel amodel) { super(amodel); setui(new steppedcomboboxui()); this.popupwidth = 0; } public steppedcombobox(final object[] items) { super(items); setui(new steppedcomboboxui()); this.popupwidth = 0; } public steppedcombobox(vector items) { super(items); setui(new steppedcomboboxui()); this.popupwidth = 0; } public void setpopupwidth(int width) { this.popupwidth = width; } public dimension getpopupsize() { dimension size = getsize(); if (this.popupwidth < 1) { this.popupwidth = size.width; } return new dimension(this.popupwidth, size.height); } public void setdimensions(int width) { dimension d = getpreferredsize(); setpreferredsize(new dimension(width, d.height)); setpopupwidth(d.width); } }
you use combo box popup.
it more flexible version of stepped combo box. best of can used on combo box since logic implemented in `popupmenulistener'.
you can control maximum width of popup. can have popup display above combo box instead of below.
Comments
Post a Comment