Remove selection color from the JTable line?

2

It may sound like bullshit, but I can not seem to find the method to remove the color from the selection when the mouse clicks the line. I have a table with red and blue lines, blue colors are lines that signify an action and red other actions. However when I select the line, the color is superimposed by the default opaque blue of the JTable, of course that when clicking on another line the defined color of the clicked line returns to normal. Anyway, my question is, how to remove this default opaque blue from JTable that signals line selection?

Below is my code, all conditions work except "isSelect", where it should leave the line with the color that already exists, and not overlap with the selection color.

  

Code

package commons;

import java.awt.Color;
import java.awt.Component;
import java.util.List;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

import model.SdkDynRellation;
import view.ViewInit;

import com.sun.tools.xjc.generator.bean.field.IsSetFieldRenderer;


public class ColorCellRenderer extends DefaultTableCellRenderer {


private List<String> list;

public ColorCellRenderer() {
    super();
}

public ColorCellRenderer(List<String> list2) {
    list = list2;
    setOpaque(true);
}

@Override
public Component getTableCellRendererComponent(JTable table, Object value,
        boolean isSelected, boolean hasFocus, int row, int column) {
    final JLabel cellComponent = (JLabel) super
            .getTableCellRendererComponent(table, value, isSelected,
                    hasFocus, row, column);


    Object val = table.getValueAt(row, 0);
    Object check = table.getValueAt(row, 1);


    if (!AuxClass.isBegin) {
        if (list.contains(val.toString())
                && check.toString().equals("true")) {
            cellComponent.setForeground(Color.WHITE);
            cellComponent.setBackground(Color.BLUE);

        } else {
            if (check.toString().equals("true") && new SdkDynRellation().getIndexes().contains(val))
            {
                cellComponent.setBackground(Color.RED);
                cellComponent.setForeground(Color.WHITE);
            } 
            else
            {
                if (check.toString().equals("true"))
                {
                    cellComponent.setBackground(Color.BLUE);
                    cellComponent.setForeground(Color.WHITE);
                }
                else 
                {
                    cellComponent.setBackground(Color.WHITE);
                    cellComponent.setForeground(Color.black);
                }

            }


        }


        if (isSelected)
        {
            cellComponent.setBackground(getBackground());
        }
    } else {
        if (list.contains(val.toString())
                && check.toString().equals("true")) {
            cellComponent.setForeground(Color.WHITE);
            cellComponent.setBackground(Color.RED);
        } else {

            cellComponent.setBackground(Color.WHITE);
            cellComponent.setForeground(Color.black);
        }

    }




    return super.getTableCellRendererComponent(table, value, isSelected,
            hasFocus, row, column);
}

}

    
asked by anonymous 19.02.2015 / 14:35

1 answer

2

You can create a customized renderer for the table. Example:

class CustomRenderer extends DefaultTableCellRenderer {
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            if (isSelected) { // Se a célula está selecionada
                c.setForeground(Color.black); // ou outra cor
            } else {
                c.setForeground(Color.gray); // ou outra cor
            }
            return c;
        }
    }

Then just pass the renderer to the table:

CustomRenderer tableRenderer = new CustomRenderer ();
   for (int i = 0; i < tableModel.getColumnCount(); i++) { // Adicionar o renderer para cada coluna
      jTable.setDefaultRenderer(jTable.getColumnClass(i), tableRenderer);
   }

I suggest you read link

    
19.02.2015 / 14:46