0

I've encountered a problem in coding my JComboBox. I am trying to change the color of some of the combo box's items. The combo box if connected to a file and by choosing one of its items, it fetches a row in that file. I want the color of the item be different if the row contains zero.

The problem is I cannot get if done with ComboBoxRenderer nor CellRenderer. Here is my code:

JComboBox combo = new JComboBox(industryName);
   combo.setSelectedItem(null);

    combo.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e){

            String selectedString = (String) combo.getSelectedItem();

            try {  
                CSVReader reader = new CSVReader(new FileReader(myFile));
                String[] nextLine;

                while ((nextLine = reader.readNext()) != null) {

                    if(nextLine[1].equals(industry) ){
                        x= nextLine; 
                        x2 = Arrays.toString(nextLine);

                        break;
                    }
                }
                reader.close();
            } catch (FileNotFoundException e1) {
                System.out.println("File not fount");
                e1.printStackTrace();
            } catch (IOException e3) {
                e3.printStackTrace();
            }
            catch (NullPointerException e2){
                JOptionPane.showMessageDialog(frame.getComponent(0), "Please choose ...");
            }
        }
    });

    ComboBoxRenderer renderer = new ComboBoxRenderer(comboBoxIndustry, x2);
    comboBoxIndustry.setRenderer(renderer);

class ComboBoxRenderer extends JPanel implements ListCellRenderer{

private static final long serialVersionUID = -1L;
private String[] x2;
private String[] strings;
JLabel text;

public ComboBoxRenderer(JComboBox combo, String[] x2) {

    this.x2 = x2;
    text = new JLabel();
    text.setOpaque(true);
    text.setFont(combo.getFont());
}

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

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

    text.setText(value.toString());
    for(int i = 0; i <x2.length; i++){
         String[] disable = x2[0].split(",");
         if(Double.parseDouble((String) disable[2]) == 0.000000){
             //combo.getEditor().getEditorComponent().setBackground(Color.YELLOW);
             text.setForeground(Color.YELLOW);
       // ((JTextField) comboBoxIndustry.getEditor().getEditorComponent()).setBackground(Color.YELLOW);
    }
    }

    return text;
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user2843669
  • 123
  • 1
  • 1
  • 9
  • 1
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). Hard code some data to replace the file I/O & CSV reader. 2) *"..if the row contains zero."* Zero what? Widgets, length, bananas..? – Andrew Thompson Oct 10 '16 at 17:04
  • Thank you for the edit Andrew, I am fairly new to asking questions here.. – user2843669 Oct 10 '16 at 17:06
  • 1
    That is not an MCVE. We can't compile or execute the code. When you post a proper MCVE, the file is irrelevant to the question. You can hardcode items in the combo box to simulate the data contained in the file. So the MCVE will just be a frame with a combo box with hard coded data and a custom renderer. – camickr Oct 10 '16 at 18:01
  • 1
    See also [*Java: Double Value Comparison*](http://stackoverflow.com/q/434657/230513). – trashgod Oct 10 '16 at 18:05

0 Answers0