what's going on? I tried to make a scrollable JFrame and it didn't work!
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.
Code java:
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. :(
Re: what's going on? I tried to make a scrollable JFrame and it didn't work!
Quote:
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.
Please post the runtime exception here.
One solution is to have a JPanel added to a JScrollPane which is added to JFrame. This will work definitely.
immutable objects
Re: what's going on? I tried to make a scrollable JFrame and it didn't work!
I know that.
It says I'm making some kind of miscast involving a JScrollPane and a JPanel. Later, it says, that I'm casting the JFrame to be a JScrollPane.
I'm guessing that layout won't let you use it to auto set a JScrollPane directly to the JFrame.
I already know how to do it with a JPanel in a JScrollPane as the content pane.
Re: what's going on? I tried to make a scrollable JFrame and it didn't work!
ScrollPaneLayout is not a common Layout Manager. See A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container) Did you read the API? It specifically says "The layout manager used by JScrollPane"...where is your JScrollPane?
With as many posts as you have, you should by now know to post the full stack trace or error message. Further,
Quote:
Also, I'm wondering what in the world a CardLayout would do
with as many posts as you have, you should by now know what google is, and where the Oracle tutorials are.
Re: what's going on? I tried to make a scrollable JFrame and it didn't work!
The problem is at this statement:
Code :
spl.layoutContainer(this);
According to java doc of class JScrollPaneLayout:
Code :
public void layoutContainer(Container parent)
And for JFrame:
Code :
public class JFrame
extends Frame
implements WindowConstants, Accessible, RootPaneContainer
JFrame extends Frame which extends Window which extends Container.
So it is no problem if pass a JFrame into layoutContainer method. I think the implementation of JScrollPaneLayout rejects JFrame.
immutable objects