mgarin / weblaf

WebLaF is a fully open-source Look & Feel and component library written in pure Java for cross-platform desktop Swing applications.
http://weblookandfeel.com
GNU General Public License v3.0
1.15k stars 235 forks source link

JTable fixed rowHeight not good (on Linux) #559

Open Sciss opened 5 years ago

Sciss commented 5 years ago

I'm still not sure you need to explicitly set a row-height, I think other look-and-feels do not need that either. Yes, default sets 16, but rowHeightSet is still false.

The result is that on Linux the default font is relatively large and JTable looks bad:

Screenshot from 2019-10-13 22-14-49

whereas WebTable looks good:

Screenshot from 2019-10-13 22-14-58

I think JTable should not require per-platform row-height tweaking, but should just take the actual font size into account.

mgarin commented 5 years ago

WebTable example looks good because it uses the feature I've mentioned on our chat earlier:

        @NotNull
        @Override
        protected List<? extends JComponent> createPreviewElements ()
        {
            final WebTable table = new WebTable ( getStyleId (), SampleData.createShortTableModel ( false ) );
            table.optimizeColumnWidths ( true );
            table.setOptimizeRowHeight ( true );
            return CollectionUtils.asList ( table );
        }

The row height optimization one.

Although you've made a good point that default row height might not have effect until set explicitely - i'll double check that tomorrow and see if it's true for all or at least some platforms/L&Fs and whether something can be improved or not.

mgarin commented 5 years ago

I have double-checked - normal JTable with no settings doesn't scale row height at all, so whether it is specified in style or not doesn't really affect the outcome.

Here is a small example I tried on Windows (with Metal L&F):

    public static void main ( final String[] args )
    {
        SwingTest.run ( MetalLookAndFeel.class.getCanonicalName (), new Runnable ()
        {
            @Override
            public void run ()
            {
                final JTable table = new JTable (
                        new Object[][]{
                                { "Col 1", "Col 2", true },
                                { "Col 1", "Col 2", false }
                        },
                        new Object[]{ "Col 1", "Col 2", "Col 3" }
                );

                TestFrame.show ( new JScrollPane ( table ) );
            }
        } );
    }

Result is a simple metal-styled table: image

Now let's say table has large font:

    public static void main ( final String[] args )
    {
        SwingTest.run ( MetalLookAndFeel.class.getCanonicalName (), new Runnable ()
        {
            @Override
            public void run ()
            {
                final JTable table = new JTable (
                        new Object[][]{
                                { "Col 1", "Col 2", true },
                                { "Col 1", "Col 2", false }
                        },
                        new Object[]{ "Col 1", "Col 2", "Col 3" }
                );
                table.setFont ( table.getFont ().deriveFont ( 40f ) );

                TestFrame.show ( new JScrollPane ( table ) );
            }
        } );
    }

Result is: image

The same happens even if I make native font large, it doesn't really matter - default JTable never scales row height with it's content. And table only allows positive row height, so there is no hack for -1 height or anything like that like in other components:

    public void setRowHeight(int rowHeight) {
        if (rowHeight <= 0) {
            throw new IllegalArgumentException("New row height less than 1");
        }
    ...
    }
Sciss commented 5 years ago

ok, thanks for checking

mgarin commented 5 years ago

The whole reason for adding TableRowHeightOptimizer utility was inability of JTable to scale with renderer size correctly. It's not a perfect solution, but unfortunately I didn't find a better way to do it yet.

mgarin commented 5 years ago

I'll actually change this to enhancement and leave it be for possible future improvements since there already are examples & screenshots here.