Results 1 to 3 of 3
- 05-24-2009, 02:34 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 11
- Rep Power
- 0
[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.
-
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.
- 05-24-2009, 04:52 PM #3
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
-
jscrollpane
By kaemonsaionji in forum New To JavaReplies: 3Last Post: 02-25-2009, 08:39 AM -
ClassCast exception with XML files
By ranjithch in forum XMLReplies: 0Last Post: 01-19-2009, 02:02 PM -
problem with Jscrollpane
By ravrajesh.ap in forum AWT / SwingReplies: 5Last Post: 01-03-2009, 10:38 PM -
[SOLVED] JScrollPane - HELP!
By terox13 in forum AWT / SwingReplies: 8Last Post: 05-10-2008, 03:58 AM -
help with JScrollPane
By tommy in forum AWT / SwingReplies: 1Last Post: 08-06-2007, 07:58 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks