Remove selection color from Jtable’s line?

Asked

Viewed 503 times

2

It may sound silly, but I can’t find the method to remove the color from the selection when the mouse clicks on the line. I have a table with red and blue lines, blue colors are lines that mean an action and red other actions. However, when I select the line, the color is superimposed by the opaque blue of Jtable’s standard selection, of course when clicking another line the defined color of the clicked line goes back to normal. Anyway, my question is, how to remove this opaque blue pattern from Jtable that signals line selection?

Below is my code, all conditions work except "isSelect", where should leave the line with the color that already exists, and do 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);
}

}

  • In the redenderer execute setOpaque(true). Also look at the method setSelectionBackground.

  • @Qmechanic73 hasn’t solved my problem yet, I edited the question and put my code.

1 answer

2


You can create a redenderer customized to 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 redenderer 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 on http://docs.oracle.com/javase/tutorial/uiswing/components/table.html

  • The line that is selected is already with the background changed, in fact what I want is not to demonstrate when a line is selected. Because each row has a checkbox in the next column. That is, if we were to think about the possibility of using the "isSelect" it should be transparent.

  • I forgot to comment, I’m already using a Renderer to set the background colors of the lines.

  • The previous code did not work because the superclass method overwritten the modifications. Already fixed. To avoid noticing that there is a line selected, simply put the background color as normal, in your case, blue or red.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.