Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make inner classes protected... #113

Closed
mlaggner opened this issue Jun 11, 2020 · 12 comments
Closed

Make inner classes protected... #113

mlaggner opened this issue Jun 11, 2020 · 12 comments
Milestone

Comments

@mlaggner
Copy link

... to avoid copying the whole content of the inner class when creating subclasses

e.g. where I found an example https://github.com/JFormDesigner/FlatLaf/blob/master/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTableHeaderUI.java#L255

@mlaggner
Copy link
Author

another similar example would be private helper methods like FlatComboBoxUI.getDisabledBackground() which I had to re-implement in the subclass just to adopt the paint method a bit

@DevCharly
Copy link
Collaborator

I'm willing to open FlatLaf as much as possible, but I'm not sure whether opening FlatTableCellHeaderRenderer is a good idea. This class is not a general header renderer. It is only used when sort icon position is not on right side. And it always delegates to the default header renderer.

What's your use case for subclassing FlatTableCellHeaderRenderer ?

@mlaggner
Copy link
Author

the problem here was that I use glazedlists for my tables and glazedlists needs a component which implements the interface SortableRenderer as header renderer to inject the sort icon into the header. So my idea was to

private static class MyHeaderRenderer extends FlatTableCellHeaderRenderer  implements SortableRenderer {
...
}

to achieve this without the need to copy the whole inner class.

Creating sub-themes on your theme would be much easier if we could use as much code as possible (protected). Just my personal opinion

@DevCharly
Copy link
Collaborator

Implemented a little app to test Glazed Lists with FlatLaf (in commit 212c553) and the table header sort icons work out of the box (without a custom header renderer and without FlatTableCellHeaderRenderer involved):

image

Top/bottom/left positioned sort arrows did not work, but is fixed in 211030b (enable with UIManager.put( "TableHeader.sortIconPosition", "top" );. This commit also changes how class FlatTableCellHeaderRenderer is internally used. Default renderer of JTableHeader is no longer changed permanently. Only temporary while painting.

image

Little "problem" is that this uses non-HiDPI icons from Glazed Lists. It is possible to replace them with TableComparatorChooser.setIconPath( "path" ) (see

TableComparatorChooser.setIconPath( "resources/windowsxp" );
), but this supports only PNGs.

Is your plan/idea to use SortableRenderer to replace glazedlists icons with FlatLaf icons?

This is probably difficult because the only method of the interface is void setSortIcon(Icon sortIcon) and does not have any information about the column or sort direction.

Would be easy if Glazed Lists would provide a TableComparatorChooser.setIcons(Icon[] icons) method. As workaround you could use reflection to set field TableComparatorChooser.icons.

BTW the recommended way to implement custom header renderer, which works with all look and feels, is:

private static class MyHeaderRenderer implements TableCellRenderer {
    private final TableCellRenderer delegate;

    MyHeaderRenderer( TableCellRenderer delegate ) {
        this.delegate = delegate;
    }

    @Override
    public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column ) {
        Component c = delegate.getTableCellRendererComponent( table, value, isSelected, hasFocus, row, column );
        // TODO do some modifications
        return c;
    }
}

// install own header renderer
JTableHeader header = itemsTable.getTableHeader();
header.setDefaultRenderer( new MyHeaderRenderer( header.getDefaultRenderer() ) );

Still don't think that it is necessary to copy code from FlatTableCellHeaderRenderer or subclass it, but I'm open for arguments...

Regarding other private methods/fields/classes: I plan to review them soon and will make them protected where it may be useful...

@DevCharly DevCharly added this to the 0.37 milestone Jun 16, 2020
@mlaggner
Copy link
Author

wow, even more that I could imagine! thanks!

@mlaggner
Copy link
Author

Is your plan/idea to use SortableRenderer to replace glazedlists icons with FlatLaf icons?

yes this would be the next step.

initially I created a header renderer which is able to display an icon instead of text AND the sort icon (so I need basically 2 JLabels). Something like
image

DevCharly added a commit that referenced this issue Jun 20, 2020
- reviewed all private methods and made them protected/public where it might be useful for subclasses
- ComboBox and Spinner: added protected getBackground() and getForeground() methods to allow subclasses to change colors
- TabbedPane: moved tab separator painting to own method

(issue #113)
@DevCharly
Copy link
Collaborator

@mlaggner wrote:

another similar example would be private helper methods like FlatComboBoxUI.getDisabledBackground() which I had to re-implement in the subclass just to adopt the paint method a bit

I've reviewed all private methods and made them protected/public where it might be useful for subclasses in commit ea2412d. Also made some changes to FlatComboBoxUI and FlatSpinnerUI, which now have protected getBackground(boolean enabled) and getForeground(boolean enabled) methods.

@mlaggner
Copy link
Author

thanks, will review my "patches" as soon as 0.37 is released.
And with your help about Glazedlists I was able to reimplement a TableComparatorChooser (mostly copied code, because I avoid using dirty reflection) with SVG sort icons

@nseven404
Copy link

image
image
Not working! :( Help me!

@DevCharly
Copy link
Collaborator

Did you enable table sorting?
E.g. invoking JTable.setAutoCreateRowSorter(true)?
This is the code used in the FlatLaf Demo (on "Data components" tab; click on column header to sort column):

@nseven404
Copy link

Thanks you very much! I want to add an icon down on the header table bar, what should I do?

@DevCharly
Copy link
Collaborator

Here is a great article that gives many details about how to sort JTable:
https://www.codejava.net/java-se/swing/6-techniques-for-sorting-jtable-you-should-know

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants