Choose a color with JComboBox Java Swing

I have a JComboBox where I want the user to select a color. JComboBox only displays colors without text. I came up with this solution. Please tell me if this is good, or this should be avoided and why. I am new to Swing and Java in general, so please be patient :)

public class ToolBar{ private MainFrame mainFrame; public ToolBar (MainFrame mainFrame) { this.mainFrame = mainFrame; } public JPanel getToolBar(){ JPanel toolbarPanel = new JPanel(new FlowLayout(FlowLayout.LEADING,2,2)); toolbarPanel.setPreferredSize(new Dimension(mainFrame.getScreenWidth(),60)); toolbarPanel.setBorder(BorderFactory.createLineBorder(Color.gray)); JButton fillButton = new JButton("Fill: "); fillButton.setPreferredSize(new Dimension(60,20)); //fillButton.setBackground(Color.red); toolbarPanel.add(fillButton); String[] test = {" ", " " , " " , " " , " " , " "}; JComboBox colorBox = new JComboBox(test); colorBox.setMaximumRowCount(5); colorBox.setPreferredSize(new Dimension(50,20)); colorBox.setRenderer(new MyCellRenderer()); toolbarPanel.add(colorBox); return toolbarPanel; } class MyCellRenderer extends JLabel implements ListCellRenderer { public MyCellRenderer() { setOpaque(true); } public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { setText(value.toString()); switch (index) { case 0: setBackground(Color.white); break; case 1: setBackground(Color.red); break; case 2: setBackground(Color.blue); break; case 3: setBackground(Color.yellow); break; case 4: setBackground(Color.green); break; case 5: setBackground(Color.gray); break; } return this; } } } 

This is working fine. It displays empty selection items in a JComboBox with different colors. The problem is that when the user selects a color, the highlight color in the JComboBox does not change. What lines of code should be added and where, when the user selects a color from the list that appears in the JComboBox field?

I tried some solutions, but the result was that when the user selects a color choice in JComboBox, it always changes to gray ...

I looked through a few similar questions, but I just can't figure out what part of the code is dealing with the JComboBox color change when the choice is made ...

+4
source share
3 answers

Try this should work. You must override setBackground ... because the internal mechanism uses the default colors from the current Look & Feel:

 Color[] colors={Color.white,Color.red,Color.blue,Color.green}; JComboBox colorBox = new JComboBox(colors); colorBox.setMaximumRowCount(5); colorBox.setPreferredSize(new Dimension(50,20)); colorBox.setRenderer(new MyCellRenderer()); 

And ListCellRender:

 class MyCellRenderer extends JButton implements ListCellRenderer { public MyCellRenderer() { setOpaque(true); } boolean b=false; @Override public void setBackground(Color bg) { // TODO Auto-generated method stub if(!b) { return; } super.setBackground(bg); } public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { b=true; setText(" "); setBackground((Color)value); b=false; return this; } } 
+3
source

ComboBox uses equals and all your lines are equal. Defining color names

 String[] test = {"red", "green" , "blue" ...}; 

But in the render call setText(" ");

+2
source

Add a case for the index == -1 and set the background color of the cell renderer for the last user selection.

0
source

Source: https://habr.com/ru/post/1502420/


All Articles