I've found some examples scattered around the internet involving getting an image or a textbox to display scroll bars, but they all involve a program that basically displays its entire contents in a scroll pane. What I need to get it to do is stick a JPanel somewhere, pile a bunch of text, icons, etc into that panel until its too big for the space I've got for it, and then scroll across it.
I'm probably going to need to be able to null-layout that panel, too, because I'm probably going to have to manually position things within it sooner or later.
So I tried to make a pretty simply program that would put three colored blocks in a space big enough for two, and display a scrollbar that would let you scroll down to see the third one. It doesn't add the scroll bar.
The imports are a bit of a mess and redundant, because this is cobbled together from a couple different files, but they're not generating any errors.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
public class ScrollDemo extends JFrame {
JScrollPane scrollpane;
public ScrollDemo() {
super("JScrollPane Demonstration");
setSize(800, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
init();
setVisible(true);
}
public void init() {
setLayout(null);
JPanel innerPanel = new JPanel();
JPanel outerPanel = new JPanel();
getContentPane().setBounds(0,0,800,600);
outerPanel.setBounds(400,0,400,400);
innerPanel.setBounds(0,0,600,600);
innerPanel.setLayout(null);
JPanel greenPanel = new JPanel();
JPanel yellowPanel = new JPanel();
JPanel bluePanel = new JPanel();
greenPanel.setBounds(0,0,200,200);
yellowPanel.setBounds(0,200,200,200);
bluePanel.setBounds(0,400,200,200);
greenPanel.setBackground(Color.GREEN);
yellowPanel.setBackground(Color.YELLOW);
bluePanel.setBackground(Color.BLUE);
innerPanel.add(greenPanel);
innerPanel.add(yellowPanel);
innerPanel.add(bluePanel);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(innerPanel);
scrollPane.setBounds(200,0,200,400);
add(scrollPane);
}
public static void main(String args[]) {
new ScrollDemo();
}
}