Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: what's going on? I tried to make a scrollable JFrame and it didn't work!

  1. #1
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Unhappy 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.

    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.


  2. #2
    Member
    Join Date
    Aug 2011
    Posts
    48
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: what's going on? I tried to make a scrollable JFrame and it didn't work!

    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
    Last edited by ha.minh.nam; December 4th, 2011 at 07:32 PM.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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,
    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.

  5. #5
    Member
    Join Date
    Aug 2011
    Posts
    48
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: what's going on? I tried to make a scrollable JFrame and it didn't work!

    The problem is at this statement:

    spl.layoutContainer(this);

    According to java doc of class JScrollPaneLayout:

    public void layoutContainer(Container parent)

    And for JFrame:

    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
    Last edited by ha.minh.nam; December 4th, 2011 at 07:32 PM.

Similar Threads

  1. connecting jframe to another jframe
    By ajtambalo in forum Member Introductions
    Replies: 2
    Last Post: May 11th, 2011, 11:24 AM
  2. How to make for loop work?
    By Dragonkndr712 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 8th, 2011, 02:14 PM
  3. Dynamically Sized Scrollable JPanels
    By aussiemcgr in forum Java Theory & Questions
    Replies: 10
    Last Post: November 20th, 2010, 02:26 PM
  4. Unnable to make validations work for login screen
    By fierof2 in forum Web Frameworks
    Replies: 8
    Last Post: July 29th, 2010, 07:12 AM
  5. how to make a simple JButton on a JFrame window?
    By chronoz13 in forum AWT / Java Swing
    Replies: 8
    Last Post: November 20th, 2009, 10:08 PM