2017-01-06 12 views
0

JComboBoxes 및 긴 문자열에 문제가 있습니다. 결과적으로 JComboBox는 JFrame에 비해 너무 길어서 전체 상자를 표시하지 않습니다. ,JComboBox가 JFrame에 너무 긴 경우

public class ComboBoxFullMenu<E> extends JComboBox<E> 
{ 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 

    public ComboBoxFullMenu(Vector<E> items) 
    { 
     super(items); 
     addActionListener(this); 
    } 

    public ComboBoxFullMenu() 
    { 
     super(); 
     addActionListener(this); 
    } 

    public ComboBoxFullMenu (DefaultComboBoxModel<E> defaultComboBoxModel) 
    { 
     super(defaultComboBoxModel); 
     addActionListener(this); 
    } 

    /** 
    * Small hack to get pop up menu size bigger enough to show items even though 
    * the combo box size could be smaller 
    * */ 
    private boolean layingOut = false; 

    @Override 
    public void doLayout() 
    { 
     try 
     { 
      layingOut = true; 
      super.doLayout(); 
     } 
     finally 
     { 
      layingOut = false; 
     } 
    } 

    @Override 
    public Dimension getSize() 
    { 
     Dimension dim = super.getSize(); 
     if (!layingOut) 
     { 
      dim.width = Math.max(dim.width, getPreferredSize().width); 
     } 
     return dim; 
    } 
} 

가 나는 또한 짧은 방법으로 선택한 항목을 표시하는 데 DefaultListCellRenderer 및 ObjectTroStringConverter를 사용하여 시도 :

JFrame frame = new JFrame(); 
    frame.setSize(500, 500); 
    JPanel panel = new JPanel(); 
    DesignGridLayout layout = new DesignGridLayout(panel); 
    Vector<ArrayList<String>> content = new Vector<ArrayList<String>>(); 
    ArrayList<String> list = new ArrayList<String>(); 
    list.add("12345"); 
    list.add("abcde"); 
    list.add("12345"); 
    list.add("abcde"); 
    list.add("12345"); 
    list.add("abcde"); 
    list.add("12345"); 
    list.add("abcde"); 
    list.add("12345"); 
    list.add("abcde"); 
    list.add("12345"); 
    list.add("abcde"); 
    content.add(list); 
    ComboBoxFullMenu<ArrayList<String>> combobox = new ComboBoxFullMenu<ArrayList<String>>(content); 
    combobox.setRenderer(new DefaultListCellRenderer() { 

     private static final long serialVersionUID = 1L; 

     @Override 
     public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) 
     { 
      super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); 
      ArrayList<String> array = (ArrayList<String>) value; 
      if (!array.isEmpty()) 
      { 
       String text = ""; 
       for (String info : array) 
       { 
        text += info + "; "; 
       } 
       setText(text); 
      } 
      return this; 
     } 
    }); 
    AutoCompleteDecorator.decorate(combobox, new ObjectToStringConverter() 
    { 

     @Override 
     public String getPreferredStringForItem(Object object) 
     { 
      if (object != null) 
      { 
       if (object instanceof ArrayList) 
       { 
        ArrayList<String> list = (ArrayList<String>) object; 
        String text = ""; 
        for (String info : list) 
        { 
         text += info + "; "; 
        } 
        return text; 
       } 
       else 
       { 
        return object.toString(); 
       } 
      } 
      return null; 
     } 
    }); 
    layout.row().grid().add(combobox); 
    frame.add(panel); 
    frame.setVisible(true); 

그리고 여기에 클래스 ComboBoxFullMenu입니다 : 여기 나는 내 문제를 보여줍니다 약간의 코드를 하지만 난 dropdownmenu에있는 모든 정보를보고 JComboBox는 가장 긴 항목에 대한 너비를 계산하는 것 같아? 다른 문제가 있으면 알려주세요.

답변

1

WideComboBox을 사용해보세요. JComboBox의 너비를 설정할 수 있지만 팝업이 표시되면 전체 항목이 표시됩니다. 팝업이 JComboBox보다 더 넓음을 의미합니다. 또한 this이 유용 할 수도 있습니다.

+0

답변 해 주셔서 감사합니다.하지만 이미 WideComboBox를 사용하고 있습니다. 그것은 내 클래스 ComboBoxFullMenu에서 볼 수있는 것과 같은 코드입니다. JTable이 작동하고있는 이유와이 간단한 예제에서 작동하지 않는 이유를 알 수 없습니다. – mavok

+0

combobox의 기본 크기를 설정해보십시오 – Jayfray

+0

combobox.setPreferredSize (새 치수 (20, 20)); 아무것도 바뀌지 않습니다 – mavok

1

체크 아웃 Combo Box Popup은 팝업 너비를 제어 할 수 있습니다. 당신은 가장 큰 문자열의 크기로 폭

  1. 을 설정하거나
  2. 는 최대 너비를 지정합니다.

문자열이 지정된 너비보다 큰 경우 팝업에서 스크롤을 켤 수 있습니다.