Reputation: 2439
I'm having this piece of code, I want to add a scrollbar for CanvasBoard, but it does not show the scroll bar. CanvasBoard and PointCanvas extends JPanel.
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel cb = new JPanel();
JScrollPane scrollPane = new JScrollPane(cb);
scrollPane.setPreferredSize(new Dimension(2000, 600));
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, scrollPane,
new JPanel());
splitPane.setOneTouchExpandable(true);
splitPane.setDividerLocation(150);
Dimension minimumSize = new Dimension(600, 600);
cb.setMinimumSize(minimumSize);
frame.add(splitPane);
frame.setSize(1200, 600);
frame.setVisible(true);
}
Do you have any idea why?Thanks!
Upvotes: 2
Views: 1492
Reputation: 51485
The default for a JScrollPane is to have the scroll bars show up when needed.
I changed a few things in your example and added commands to make the scroll bars visible.
Here's your Swing layout.
And here's the modified code.
package com.ggl.testing;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;
public class ScrollPaneTest implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new ScrollPaneTest());
}
@Override
public void run() {
JFrame frame = new JFrame("Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel cb = new JPanel();
JScrollPane scrollPane = new JScrollPane(cb);
scrollPane
.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane
.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(2000, 600));
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
scrollPane, new JPanel());
splitPane.setOneTouchExpandable(true);
splitPane.setDividerLocation(150);
Dimension minimumSize = new Dimension(600, 600);
cb.setMinimumSize(minimumSize);
frame.add(splitPane);
frame.setSize(1200, 600);
frame.setVisible(true);
}
}
Upvotes: 2