Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #38301

Re: How does a scrollbar paint differently?

Newsgroups comp.lang.java.programmer
Date 2018-05-30 10:53 -0700
References (3 earlier) <21dc0f98-f0b2-44fd-967c-e98563fcbb15@googlegroups.com> <194b2ea5-122d-49f6-b284-9f80fcb2ab62@googlegroups.com> <e7be5767-600c-4bc2-af42-93cd681d6abe@googlegroups.com> <pemimn$q30$1@dont-email.me> <afe8fb1f-6821-4abf-81fd-9c903f6fe4a0@googlegroups.com>
Message-ID <6fee2a82-bcd7-4cdc-8ee2-183109738808@googlegroups.com> (permalink)
Subject Re: How does a scrollbar paint differently?
From Eric Douglas <e.d.programmer@gmail.com>

Show all headers | View raw


On Wednesday, May 30, 2018 at 1:32:05 PM UTC-4, Eric Douglas wrote:
> On Wednesday, May 30, 2018 at 12:15:30 PM UTC-4, Daniele Futtorovic wrote:
> Basically, my test class was doing this (with the setLookAndFeel in a custom class which extends JFrame).
> 
> import java.awt.Dimension;
> import java.awt.EventQueue;
> 
> import javax.swing.DefaultListModel;
> import javax.swing.JFrame;
> import javax.swing.JList;
> import javax.swing.JScrollPane;
> import javax.swing.ScrollPaneConstants;
> import javax.swing.UIManager;
> import javax.swing.UnsupportedLookAndFeelException;
> 
> public class TestListScroll {
> 
>      private DefaultListModel<String> jlm = new DefaultListModel<>();
>      private JList<?> jl = new JList<>(jlm);
>      private JScrollPane sp = new JScrollPane(jl);
> 
>      public TestListScroll() {
>           final JFrame f = new JFrame();
>           try {
>                UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
>           } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
>                e.printStackTrace();
>           }
>           f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
>           f.setPreferredSize(new Dimension(180, 120));
>           sp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
>           sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
>           f.getContentPane().add(sp);
>           jl.setFixedCellHeight(15);
>           jlm.addElement("one");
>           jlm.addElement("two");
>           jlm.addElement("three");
>           jlm.addElement("four");
>           jlm.addElement("five");
>           jlm.addElement("six");
>           jlm.addElement("seven");
>           f.setLocationRelativeTo(null);
>           f.pack();
>           f.setVisible(true);
>      }
> 
>      public static void main(final String[] args) {
> 
>           EventQueue.invokeLater(new Runnable() {
> 
>                @Override
>                public void run() {
>                     new TestListScroll();
>                }
>           });
>      }

So I figured out I need to setLookAndFeel up front:

public class TestListScroll {
     {
          try {
               UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
          } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
               e.printStackTrace();
          }
     }

     private DefaultListModel<String> jlm = new DefaultListModel<>();
     private JList<?> jl = new JList<>(jlm);
     private JScrollPane sp = new JScrollPane(jl);

     public TestListScroll() {
          final JFrame f = new JFrame();
          f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
...

or after the fact.
...
          f.pack();
          SwingUtilities.updateComponentTreeUI(f);
          f.setVisible(true);
     }

You can't just assign it to a JScrollBar after the JScrollPane is created by just calling UIManager.setLookAndFeel, or by calling .updateUI() on the JScrollPane.  You have to call the setLookAndFeel before the JScrollPane is created, or get the scrollbar(s) from the JScrollPane and call their updateUI() method.  I was missing this in my test, took me awhile to realize.

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

How does a scrollbar paint differently? Eric Douglas <e.d.programmer@gmail.com> - 2018-05-29 12:42 -0700
  Re: How does a scrollbar paint differently? Eric Douglas <e.d.programmer@gmail.com> - 2018-05-29 13:50 -0700
    Re: How does a scrollbar paint differently? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-05-29 17:17 -0400
      Re: How does a scrollbar paint differently? Eric Douglas <e.d.programmer@gmail.com> - 2018-05-30 07:37 -0700
        Re: How does a scrollbar paint differently? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-05-30 11:09 -0400
          Re: How does a scrollbar paint differently? Eric Douglas <e.d.programmer@gmail.com> - 2018-05-30 08:26 -0700
        Re: How does a scrollbar paint differently? Eric Douglas <e.d.programmer@gmail.com> - 2018-05-30 08:24 -0700
          Re: How does a scrollbar paint differently? Eric Douglas <e.d.programmer@gmail.com> - 2018-05-30 08:46 -0700
            Re: How does a scrollbar paint differently? Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2018-05-30 18:14 +0200
              Re: How does a scrollbar paint differently? Eric Douglas <e.d.programmer@gmail.com> - 2018-05-30 10:31 -0700
                Re: How does a scrollbar paint differently? Eric Douglas <e.d.programmer@gmail.com> - 2018-05-30 10:53 -0700
                Re: How does a scrollbar paint differently? Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2018-05-30 20:35 +0200

csiph-web