2013-12-14 5 views
0

에서 강조 삭제 그런 다음 목록을 열고 커서를 항목 위에 올려 놓으면 해당 항목이 강조 표시되고 모든 항목이 정상적으로 표시되고 아무 것도 잘못 표시되지 않습니다.JComboBox에

after

그래서 제 질문은 다음과 같습니다 :
이 어떻게 강조 멀리 갈 수

는 그러나 문제는 지금은 항목에 클릭 한 번 강조를 유지하는 것이 무엇입니까?
커뮤니티의 패키지 나 오버로드 등으로 인해 어려움을 겪지 않는 것이 좋습니다.

맞다면 콤보 상자의 작업 수신기의 '루트'에 있어야합니까?
은 그래서 :

public void actionPerformed(ActionEvent e) 
{ 
    if(e.getSource() == comboBox) 
    { 
     // code to delete the highlighting 
    } 
} 
+1

다른 [Look and Feel] (http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html)을 사용해보십시오. – jaco0646

+1

* "어떻게 강조 표시를 제거 할 수 있습니까?"* 해당 가상 콤포넌트를 언제 어떻게 알 수 있습니까? 초점이 있니? 이것은 제작 과정에서 또 다른 '사용할 수없는 GUI'처럼 들립니다. :( –

+0

@AndrewThompson 아니요, 아니요, 선택한 항목이 아닌 항목 만 선택하면 사라집니다 (드롭 다운 메뉴의 항목 위에 마우스를 올려 놓을 때) –

답변

0

이 비슷한 문제를 가진 사람들을위한보다 쉽게하려면, 여기에 내가 쓴 렌더러에 대한 코드 이전 코드로 제대로 코드가 업데이트 작동하지 않았고 작동합니다 모두 괜찮음

3

강조 표시는 콤보 상자의 기본 렌더러에 의해 수행된다.

사용자 정의 렌더러를 제공하는 예제는 Providing Custom Renderers에있는 스윙 튜토리얼의 섹션을 참조하십시오. 선택한 값에 따라 배경/전경을 변경하지 않는 렌더러 만 필요합니다.

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

class ComboBoxRenderer extends JLabel implements ListCellRenderer 
{ 
    private boolean colorSet; 
    private Color selectionBackgroundColor; 

    public ComboBoxRenderer() 
    { 
     setOpaque(true); 
     setHorizontalAlignment(LEFT); 
     setVerticalAlignment(CENTER); 
     colorSet = false; 
     selectionBackgroundColor = Color.red; // Have to set a color, else a compiler error will occur 
    } 

    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) 
    { 
     // Check if color is set (only runs the first time) 
     if(!colorSet) 
     { 
      // Set the list' background color to original selection background of the list 
      selectionBackgroundColor = list.getSelectionBackground(); 
      // Do this only one time since the color will change later 
      colorSet = true; 
     } 

     // Set the list' background color to white (white will show once selection is made) 
     list.setSelectionBackground(Color.white); 

     // Check which item is selected 
     if(isSelected) 
     { 
      // Set background color of the item your cursor is hovering over to the original background color 
      setBackground(selectionBackgroundColor); 
     } 
     else 
     { 
      // Set background color of all other items to white 
      setBackground(Color.white); 
     } 

     // Do nothing about the text and font to be displayed 
     setText((String)value); 
     setFont(list.getFont()); 

     return this; 
    } 
} 

편집 :

+0

이 솔루션은 정말로 도움이되었습니다. 처음에는 모든 것이 실제로 복잡해 보였습니다 (저는 진정한 신인 선수입니다).하지만 결국에는 매우 쉬워 보였습니다. –

관련 문제