简体   繁体   中英

Refresh JTable after look and feel update

I have a JTable with custom TableCellRenderer.

public class DateCellRenderer extends DefaultTableCellRenderer {

    private static final long serialVersionUID = 58L;

    public DateCellRenderer() {
        super();
        setHorizontalAlignment(CENTER);     
        setOpaque(true);
    }
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {         
        if (value instanceof Date) {
            String date = new SimpleDateFormat("dd-MM-yyyy").format((Date) value);
            setText(date);
        }
        return this;
    }
}

Also in my application I have a drop down menu by which I can change the look and feel. This drop down menu is in a parent frame and the table is in a dialog. When the dialog is opened the parent frame is inaccessible. So to change the look and feel I have to close the dialog first.

Now in a particular skin if the table is populated by some data and I change the look and feel from parent frame and again open the dialog then the column, where I have added the TableCellRenderer, is keeping the old look and feel. It is not updating while the other columns render themselves in the new look and feel.

I am unable to find the problem and its solution. Any help is appreciable.

Note: The look and feel update of the application is made by the following snippet

javax.swing.UIManager.setLookAndFeel(uiProperties.getThemeModel().getThemeClass());
ComponentFactory.getLibraryFrame().getRootPane().updateUI();
for (int i = 0; i < Frame.getWindows().length; i++) {
    SwingUtilities.updateComponentTreeUI(Frame.getWindows()[i]);
}
for (int i = 0; i < Frame.getFrames().length; i++) {
    SwingUtilities.updateComponentTreeUI(Frame.getFrames()[i]);
}

Thanks in advance.

In HiFi theme chosen first: 在此输入图像描述

Then I change the theme to Fast, and the second column "Released" not updated its ui: 在此输入图像描述

The JTable is:

public class MovieSearchResultTable extends BaseTable {

    private static final long serialVersionUID = 45L;

    public MovieSearchResultTable(TableModel tableModel) {
        super(tableModel);
        LibraryLogger.initMessage(getClass().getSimpleName());
    }

    @Override
    public void initialize() {
        setFillsViewportHeight(true);
        setAutoResizeMode(AUTO_RESIZE_OFF);
        getColumnModel().getColumn(1).setCellRenderer(new DateCellRenderer());//if I comment out this line then no problem. but without CellRenderer how could I format a Date, if I use formatted String instead of Date, then the column will not sort!!
    }

    @Override
    public boolean getScrollableTracksViewportWidth() {
        return getPreferredSize().getWidth() < getParent().getWidth();
    }
}

I think that not good L&F, JTable looks like ... ok, but other Compound JComponents aren't ...., not sure I haven't wasting my time, I leaving to test that, maybe is there something described about that on their Forum or Documentation or BugParades, but nothing from your question

there is very simple way and you can any time to check that

1) go to Insubstantial

2) download code source,

3) import all classes to the IDE (2-15 min depends of PC HardWare)

4) search for folder test, there is Check.java,

5) run that and to try everything in JMenu Look and Feel, before that required to download API's for every Custom Java Swing Look and Feels

在此输入图像描述

The solution, you need to override public Component prepareRenderer(TableCellRenderer renderer, int row, int column)

Here is the class:

public class MovieSearchResultTable extends BaseTable {

    private static final long serialVersionUID = 45L;

    private int rolloverRowIndex = -1;

    public MovieSearchResultTable(TableModel tableModel) {
        super(tableModel);
        LibraryLogger.initMessage(getClass().getSimpleName());
    }   

    public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
        Component component = super.prepareRenderer(renderer, row, column);
        Color foreground = getForeground();
        Color background = getBackground();
        if (isRowSelected(row)) {
            foreground = getSelectionForeground();
            background = getSelectionBackground();
        }
        else if (row == rolloverRowIndex) {
            foreground = getSelectionForeground();
            background = ColorHelper.brighter(getSelectionBackground(), 40);
        }
        else if (row % 2 == 0) {
            background = ColorHelper.brighter(getParent().getBackground(), 20);
        }
        component.setForeground(foreground);
        component.setBackground(background);
        return component;
    }

    private class RolloverListener extends MouseInputAdapter {

        public void mouseExited(MouseEvent e) {
            rolloverRowIndex = -1;
            repaint();
        }

        public void mouseMoved(MouseEvent e) {
            int row = rowAtPoint(e.getPoint());
            if (row != rolloverRowIndex) {
                rolloverRowIndex = row;
                repaint();
            }
        }
    }

    @Override
    public void initialize() {
        setFillsViewportHeight(true);
        setAutoResizeMode(AUTO_RESIZE_OFF);
        TableColumnModel tableColumnModel = getColumnModel();
        for(ComponentConstant.ColumnName columnName : ComponentConstant.Column.MOVIE_SEARCH_RESULT_TABLE) {
            int order = columnName.getOrder();
            TableColumn tableColumn = tableColumnModel.getColumn(order); 
            if(order == 0) {
                continue;
            }

            tableColumn.setCellRenderer(RendererFactory.getMovieSearchResultTableCellRenderer());
        }
        RolloverListener listener = new RolloverListener();
        addMouseMotionListener(listener);
        addMouseListener(listener);
    }

    @Override
    public boolean getScrollableTracksViewportWidth() {
        return getPreferredSize().getWidth() < getParent().getWidth();
    }
}

Thanks.

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