简体   繁体   English

在外观更新后刷新JTable

[英]Refresh JTable after look and feel update

I have a JTable with custom TableCellRenderer. 我有一个带有自定义TableCellRenderer的JTable。

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. 现在在特定的皮肤中,如果表格由某些数据填充,我从父框架更改外观并再次打开对话框,那么我添加了TableCellRenderer的列保持旧的外观。 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: 首先选择HiFi主题: 在此输入图像描述

Then I change the theme to Fast, and the second column "Released" not updated its ui: 然后我将主题更改为Fast,第二列“Released”未更新其ui: 在此输入图像描述

The JTable is: JTable是:

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 我认为不好L&F,JTable看起来......好吧,但是其他复合JComponents不是......,不确定我没有浪费时间,我离开去测试那个,也许有一些描述的东西在他们的论坛或文档或BugParades上,但你的问题没有任何内容

there is very simple way and you can any time to check that 有非常简单的方法,你可以随时检查

1) go to Insubstantial 1)去Insubstantial

2) download code source, 2)下载代码源,

3) import all classes to the IDE (2-15 min depends of PC HardWare) 3)将所有类导入IDE(2-15分钟取决于PC硬件)

4) search for folder test, there is Check.java, 4)搜索文件夹测试,有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 5)运行它并尝试JMenu外观中的所有内容,之后需要为每个自定义Java Swing外观和感觉下载API

在此输入图像描述

The solution, you need to override public Component prepareRenderer(TableCellRenderer renderer, int row, int column) 解决方案,您需要覆盖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. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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