Mark / Unmark a line in JTable by changing its color

0

Good morning everyone!

I'm new here in the forum, I apologize if I opened this topic in the wrong place. Hi, I'm trying to create a Java Swing with the JTable component. I've tried a few things, and nothing that gives me a light!

I would like it to work as follows, I have a JTable with multiple results and when the user clicks on a line this line should change color indicating that it is selected (at this point I fill in an object with a value, and from there, no other line can be selected while this selected line is not "deselected"), and when clicked again and deselected, it returns to the default color. (The object that locks the other lines, returns to be null at the moment, allowing the other line to be selected.)

Well the code I managed to do until then was this:

public void mouseClicked(MouseEvent e) {
int l = table.convertRowIndexToModel(table.getSelectedRow());
int c = table.convertColumnIndexToModel(table.getSelectedColumn());

                System.out.println("ROW: " + l + " | COLUMN: " + c);

                Component com = table.prepareRenderer(table.getCellRenderer(l, c), l, c);

                System.out.println("COMP: " + ((com.getBackground() == Color.RED) ? "É VERMELHO" : "NÃO É"));

                if (((com.getBackground() == Color.RED) && (services.getMarkFocus() != null))) {
                    System.out.println("PRETO");
                    com.setBackground(Color.BLACK);
                    services.setMarkFocus(null);
                } else if (((com.getBackground() != Color.RED) && (services.getMarkFocus() == null))) {
                    System.out.println("VERMELHO");
                    com.setBackground(Color.RED);
                    PlaceMark p = new PlaceMark();
                    p.setId(1);
                    services.setMarkFocus(p);
                }

                System.out.println("STATUS MARK: " + ((services.getMarkFocus() != null) ? true : false) + 
                        "\nVERMELHO? " + ((com.getBackground() == Color.RED) ? true : false));
                table.clearSelection();
            super.mouseClicked(e);
        }

This code even changes the color of the line, but the second time it pulls the line "it seems" that it picks up the color of the line and says it is not red, and in the first click it has turned red.

NOTE: I am using an AbstractTableModel, which I did some issues to put image into the table.

From now on, I hope that someone can give me a light.

A hug to everyone ...

SuperMock

    
asked by anonymous 12.01.2016 / 13:11

1 answer

1

Hello,

When the line is selected (it is in focus), by default Java Swing changes the color to blue. One option for you would be to clear the selection from the table before getting the component.

public void mouseClicked(MouseEvent e) {
    int l = table.convertRowIndexToModel(table.getSelectedRow());
    int c = table.convertColumnIndexToModel(table.getSelectedColumn());

    //limpe a seleção antes de obter o component
    table.clearSelection();

    System.out.println("ROW: " + l + " | COLUMN: " + c);

    Component com = table.prepareRenderer(table.getCellRenderer(l, c), l, c);

    System.out.println("COMP: " + ((com.getBackground() == Color.RED) ? "É VERMELHO" : "NÃO É"));

    if (((com.getBackground() == Color.RED) && (services.getMarkFocus() != null))) {
        System.out.println("PRETO");
        com.setBackground(Color.BLACK);
        services.setMarkFocus(null);
    } else if (((com.getBackground() != Color.RED) && (services.getMarkFocus() == null))) {
        System.out.println("VERMELHO");
        com.setBackground(Color.RED);
        PlaceMark p = new PlaceMark();
        p.setId(1);
        services.setMarkFocus(p);
    }

    System.out.println("STATUS MARK: " + ((services.getMarkFocus() != null) ? true : false)
            + "\nVERMELHO? " + ((com.getBackground() == Color.RED) ? true : false));

    super.mouseClicked(e);
}
    
12.01.2016 / 17:33