I tried this to try and add a scroll pane directly to the JFrame.
It should work by inheritance. But it complains I'm casting my class to be a JscrollPane or a JPanel or casting a JPanel to be a JScrollPane or casting a JScrollPane to be a JPanel.
import java.awt.event.*; import javax.swing.*; import java.awt.*; import java.util.*; import javax.swing.ScrollPaneLayout; /** * Write a description of class ScrollableJFrame here. * * @author (your name) * @version (a version number or a date) */ public class ScrollableJFrame extends JFrame { public ScrollableJFrame() { super("A scrollable JFrame at last!!!"); setVisible(true); ScrollPaneLayout spl = new ScrollPaneLayout(); spl.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); spl.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); spl.layoutContainer(this); setLayout(spl); setContentPane(getContentPane()); } public static void main(String[] args) { new ScrollableJFrame(); } }
So if this is used for only JScrollPanes, which I'm starting to feel it was meant only to be used by them, despite JFrame being legally able to by inheritance, no compiler errors, but always a Class Cast Exception.
So, what does this layout manager do if it doesn't automatically add a JScrollPane to any Container I feel like adding it to, like I thought it would?
Also, I'm wondering what in the world a CardLayout would do. When would you use one?
I'm experimenting with Layouts and had always wanted to make a scrollable JFrame and thought that ScrollPaneLayout was the answer to my quest. Apparently not.