Java: How to change JTable drag & drop dropLine color?

ImJustACowLol :

I am currently trying to enable drag & drop in a JTable with a heavily customized UI. When doing drag & drop events of table rows, a line appears at the place where the row will be dropped (this line is drawn by Java). I am currently trying to figure out how I can change the color of this target line / drop line, but I am unsure as of how to do it. Can anyone point me in the right direction of where to look, or how I can do this?

Below is a screenshot of the JTable. For some reason, my cursor wasn't captured in the screenshot, but the cell at which I held my cursor was drawn in the color that I want the line to be in. With the leftmost cell and the rightmost cell, the color for some reason is blue-ish.

enter image description here

ImJustACowLol :

Found the answer... And gosh this reminds me why I think Java Swing is an awfully crappy area of Java.

If you want to change it for every single JTable in your application, you can use UIManager to set the color for the drop line:

UIManager.put("Table.dropLineColor", Color.cyan);
UIManager.put("Table.dropLineShortColor", Color.cyan);

If you want to set it only for one single table, then you have to set a custom UI for your table:

myTable.setUI(new CustomTableUI());

CustomTableUI then ensures that in UIManager, the default color for the dropLine is changed right before the line is painted. Afterwards, the defaults are restored:

private class CustomTableUI extends BasicTableUI {

    @Override
    public void paint(Graphics g, JComponent c) {
        // Store defaults
        Color dropLineColor = UIManager.getColor("Table.dropLineColor");
        Color dropLineShortColor = UIManager.getColor("Table.dropLineShortColor");

        // Set your custom colors here
        UIManager.put("Table.dropLineColor", Color.cyan);
        UIManager.put("Table.dropLineShortColor", Color.cyan);

        // Allow the table to be painted
        super.paint(g, c);

        // Restore the defaults
        UIManager.put("Table.dropLineColor", dropLineColor);
        UIManager.put("Table.dropLineShortColor", dropLineShortColor);
    }

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=165692&siteId=1