简体   繁体   中英

Why my component column do not highlight?

我的框架

As you can see from the image I have my jtable in the frame with two column name and icon. The icon column doesn't highlight. Why? This is my renderer. My icon is a JComponent Object with a green rectangle

public class myRenderer extends JPanel implements TableCellRenderer {
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) 
    {
        if (hasFocus)
            setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
        else
            setBorder(null);

        return (Component) value;
    }
};

Documentation of TableCellRenderer :

hasFocus - if true , render cell appropriately. For example, put a special border on the cell, if the cell can be edited, render in the color used to indicate editing

probably the icon column is not editable and/or the cell does not have the focus.

(use debugger or println to see if hasFocus is true)

My icon is a JComponent Object with a green rectangle

A TableModel should NOT store a component as data. A model should store data and then renderer the data.

If you want to display a Rectangle, then add a custom Icon to the model. For an example of a custom icon see: https://stackoverflow.com/a/32700526/131872

Then in your JTable or TableModel you override the getColumnClass(...) method to return Icon.class for the second column and the table will use the default Icon renderer. See: https://stackoverflow.com/a/5615516/131872

If you really need a custom renderer then you should extend from the default renderer since it will automatically provide support for highlighting and focus on rows and cells.

If you implement TableCellRenderer then you are responsible for highlighting and focus painting in the renderer based of the parameters passed to the method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM