2015-07-10 60 views
0

A Stepped ComboBox對於使下拉彈出式菜單比文本字段更加有用。但是,當新內容添加到列表中時,彈出窗口將獲取其初始寬度。Stepped ComboBox refresh

默認情況下

enter image description here

刷新後(新元素)

enter image description here

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

回答

0

ComboBox仍然使用其先前的PreferredSize。需要將首選大小設置回null,以便我們獲得列表中新內容所偏好的大小。

空隙javax.swing.JComponent.setPreferredSize(尺寸使用preferredSize)

將組件的最佳尺寸。如果preferredSize爲空,則會詢問用戶界面的首選大小。

public void setDimensions(int width) { 
    setPreferredSize(null); 
    Dimension d = getPreferredSize(); 
    setPreferredSize(new Dimension(width, d.height)); 
    setPopupWidth(d.width); 
} 

結果

enter image description here

1

你可以使用Combo Box Popup

這是一個更靈活的步進組合框的版本。最重要的是它可以在任何組合框上使用,因爲邏輯是在`PopupMenuListener'中實現的。

您可以控制彈出窗口的最大寬度。您甚至可以在組合框上方彈出顯示,而不是在下面。

+0

我會在將來使用它,謝謝。 –