Java – multiple colors for each item in jcombobox
•
Java
I tried to make one Combo@R_514_2419 @Use different colors for different items I wrote some test code, but it didn't seem to work Adding to the renderer causes the program to crash, but annotating it causes the box to appear in the box
Is there anything I'm missing or I did wrong? I tried using the custom combobox renderer tutorial as an example
This is my code:
TestComboColor. java
import java.awt.Color; import javax.swing.JCombo@R_514_2419@; 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); JCombo@R_514_2419@ cmb = new JCombo@R_514_2419@(); Combo@R_514_2419@Renderer renderer = new Combo@R_514_2419@Renderer(cmb); renderer.setColors(colors); renderer.setStrings(strings); cmb.setRenderer(renderer); frame.add(cmb); frame.pack(); frame.setVisible(true); } }
Combo@R_514_2419 @Renderer. java
import java.awt.Color; import java.awt.Component; import javax.swing.JCombo@R_514_2419@; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.ListCellRenderer; public final class Combo@R_514_2419@Renderer extends JPanel implements ListCellRenderer { private static final long serialVersionUID = -1L; private Color[] colors; private String[] strings; JPanel textPanel; JLabel text; public Combo@R_514_2419@Renderer(JCombo@R_514_2419@ 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; } }
thank you!
Solution
Is that what you mean?
import java.awt.Color; import java.awt.Component; import javax.swing.*; public class TestComboColor { static Color[] colors = {Color.BLUE,"Test3"}; public static void main(String[] args) { JFrame frame = new JFrame("JAVA"); frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); JCombo@R_514_2419@ cmb = new JCombo@R_514_2419@(strings); Combo@R_514_2419@Renderer renderer = new Combo@R_514_2419@Renderer(cmb); renderer.setColors(colors); renderer.setStrings(strings); cmb.setRenderer(renderer); frame.add(cmb); frame.pack(); frame.setVisible(true); } } class Combo@R_514_2419@Renderer extends JPanel implements ListCellRenderer { private static final long serialVersionUID = -1L; private Color[] colors; private String[] strings; JPanel textPanel; JLabel text; public Combo@R_514_2419@Renderer(JCombo@R_514_2419@ 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,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; } }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码