I want to add a JScrollPane to a JPanel. I have added some more code to this old question of mine.
I have a Panel class as follows:
import java.awt.*;
import javax.swing.JPanel;
public class Panel extends JPanel
{
public Panel()
{
super (true);
}
public void paintComponent (Graphics g)
{
super.paintComponent(g);
setBackground(Color.WHITE);
g.drawRect(250, 250, 10, 10);
}
}
and a JScrollPaneTest class as follows:
import javax.swing.*;
public class JScrollPaneTest extends JFrame
{
Panel panel = new Panel();
public JScrollPaneTest ()
{
super("Test JScrollPane");
add(new JScrollPane(panel, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS));
setSize(300,300);
setVisible(true);
}
public static void main (String[] args)
{
JScrollPaneTest test = new JScrollPaneTest();
test.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
I have drawn a small rectangle in the lower right corner on the Panel; apparently the JScrollPane does not work because when resizing the rectangle disappears. Please let me know if you have any suggestions; thanks!