2012-06-08 15 views
9

Tôi đang cố gắng tạo ComboBox sử dụng các màu khác nhau cho các mục khác nhau. Tôi đã viết ra một số mã thử nghiệm nhưng nó dường như không hoạt động. Việc thêm vào trình kết xuất làm cho chương trình bị lỗi nhưng việc nhận xét nó làm cho hộp hiển thị trên khung.Nhiều màu cho mỗi mục trong JComboBox

Có bất kỳ điều gì tôi thiếu hoặc tôi đang làm điều này theo cách sai? Tôi đã thử sử dụng hướng dẫn custom ComboBox Renderer làm ví dụ.

Đây là mã của tôi:

TestComboColor.java

import java.awt.Color; 

import javax.swing.JComboBox; 
import javax.swing.JFrame; 


public class TestComboColor { 

    static Color[] colors = {Color.BLUE, Color.GRAY, Color.RED}; 
    static String[] strings = {"Test1", "Test2", "Test3"}; 

    public static void main(String[] args) 
    { 
     JFrame frame = new JFrame("JAVA"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JComboBox cmb = new JComboBox(); 
     ComboBoxRenderer renderer = new ComboBoxRenderer(cmb); 

     renderer.setColors(colors); 
     renderer.setStrings(strings); 

     cmb.setRenderer(renderer); 

     frame.add(cmb); 
     frame.pack(); 
     frame.setVisible(true); 
    } 
} 

ComboBoxRenderer.java

import java.awt.Color; 
import java.awt.Component; 

import javax.swing.JComboBox; 
import javax.swing.JLabel; 
import javax.swing.JList; 
import javax.swing.JPanel; 
import javax.swing.ListCellRenderer; 


public final class ComboBoxRenderer extends JPanel implements ListCellRenderer 
{ 

    private static final long serialVersionUID = -1L; 
    private Color[] colors; 
    private String[] strings; 

    JPanel textPanel; 
    JLabel text; 

    public ComboBoxRenderer(JComboBox combo) { 

     textPanel = new JPanel(); 
     textPanel.add(this); 
     text = new JLabel(); 
     text.setOpaque(true); 
     text.setFont(combo.getFont()); 
     textPanel.add(text); 
    } 

    public void setColors(Color[] col) 
    { 
     colors = col; 
    } 

    public void setStrings(String[] str) 
    { 
     strings = str; 
    } 

    public Color[] getColors() 
    { 
     return colors; 
    } 

    public String[] getStrings() 
    { 
     return strings; 
    } 

    @Override 
    public Component getListCellRendererComponent(JList list, Object value, 
      int index, boolean isSelected, boolean cellHasFocus) { 

     if (isSelected) 
     { 
      setBackground(list.getSelectionBackground()); 
     } 
     else 
     { 
     } 

     if (colors.length != strings.length) 
     { 
      System.out.println("colors.length does not equal strings.length"); 
      return this; 
     } 
     else if (colors == null) 
     { 
      System.out.println("use setColors first."); 
      return this; 
     } 
     else if (strings == null) 
     { 
      System.out.println("use setStrings first."); 
      return this; 
     } 

     text.setText(strings[index]); 
     text.setForeground(colors[index]); 
     text.setBackground(getBackground()); 
     return text; 


    } 

} 

Cảm ơn!

+0

này mảng của Màu sắc có thể là nghiêm ngặt cho màu sắc an toàn, nghiêm ngặt đối với bất cứ màu sắc hoặc kết hợp với JColorChooser ??? – mKorbel

+0

Mảng màu sẽ có thể nhập bất kỳ màu nào bao gồm (màu mới (#, #, #)) mặc dù tôi vừa mới sử dụng các màu an toàn (Color.BLUE, vv) cho mục đích thử nghiệm. Ngoài ra, tôi không sử dụng JColorChooser cả. –

Trả lời

12

Đây có phải là ý của bạn không?

TestComboColor

import java.awt.Color; 
import java.awt.Component; 
import javax.swing.*; 

public class TestComboColor { 

    static Color[] colors = {Color.BLUE, Color.GRAY, Color.RED}; 
    static String[] strings = {"Test1", "Test2", "Test3"}; 

    public static void main(String[] args) 
    { 
     JFrame frame = new JFrame("JAVA"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JComboBox cmb = new JComboBox(strings); 
     ComboBoxRenderer renderer = new ComboBoxRenderer(cmb); 

     renderer.setColors(colors); 
     renderer.setStrings(strings); 

     cmb.setRenderer(renderer); 

     frame.add(cmb); 
     frame.pack(); 
     frame.setVisible(true); 
    } 
} 

class ComboBoxRenderer extends JPanel implements ListCellRenderer 
{ 

    private static final long serialVersionUID = -1L; 
    private Color[] colors; 
    private String[] strings; 

    JPanel textPanel; 
    JLabel text; 

    public ComboBoxRenderer(JComboBox combo) { 

     textPanel = new JPanel(); 
     textPanel.add(this); 
     text = new JLabel(); 
     text.setOpaque(true); 
     text.setFont(combo.getFont()); 
     textPanel.add(text); 
    } 

    public void setColors(Color[] col) 
    { 
     colors = col; 
    } 

    public void setStrings(String[] str) 
    { 
     strings = str; 
    } 

    public Color[] getColors() 
    { 
     return colors; 
    } 

    public String[] getStrings() 
    { 
     return strings; 
    } 

    @Override 
    public Component getListCellRendererComponent(JList list, Object value, 
      int index, boolean isSelected, boolean cellHasFocus) { 

     if (isSelected) 
     { 
      setBackground(list.getSelectionBackground()); 
     } 
     else 
     { 
      setBackground(Color.WHITE); 
     } 

     if (colors.length != strings.length) 
     { 
      System.out.println("colors.length does not equal strings.length"); 
      return this; 
     } 
     else if (colors == null) 
     { 
      System.out.println("use setColors first."); 
      return this; 
     } 
     else if (strings == null) 
     { 
      System.out.println("use setStrings first."); 
      return this; 
     } 

     text.setBackground(getBackground()); 

     text.setText(value.toString()); 
     if (index>-1) { 
      text.setForeground(colors[index]); 
     } 
     return text; 
    } 
} 
+0

Có! Hoàn hảo, cảm ơn bạn! –

+0

Bạn được chào đón. :) –

+1

+1 [Tôi có nghĩa là, sửa đổi chút ít cần thiết] (http://stackoverflow.com/a/10723340/714968) – mKorbel