Atomiklan
Atomiklan

Reputation: 5464

Where to set VERTICAL_SCROLLBAR_NEVER?

I am very new to Java, so go easy on me please. After quite a bit of research I believe I understand that the JScrollPane option displays the scroll bar by default. I am trying to change this behavior in some existing code. I believe the default simply needs to be changed with VERTICAL_SCROLLBAR_NEVER. However, I am having difficulty determining where this is set in the code. Can somebody please help show me where this property is set (or if I am even on the right track)?

This is the segment of code I believe to be relevant to this question. A scroll bar is present on the right side of the split pane window. I would like to permanently hide this.

public class LauncherFrame extends JFrame {

    private final Launcher launcher;

    @Getter
    private final InstanceTable instancesTable = new InstanceTable();
    private final InstanceTableModel instancesModel;
    @Getter
    private final JScrollPane instanceScroll = new JScrollPane(instancesTable);
    private WebpagePanel webView;
    private JSplitPane splitPane;
    private final JButton launchButton = new JButton(SharedLocale.tr("launcher.launch"));
    private final JButton refreshButton = new JButton(SharedLocale.tr("launcher.checkForUpdates"));
    private final JButton optionsButton = new JButton(SharedLocale.tr("launcher.options"));
    private final JButton selfUpdateButton = new JButton(SharedLocale.tr("launcher.updateLauncher"));
    private final JCheckBox updateCheck = new JCheckBox(SharedLocale.tr("launcher.downloadUpdates"));

    public LauncherFrame(@NonNull Launcher launcher) {
        super(tr("launcher.title", launcher.getVersion()));

        this.launcher = launcher;
        instancesModel = new InstanceTableModel(launcher.getInstances());

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(new Dimension(615, 322));
        setResizable(false);
        initComponents();
        setLocationRelativeTo(null);

        SwingHelper.setFrameIcon(this, Launcher.class, "icon.png");

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                loadInstances();
            }
        });
    }

    private void initComponents() {
        JPanel container = createContainerPanel();
        container.setLayout(new MigLayout("fill, insets dialog", "[][]push[][]", "[grow][]"));

        webView = createNewsPanel();
        splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, instanceScroll, webView);
        selfUpdateButton.setVisible(launcher.getUpdateManager().getPendingUpdate());

        launcher.getUpdateManager().addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                if (evt.getPropertyName().equals("pendingUpdate")) {
                    selfUpdateButton.setVisible((Boolean) evt.getNewValue());

                }
            }
        });

        updateCheck.setSelected(true);
        instancesTable.setModel(instancesModel);
        launchButton.setFont(launchButton.getFont().deriveFont(Font.BOLD));
        splitPane.setDividerLocation(100);
        splitPane.setDividerSize(4);
        splitPane.setOpaque(false);
        container.add(splitPane, "grow, wrap, span 5, gapbottom unrel, w null:680, h null:350");
        SwingHelper.flattenJSplitPane(splitPane);
        container.add(refreshButton);
        container.add(updateCheck);
        container.add(selfUpdateButton);
        container.add(optionsButton);
        container.add(launchButton);

        add(container, BorderLayout.CENTER);
    }

* UPDATE *

Attempted suggestions below, but still getting visible scroll bar. I changed window to resizable true so you can see it easier when reduced in size.

Scroll bar example

enter image description here

Upvotes: -1

Views: 145

Answers (2)

Perdi Estaquel
Perdi Estaquel

Reputation: 846

Your splitPane's two components are a JScrollPane on the left, and on the right we cannot tell because you did not include that part of the code.

From the two screenshots, we can only guess you are interested in having the component on the RIGHT without scrollbars.

So if that's the case, you'll have to show the createNewPanel() code, or indeed make the right component a JScrollPane with no scrollbars too.

Upvotes: 0

khelwood
khelwood

Reputation: 59233

You can pass scrollbar policies into the constructor when you create the new scrollpane.

private final JScrollPane instanceScroll = new JScrollPane(instancesTable, 
                           ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER, 
                           ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

Or you can leave your constructor call as it is, and set the policy (for instance) in your initComponents() method.

instanceScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);

Upvotes: 1

Related Questions