3

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: enter image description here

Then I change the theme to Fast, and the second column "Released" not updated its ui: enter image description here

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();
    }
}
Tapas Bose
  • 28,796
  • 74
  • 215
  • 331
  • which from Look and Feel – mKorbel Feb 21 '12 at 17:45
  • I am using jtatoo - http://www.jtattoo.net/ look and feel. It has sub look and feels like HiFi, Fast, Acryl, Aero etc. That dropdown consists these list of themes. – Tapas Bose Feb 21 '12 at 17:54
  • right now is your question clear, but without code generated some issue ..... or pictures is your question pretty un_answerable, did you only to tried change JTatoo○s Themes on fly, nothins else ??? – mKorbel Feb 21 '12 at 17:58

2 Answers2

3

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

enter image description here

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • I have downloaded the Insubstantial-insubstantial-insubstantial_7.0-21-gefb2aaf but couldn't find Check.java. :( – Tapas Bose Feb 21 '12 at 18:32
  • eeeerggth :-) I can see this one on https://github.com/Insubstantial/insubstantial/tree/master/substance/src/test/java/test – mKorbel Feb 21 '12 at 18:36
  • I spent 2 and half weeks for pick up my favorite L&F (original insomnia) – mKorbel Feb 21 '12 at 18:38
  • mKorbel I am afraid that after using Substance look and feel the problem is still persisting. – Tapas Bose Feb 21 '12 at 19:29
  • @Tapas Bose I'm sure that container and procedures for L&F changes are ok, tested nTimes, I using this that for testing of my own classes (contents), this window always tell me truth, there must be another issue, – mKorbel Feb 21 '12 at 19:34
  • +1, will always cherish this link for different Look And Feels :-) – nIcE cOw Feb 22 '12 at 07:15
2

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.

Tapas Bose
  • 28,796
  • 74
  • 215
  • 331
  • I think that too complicated, please excluding prepareRenderer +1, is everything for another questions how JTable, TableModel works – mKorbel Feb 21 '12 at 22:12