Results 1 to 3 of 3
  1. #1
    thayalan is offline Member
    Join Date
    Feb 2009
    Posts
    11
    Rep Power
    0

    Default [SOLVED] JScrollPane - ClassCast Exception

    Hi there,

    In my code I have class CustomPanel which extends JPanel. myPanel is an instance of CustomPanel (for your info, myPanel is a white space used as sketchpad)

    I am in the process of making myPanel scrollable. I am getting the following error during runtime when i begin by drawing the first object on myPanel:

    "Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JViewport cannot be cast to javax.swing.JPanel"

    The following is the code used for making myPanel scrollable:

    myPanel.setLayout(null);
    myPanel.setPreferredSize(new Dimension(800,600));

    JScrollPane scrollPane=new JScrollPane();
    scrollPane.getViewport().add( myPanel );

    Container container = getContentPane();
    container.setLayout(new BorderLayout());
    container.add(scrollPane, BorderLayout.CENTER);
    container.validate();
    setSize(600, 400);
    setVisible(true);

    Please advice. Thank you.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    The reason for your problem is not jumping out at me based on the code you've posted, and that suggests to me that you're problem lies elsewhere, in code not posted. My bet is that to solve this here, you'll have to post more code and also indicate what line throws the error.

    I suggest that you create a very small program that demonstrates your problem, has no extraneous code unrelated to the problem, and yet is compilable and runnable from our boxes at home, an SSCCE. The link will describe in more detail exactly what an SSCCE is and how to make it. It requires a bit of effort on your part, but posting one makes it much more likely that we can supply you with a solution. And in actuality, the process of creating one more often than not will allow you to solve the problem yourself as it requires you to see the problem in all its nakedness.

    Best of luck.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    The child of a JScrollPane is its JViewport. The Container class has methods you can use to explore/navigate a containment hierarchy.
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class Test {
        private JScrollPane getContent() {
            JPanel panel = new JPanel();
            panel.setBackground(Color.pink);
            panel.setPreferredSize(new Dimension(500,500));
            return new JScrollPane(panel);
        }
    
        public static void main(String[] args) {
            Test test = new Test();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test.getContent());
            f.setSize(400,400);
            f.setLocation(100,100);
            f.setVisible(true);
            test.exploreContainerHierarchy(f);
        }
    
        private void exploreContainerHierarchy(Container parent) {
            Component[] c = parent.getComponents();
            for(int i = 0; i < c.length; i++) {
                System.out.println(c[i].getClass().getSimpleName());
                if(((Container)c[i]).getComponentCount() > 0) {
                    exploreContainerHierarchy((Container)c[i]);
                }
            }
        }
    }

Similar Threads

  1. jscrollpane
    By kaemonsaionji in forum New To Java
    Replies: 3
    Last Post: 02-25-2009, 08:39 AM
  2. ClassCast exception with XML files
    By ranjithch in forum XML
    Replies: 0
    Last Post: 01-19-2009, 02:02 PM
  3. problem with Jscrollpane
    By ravrajesh.ap in forum AWT / Swing
    Replies: 5
    Last Post: 01-03-2009, 10:38 PM
  4. [SOLVED] JScrollPane - HELP!
    By terox13 in forum AWT / Swing
    Replies: 8
    Last Post: 05-10-2008, 03:58 AM
  5. help with JScrollPane
    By tommy in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 07:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •