2009-06-03 7 views
3

I는 다음과 같습니다 드롭 다운 목록을하고 싶습니다 :JComboBox의 모양을 사용자 정의하려면 어떻게해야합니까?

alt text http://i44.tinypic.com/v80z81.png

JComboBox에 내가 필요로하는 기능을 제공합니다, 그래서 사용자 정의 구성 요소를 작성할 필요가 없습니다. 하지만이를 달성하기 위해 JComboBox의 전체 모양을 사용자 정의 할 수있는 방법을 찾을 수 없습니다. 어떤 제안?

업데이트 : 모든 JComboBox가 이와 같이 보이기를 바랍니다. 이 사용자 지정 모양으로 만 하나 만들고 싶습니다. 그래서 제가 말할 수있는 한, 맞춤형 L & F를 만드는 것은 해결책이 아닙니다.

+0

답변을 찾았습니까? –

+0

@Stephane, 나는 맞춤 구성 요소를 만들었습니다. –

+0

동일한 문제가 발생했습니다 ... 맞춤 구성 요소를 공유 할 수 있습니까? – sirbris

답변

8

JComboBox UI는 텍스트 필드, 목록 및 화살표로 구성됩니다. 당신은 어떤 준비가 L & F를 사용하거나 신디 L로 가장 쉽게 구현할 수 있습니다 맞게 사용자 정의 & F.

초기화 L & F :

SynthLookAndFeel lookAndFeel = new SynthLookAndFeel(); 

try { 
    lookAndFeel.load(YourClassAlongWithSynthXml.class.getResourceAsStream("synth.xml"), YourClassAlongWithSynthXml.class); 
} catch (ParseException e) { 
    e.printStackTrace(); 
} 

UIManager.setLookAndFeel(lookAndFeel); 
synth.xml이 같은 포함

:

<style id="textfield"> 
    <insets top="4" left="6" bottom="4" right="6" /> 

    <state> 
     <font name="Verdana" size="12" /> 

     <!-- 
     <color type="BACKGROUND" value="#D2DFF2" /> 
     --> 
     <color type="TEXT_FOREGROUND" value="#003c2d" /> 
    </state> 

    <imagePainter method="textFieldBorder" path="images/textfield.png" sourceInsets="4 6 4 6" paintCenter="false" /> 
</style> 

<bind style="textfield" type="region" key="TextField" /> 

<style id="arrowStyle"> 
    <imagePainter method="arrowButtonForeground" path="images/arrow-up.png" sourceInsets="0 0 0 0" stretch="false" direction="north" /> 
    <imagePainter method="arrowButtonForeground" path="images/arrow-down.png" sourceInsets="0 0 0 0" stretch="false" direction="south" /> 
    <imagePainter method="arrowButtonForeground" path="images/arrow-left.png" sourceInsets="0 0 0 0" stretch="false" direction="west" /> 
    <imagePainter method="arrowButtonForeground" path="images/arrow-right.png" sourceInsets="0 0 0 0" stretch="false" direction="east" /> 
</style> 

<bind key="ArrowButton" type="region" style="arrowStyle" /> 

<style id="comboArrowStyle"> 
    <imagePainter method="arrowButtonForeground" path="images/combobox-arrow.png" sourceInsets="0 0 0 0" stretch="false" direction="south" /> 
</style> 

Synth L & F를 사용하면 UI 구성 요소의 모양을 완전히 정의해야합니다.

하지만 당신은 단지 기본 L & F를 재사용 색상을 수정해야하는 경우, 당신은 콤보 상자의 UI 초기화 다음 조각을 사용할 수 있습니다 : 당신이 전체를 만들지 않으려면

final Color COLOR_BUTTON_BACKGROUND = Color.decode("#d3dedb"); 

UIManager.put("ComboBox.buttonBackground", COLOR_BUTTON_BACKGROUND); 
UIManager.put("ComboBox.buttonShadow", COLOR_BUTTON_BACKGROUND); 
UIManager.put("ComboBox.buttonDarkShadow", COLOR_BUTTON_BACKGROUND); 
UIManager.put("ComboBox.buttonHighlight", COLOR_BUTTON_BACKGROUND); 
+0

모양을 바꾸고 싶을 때 좋습니다. 모든 콤보 상자. 하지만이 사용자 정의 모양을 사용하여 하나를 만들고 싶습니다. –

+0

UIManager로 arrowStyle을 어떻게 변경합니까? –

1

을 모양과 느낌, 가장 간단한 대답은 jcombobox를 서브 클래스 화하고 paintComponent 메소드를 대체하고 원하는 것을 그립니다.

배경을 회색으로 채운 다음 밝은 색으로 텍스트와 화살표를 그립니다.

이 경로를 사용하는 경우 VM이 모양과 느낌을 업데이트하려고하면 콤보 상자가 잘못된 색으로 되돌아 가지 않도록 updateUI 메서드를 재정의해야 할 수도 있습니다.

관련 문제