Mark/Deselect a line in Jtable, changing its color

Asked

Viewed 614 times

0

Good morning, everyone!

I’m new to the forum, and I apologize if I opened this topic in the wrong place. Well I have a huge problem with an action that I’m trying to do in Java Swing with the Jtable component, I’ve looked in several places and nothing that gives me a light!

What I would like it to work is the following, I have a Jtable with multiple results and when the user clicks on a row this row should change color indicating that it is selected (At this point I fill an object with a value, and from there, no other lines can be selected as long as this selected line is not "deselected"), and when it is re-clicked and deselected, it returns the default color. (The object that hangs the other lines is null again at this time, allowing the other line to be selected).

Well the code I was able 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 "looks" it takes the color of the line and says it is not red, being that at the first click it became red.

NOTE: I am using an Abstracttablemodel, which I made some edits to put image in the table.

Already I am in the waiting, and I hope that someone can give me a light.

Hugging...

Supermock

  • Welcome just a clarification, the OS not exactly a forum and yes a question and answer site, so don’t worry about posting on the "specific place". http://answall.com/tour

  • This question of how to color specific lines of a Jtable, does this change when the Jtable is being constructed correctly? Because what I would like to happen is that this change is at execution time.

  • I believe so, but I thought it might be relevant. I didn’t have time to try to see your specific case.

1 answer

1

Hello,

When the line is selected (it is in focus), by default Java Swing changes the color to blue. An option for you would be to clear the selection of 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);
}

Browser other questions tagged

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