2012-02-02 1 views
8

은 내가 JScrollPane의 내부 JList을 배치하기 위해 노력하고있어 그것은 알파벳과 같이 수직 열에서 항목을 나열했다 JScrollPane처럼 의 수직 방향으로 스크롤하십시오.JList - 수직 랩핑 방향으로 가로 대신 세로 스크롤 막대를 사용 하시겠습니까? 내가 좋겠, 그러나 공간 밖으로 <code>JList</code> 실행 더 많은 항목을 표시 할 때</p> <pre><code>A D G B E H C F </code></pre> <p>을 :

VERTICAL_WRAP을 사용할 때 작동합니다. 그러나, 세로 랩을 사용할 때 가로 스크롤 막대가 나타나고 HORIZONTAL_WRAP을 사용할 때 원하는 스크롤바가 표시되지만 항목이 싫어하는 순서로 배치됩니다. 내 케이크 먹어도 먹을 수 있을까요? 다음은 내가하려는 일의 간단한 예입니다.

enter image description here

내가 얻을 수있는 가장 가까운,하지만 난 수직 알파벳 순서를 유지하면서 세로로 스크롤 할 수 있도록하고 싶습니다. 내가의 비록했습니다

public class ScrollListExample { 
    static List<String> stringList = new ArrayList<String>(); 
    static { 
     for (int i = 0; i < 500; i++) { 
      stringList.add("test" + i); 
     } 
    } 

    public static void main(final String[] args) { 
     final JFrame frame = new JFrame(); 
     final Container contentPane = frame.getContentPane(); 
     final JList list = new JList(stringList.toArray()); 
     list.setLayoutOrientation(JList.VERTICAL_WRAP); 
     list.setVisibleRowCount(0); 
     final JScrollPane scrollPane = new JScrollPane(list); 
     contentPane.add(scrollPane); 
     frame.setPreferredSize(new Dimension(800, 400)); 
     frame.pack(); 
     frame.setVisible(true); 
    } 
} 

하나 개의 솔루션 : 셀 크기를 알고 경우 나 구성 요소 리스너를 만들고, resize 이벤트를 수신 할 수 있습니다. 해당 이벤트가 발생하면 가로 스크롤을 방지하기 위해 원하는 행 수를 계산할 수 있습니다. 이것은 단지 해킹처럼 보이고 가변 크기의 텍스트 구성 요소로 어떻게 작동하는지 모르겠습니다.

+1

좋은 질문 한 – mKorbel

+0

이유'사용할 때 가로 스크롤 막대를 얻고을 피하기 위해 작은 수정을 제안 VERTICAL_WRAP'은'VERICAL_WRAP => 셀이 세로로, 그리고 가로로 흘러가는 "newspaper style"레이아웃을 나타냅니다. 셀이 세로로 흐르면 치수가 바뀌므로 수평으로 채우기 시작합니다. 수평 스크롤 바 – RanRag

+0

그건 의미있는 일이지만, 내 구성 요소의 크기를 조정하려는 대부분의 시도는 예상했던대로 효과가 없습니다. 수평 스크롤 막대가 아닌 세로 스크롤 막대를 표시하려면 어떻게해야합니까? 크기를 조정해야하는 특정 구성 요소가 있습니까? – Lockyer

답변

2

저는 솔루션이 훌륭하고 해킹이되지 않는다고 생각합니다. 모든 기본 제공 기능은 기본적으로 동일한 작업을 수행해야합니다.

다음은 원하는대로 수정 한 예제입니다.

public class ScrollListExample { 
    static List<String> stringList = new ArrayList<String>(); 
    static { 
     for (int i = 0; i < 500; i++) { 
      stringList.add("test" + i); 
     } 
    } 

    public static void main(final String[] args) { 
     final JFrame frame = new JFrame(); 
     final Container contentPane = frame.getContentPane(); 
     final JList list = new JList(stringList.toArray()); 
     list.setLayoutOrientation(JList.VERTICAL_WRAP); 
     final JScrollPane scrollPane = new JScrollPane(list); 
     contentPane.add(scrollPane); 
     frame.setPreferredSize(new Dimension(800, 400)); 
     frame.pack(); 

     list.addComponentListener(new ComponentAdapter() { 
      @Override 
      public void componentResized(ComponentEvent e) { 
       fixRowCountForVisibleColumns(list); 
      } 
     }); 

     fixRowCountForVisibleColumns(list); 
     frame.setVisible(true); 
    } 

    private static void fixRowCountForVisibleColumns(JList list) { 
     int nCols = computeVisibleColumnCount(list); 
     int nItems = list.getModel().getSize(); 

     // Compute the number of rows that will result in the desired number of 
     // columns 
     int nRows = nItems/nCols; 
     if (nItems % nCols > 0) nRows++; 
     list.setVisibleRowCount(nRows); 
    } 

    private static int computeVisibleColumnCount(JList list) { 
     // It's assumed here that all cells have the same width. This method 
     // could be modified if this assumption is false. If there was cell 
     // padding, it would have to be accounted for here as well. 
     int cellWidth = list.getCellBounds(0, 0).width; 
     int width = list.getVisibleRect().width; 
     return width/cellWidth; 
    } 
} 
+0

내가 놓친 부분이 있었으면 좋았을 텐데, 아마도 이것은 가장 간단한 해결책 일 것 같습니다. – Lockyer

0

이게 무슨 일입니까? (기본 크기를 변경해야 할 수도 있습니다 ...)

public class ScrollListExample { 
    static List<String> stringList = new ArrayList<String>(); 
    static { 
     for (int i = 0; i < 500; i++) { 
      stringList.add("test" + i); 
     } 
    } 

    public static void main(final String[] args) { 
     final JFrame frame = new JFrame(); 
     final Container contentPane = frame.getContentPane(); 
     final JList list = new JList(stringList.toArray()); 
     final JScrollPane scrollPane = new JScrollPane(list); 
     scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
     scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 
     contentPane.add(scrollPane); 
     frame.setPreferredSize(new Dimension(200,200)); 
     frame.pack(); 
     frame.setVisible(true); 
    } 
} 
+0

죄송합니다. 목록에있는 항목이 사용 가능한 공간을 채우도록 명시 적으로 명시해야합니다. 따라서 창을 수평으로 확장하면 가능한 경우 추가 열을 추가해야합니다. – Lockyer

0

브릴리언트 코드 @Kevin K ... 나는 arithmeticexception이 (0으로 나누기)

private int computeVisibleColumnCount(JList list) 
    { 
     int cellWidth = list.getCellBounds(0, 0).width; 
     int width = list.getVisibleRect().width; 
     return width == 0 ? 1 : width/cellWidth; 
    } 
+0

이것은 자유로운 서있는 대답 대신에 집에서 더 많은 의견을 제시하는 것처럼 보입니다. – Lockyer

관련 문제