我有一个带有自定义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;
}
}
同样在我的应用程序中,我有一个下拉菜单,通过它可以更改外观和感觉。此下拉菜单位于父框架中,表格位于对话框中。打开对话框时,父框架不可访问。因此,要更改外观,我必须先关闭对话框。
现在在特定皮肤中,如果表格由某些数据填充,我从父框架更改外观并再次打开对话框,那么我添加了TableCellRenderer的列保持旧的外观。当其他列以新的外观呈现时,它不会更新。
我无法找到问题及其解决方案。任何帮助都很明显。
注意:应用程序的外观更新由以下代码段
完成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]);
}
提前致谢。
首先选择HiFi主题:
然后我将主题更改为Fast,第二列“Released”未更新其ui:
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();
}
}
答案 0 :(得分:3)
我认为不好L&amp; F,JTable看起来......好吧,但是其他复合JComponents不是......,不确定我没有浪费时间,我离开去测试,也许是在他们的论坛或文档或BugParades中描述了一些内容,但没有提到您的问题
有非常简单的方法,你可以随时检查
1)转到Insubstantial
2)下载代码源,
3)将所有类导入IDE(2-15分钟取决于PC硬件)
4)搜索文件夹测试,有Check.java,
5)运行该操作并尝试JMenu外观中的所有内容,然后才需要为每Custom Java Swing Look and Feels
下载API
答案 1 :(得分:2)
解决方案,您需要覆盖public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
这是班级:
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();
}
}
感谢。