2015-01-07 4 views
0

JOptionPane에서 JList을 사용하여 대화 상자에 줄을 표시합니다. 단순히 선의 내용에 따라 선의 배경색과 글꼴을 변경하기 만하면됩니다.JList에서 줄의 글꼴 및 배경색 변경

나는 이것에 대한 유용한 기사를 얻을 수 없으며 찾지 못했습니다. 내 실제 문제는 다음 코드에서 메서드 getListCellRendererComponent이 호출되지 않는다는 것입니다. 대화 상자는 "한 줄에 모든 텍스트"라는 한 줄로 나타나지만 색상/글꼴은 변경되지 않습니다.

아무도 도와 드릴 수 있습니까?

final DefaultListModel d = new DefaultListModel(); 
    final JList list = new JList(d); 

    ListCellRenderer renderer = new ListCellRenderer() { 
     @Override 
     public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 
      JLabel label = new JLabel(); 
      label.setText(value.toString()); 

      label.setFont(new Font("Courier New", Font.ITALIC, 12)); 
      label.setBackground(new Color(12, 12, 12)); 

      int i = 1/0; // <<<<<< --- does not throw an error, so it doesn't get into this. 
      return label; 
     } 
    }; 
    list.setCellRenderer(renderer); 

    for (int iList = 0; iList < alSuggestionsText.size(); iList++) { 
     // bigList[iList] = alTexte.get(iList); 
     d.addElement(alSuggestionsText.get(iList)); 
     // jlist.add(bigList); 
    } 

    final String sIgronreText = "any text for one line"; 

    d.addElement(sIgronreText); 

    final JList jlist = new JList(d); 

    JOptionPane jpane = new JOptionPane(); 
    jpane.showMessageDialog(null, jlist, sWikiidtemp, JOptionPane.PLAIN_MESSAGE); 

답변

3

두 개의 서로 다른 JLists가 있습니다. 처음에는 ListCellRenderrer를 설정합니다.

list.setCellRenderer(renderer); 

당신이 대화 상자에 표시 다른 하나

pane.showMessageDialog(null, jlist, "adsfasdf", JOptionPane.PLAIN_MESSAGE); 

추가 :

final JList jlist = new JList(d); 
    jlist.setCellRenderer(renderer); 

가이 작업을 진행합니다.

+0

오 마이 좋아. 나는 너무 어리 석다! !!! 그래 네가 맞아. 그렇게 자세히 조사해 주셔서 고맙습니다. 나는 지금 매우 행복하다 :-) –

+0

누구에게나 일어난다. –