Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to make a large scrollable panel?  RSS feed

 
dejie lin
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have drawn a lovely diagram in a panel, it is quite large, How can i made the panel scrollable? i have already tried the following method, but there's no scrollbar comes out.
JPanel panel = new MyPainting(); //there are lot of lines and text are drawn in MyPainting panel
JScrollPane sp = new JScrollPane(panel);
getContentPane().add(sp,java.awt.BorderLayout.CENTER);
i was troubling in the problem two days already, anybody can help me? Do thanks
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your code looks fine. the scrollpane will only show up when the panel is only partially visible. if this is not the case, you can try two things:
- set the JScrollPane to display the scrollbars always,
- set the preferred size of the viewport of the scrollpane (scrollpane.getViewport().setPreferredSize(Dimension)).
but as your code looks ok, these changes won't have an effect, I suppose. in this case, the bug is somewhere in the rest of your code.
Chantal
[ January 29, 2003: Message edited by: Chantal Ackermann ]
 
Ashish Hareet
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Try setPreferredSize(Dimension) in the constructor of our JPanel subclass - should work.
HTH
Ashish H.
 
dejie lin
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for your kindly suggestions, now the scroll bar appear in my JPanel, and works well, but there's one problem:
it seems the scroll bar only works well unless the dimension of the JPanel is specified.
But in my project, the size of the diagram which would be drawn in a Jpanel is not fixed...the diagram is based on a standard text file.
so how can i make the scroll bar function well if the dimention of the JPanel is unknown?
thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!