Link to home
Start Free TrialLog in
Avatar of alpjose
alpjose

asked on

Adding ScrollBars to JPanel

Hi ,

  I have JFrame, over which i have a JInternalFrame. On this JInternalFrame i am displaying an Image using JPanel, ie JPanel which contains the image is placed on JInternalFrame.

Code is like this.

class myclass extends JPanel{
...
----
// constructor
myclass()
{
-- --
--
JScrollPane scroll = new JScrollPane(this);

}

}
i have set the color of scrollpane to white(for testing the presence)

When the image is displayed the scrollpane appears as a bar next to image,
when the image is zoomed, this bar disappears and the image is zoomed, but no scrollbars appear.

can i get help for this,

regards
Avatar of weelink
weelink

How did you filled the scrollpane with the image?
Avatar of Mayank S
JScrollPane scroll = new JScrollPane ( this, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED ) ;

Mayank.
When I want a image with scrollbars I do the following:

ImageIcon image = new ImageIcon("image.gif");
JLabel lblImage = new JLabel(image);

JScrollPane scrollPane = new JScrollPane(lblImage);


Does this help?
Avatar of alpjose

ASKER

Hi ,

  I have added image to JPanel. then added this JPanel to JScrollPane.
This object of scrollpane is called in another class which is JInternalFrame.

When the image is zoomed the scrollbars appear and if the mouse is just moved a bit on the image immediately the scrollbars disappear.

this is the problem.

i have used this.

JScrollPane scroll = new JScrollPane ( this, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED ) ;

thanks and regards


Try:

JScrollPane scroll = new JScrollPane ( this, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS ) ;

Mayank.
Avatar of alpjose

ASKER

mayank, i have tried this also, same thing happens, when the image is zoomed, scrollbars appear and when the mouse is just moved on the image , i think when it get repainted it immediately disappears,
what can be be done for this
Did you implement a MouseListener somewhere?
Avatar of alpjose

ASKER

yes i have implemented MouseListener,
Does this MouseListener has anything to do with the scrollpane? And if so, could you post the code?
Avatar of alpjose

ASKER

Now the Scrollbars doesn't disappear, the scrollbars(both horizontal and vertical) are visible.

I have set the preferredSize(800,800);, so it is appearing, but when the scrollbars are scrolled, without zooming, repainting becomes very slow it becomes alright when i minimize and maximize the window, is there a way out of this.

once zoomed and scrolled, it works quiet OK,not perfectly.

ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of alpjose

ASKER

Hi,
  Thank you , it works properly.
  The Component's setPreferredSize() method has to be used for the JScrollpane to work properly.

And for repaint problem, in the paint method paintComponent() method has to be used.


it works fine.

Pls close this question

Thanks and Regards


alpjose:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
Recommendation: Accept mayankeagle's comment as answer.