2012-01-17 4 views
1

JComboBox의 가로 스크롤 막대를 OTN에서 스레드 - Horizontal scrollbar for JComboBox across multiple look and feel으로 참조를 사용하여 추가하려하지만 내 경우에는 작동하지 않습니다.JComboBox에서 가로 스크롤 막대를 추가 할 수 없습니다.

JComboBox에 가로 스크롤 막대를 올바르게 추가하는 방법은 무엇입니까?

코드 -

public class TestJComboBoxWithScrollBar { 

    TestJComboBoxWithScrollBar() { 
     JDialog jDialog = new JDialog(); 
     jDialog.setTitle("Test JComboBox With ScrollBar"); 
     JPanel jPanel_Sort = new JPanel(); 
     GridBagLayout gbl = new GridBagLayout(); 
     GridBagConstraints bagConstraints = new GridBagConstraints(); 
     jPanel_Sort.setLayout(gbl); 
     bagConstraints.gridwidth = 1; 
     bagConstraints.gridheight = 1; 
     bagConstraints.fill = GridBagConstraints.NONE; 
     bagConstraints.anchor = GridBagConstraints.WEST; 
     bagConstraints.weightx = 0; 
     bagConstraints.weighty = 0; 
     bagConstraints.insets = new Insets(5, 5, 5, 5); 
     bagConstraints.gridx = 0; 
     bagConstraints.gridy = 0; 
     SampleJComboBoxWithScrollBar cmbHeaders = new SampleJComboBoxWithScrollBar(); 
     cmbHeaders.addItem("aaaaaaaaaaaaa"); 
     cmbHeaders.addItem("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); 
     cmbHeaders.setPreferredSize(new Dimension(190, 50)); 
     cmbHeaders.setMinimumSize(new Dimension(190, 50)); 
     cmbHeaders.setMaximumSize(new Dimension(190, 50)); 
     jPanel_Sort.add(cmbHeaders, bagConstraints); 
     JCheckBox chkOrderBy = new JCheckBox("Asc"); 
     bagConstraints.gridx = 1; 
     bagConstraints.gridy = 0; 
     jPanel_Sort.add(chkOrderBy, bagConstraints); 
     jPanel_Sort.setPreferredSize(new Dimension(220, 70)); 
     jPanel_Sort.setMinimumSize(new Dimension(220, 70)); 
     jPanel_Sort.setMaximumSize(new Dimension(220, 70)); 
     jDialog.add(jPanel_Sort, BorderLayout.CENTER); 
     jDialog.setPreferredSize(new Dimension(300, 100)); 
     jDialog.pack(); 
     jDialog.setResizable(false); 
     jDialog.setModal(true); 
     jDialog.setVisible(true); 
    } 

    public static void main(String[] argu) { 
     new TestJComboBoxWithScrollBar(); 
    } 

    class SampleJComboBoxWithScrollBar extends JComboBox { 

     SampleJComboBoxWithScrollBar() { 
      super(); 
      this.addPopupMenuListener(this.getPopupMenuListener()); 
      this.adjustScrollBar(); 
     } 

     private void adjustPopupWidth() { 
      if (getItemCount() == 0) { 
       return; 
      } 
      Object comp = getUI().getAccessibleChild(this, 0); 
      if (!(comp instanceof JPopupMenu)) { 
       return; 
      } 
      JPopupMenu popup = (JPopupMenu) comp; 
      JScrollPane scrollPane = (JScrollPane) popup.getComponent(0); 
      Object value = getItemAt(0); 
      Component rendererComp = getRenderer().getListCellRendererComponent(new JList(), value, 0, false, false); 
      if (rendererComp instanceof JXTable) { 
       scrollPane.setColumnHeaderView(((JTable) rendererComp).getTableHeader()); 
      } 
      Dimension prefSize = rendererComp.getPreferredSize(); 
      Dimension size = scrollPane.getPreferredSize(); 
      size.width = Math.max(size.width, prefSize.width); 
      scrollPane.setPreferredSize(size); 
      scrollPane.setMaximumSize(size); 
      scrollPane.revalidate(); 
     } 

     private void adjustScrollBar() { 
      if (getItemCount() == 0) { 
       return; 
      } 
      Object comp = getUI().getAccessibleChild(this, 0); 
      if (!(comp instanceof JPopupMenu)) { 
       return; 
      } 
      JPopupMenu popup = (JPopupMenu) comp; 
      JScrollPane scrollPane = (JScrollPane) popup.getComponent(0); 
      scrollPane.setHorizontalScrollBar(new JScrollBar(JScrollBar.HORIZONTAL)); 
      scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 
     } 

     private PopupMenuListener getPopupMenuListener() { 

      return new PopupMenuListener() { 

       @Override 
       public void popupMenuWillBecomeVisible(PopupMenuEvent e) { 
        adjustPopupWidth(); 
       } 

       @Override 
       public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { 
       } 

       @Override 
       public void popupMenuCanceled(PopupMenuEvent e) { 
       } 
      }; 

     } 
    } 
} 
+0

에서 호출되지 않습니다 결코 adjustScrollBar() 빨리,이 [SSCCE] (HTTP를 게시 한 후 무효 : // sscce.org/). –

답변

3

당신이 코드에 의해 게시하는 의미 경우 @Kleopatra 더 도움이 필요한 PopupMenuListener

private PopupMenuListener getPopupMenuListener() { 

    return new PopupMenuListener() { 

     @Override 
     public void popupMenuWillBecomeVisible(PopupMenuEvent e) { 
      //adjustPopupWidth(); 
      adjustScrollBar(); 
     } 

     @Override 
     public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { 
     } 

     @Override 
     public void popupMenuCanceled(PopupMenuEvent e) { 
     } 
    }; 
} 
+0

예, 작품입니다. 감사 –