2

I have a very simple JFrame with only single JSlider in its North Panel. I want JSlider to be displayed in minimum size by following method.

slider.putClientProperty("JComponent.sizeVariant", "mini");

but it is still displaying in its normal size. Following trick is not working too.

SwingUtilities.updateComponentTreeUI(this);

Below is a full program.

import java.awt.*;
import javax.swing.*;

public class TestSliderSize extends JFrame {

    public TestSliderSize() {
        setLookNFeel();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        this.setSize(screenSize.width / 2, screenSize.height / 2);
        JSlider slider = new JSlider(SwingConstants.HORIZONTAL, 0, 50, 10);
        slider.putClientProperty("JComponent.sizeVariant", "mini");
        slider.setMajorTickSpacing(10);
        slider.setMinorTickSpacing(1);
        slider.setPaintTicks(true);
        slider.setPaintLabels(true);

            //SwingUtilities.updateComponentTreeUI(this);
        getContentPane().setLayout(new BorderLayout());
        JPanel centerPanel = new JPanel(new BorderLayout());

        JPanel northPanel = new JPanel(new FlowLayout());
        Dimension d1 = centerPanel.getSize();
        northPanel.setPreferredSize(new Dimension(screenSize.width / 2, 100));
        northPanel.add(slider);
        northPanel.setBackground(Color.BLACK);
        centerPanel.add(northPanel, BorderLayout.NORTH);

        JPanel innerPanel = new JPanel();
        innerPanel.setBackground(Color.ORANGE);
        centerPanel.add(innerPanel, BorderLayout.CENTER);

        SwingUtilities.updateComponentTreeUI(this);

        getContentPane().add(centerPanel, BorderLayout.CENTER);
        this.setVisible(true);

    }

    public static void main(String[] args) {

        new TestSliderSize();
    }

    private void setLookNFeel() {
        try {              //   UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e1) {
        }
    }
}

Now I want help in displying the slider in minimum size.

  • 1
    What is the difference between normal size and minimum size? I don't get what you are expecting. The minimum size _is_ the normal size, given it's preferred size it respected, which is what's occurring in your code – Paul Samsotha Jul 12 '14 at 08:54
  • 1
    And why does your question title say JScrollPane? I don't see any mention of a JScrollPane neither in your description nor in your code. – Paul Samsotha Jul 12 '14 at 09:18
  • 1
    it was a mistake i have corrected it. – user3741146 Jul 12 '14 at 10:55

1 Answers1

2

The key here is to pack() the enclosing Window so that components adopt their preferred sizes. The innerPanel has been given an arbitrary size to illustrate the effect. See also Initial Threads.

image

import java.awt.*;
import javax.swing.*;

public class TestSliderSize extends JFrame {

    public TestSliderSize() {
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        JSlider slider = new JSlider(SwingConstants.HORIZONTAL, 0, 50, 10);
        slider.putClientProperty("JComponent.sizeVariant", "mini");
        slider.setMajorTickSpacing(10);
        slider.setMinorTickSpacing(1);
        slider.setPaintTicks(true);
        slider.setPaintLabels(true);

        JPanel centerPanel = new JPanel(new BorderLayout());
        JPanel northPanel = new JPanel();
        northPanel.add(slider);
        northPanel.setBackground(Color.BLACK);
        centerPanel.add(northPanel, BorderLayout.NORTH);
        JPanel innerPanel = new JPanel() {

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 100); // arbitrary size
            }
        };
        innerPanel.setBackground(Color.ORANGE);
        centerPanel.add(innerPanel, BorderLayout.CENTER);
        this.add(centerPanel, BorderLayout.CENTER);

        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestSliderSize();
            }
        });
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045