Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What's involved in converting JPanel to JScrollPane?

Status
Not open for further replies.

sergeiY

Programmer
Feb 13, 2003
132
AU
I've build my app and now I realised that I need scrolling functionality on my JPanel. What's involved in converting JPanel to JScrollPane ? Is this what I need to do or is there another way to do it ? I found couple of examples of JScrollPane on sun site but for some reason they don't work...
 
All you have to do is add the JPanel to the JScrollPanes viewport.

However, the JPanel has to contain enough other components to require scrolling, otherwise no scroll bars will appear. To get around this, set the size of the JPanel with setPreferredSize().
 
I've changed it to JScrollPane now but becouse I don't load any big items on it it doesn't show scrolls... am I right ?

I am using it as a canvas to draw various shapes... but I would like to scroll it. Can I forse it to show scroll bars ?

here is my code:
public ApplicationFrame()
{
final int DEFAULT_FRAME_WIDTH = 450;
final int DEFAULT_FRAME_HIGHT = 450;
setSize(DEFAULT_FRAME_WIDTH, DEFAULT_FRAME_HIGHT);

Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());

//Init of top/help panel
myHelpPanel = new HelpPanel();
contentPane.add(myHelpPanel, "North");

//init design area and add it to the container
myDesignArea = new DesignCanvas(this);
JScrollPane myScrollPane = new JScrollPane();
myScrollPane.getViewport().add(myDesignArea);

//drow canvas border.
Border compound = BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(5,15,15,15), BorderFactory.createLineBorder(Color.black));
myScrollPane.setBorder(compound);

contentPane.add(myScrollPane, BorderLayout.CENTER);

//init tool bar and add it to the container
DesignToolBar myTools = new DesignToolBar(myDesignArea, myHelpPanel);
contentPane.add(myTools, "South");

//init of menu bar and add it to the container
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);

//create menu item listener
MenuListener menuItemListener = new MenuListener();

//create file menu items and add them to the menu bar
JMenu fileMenu = new JMenu("File");
 
Try setting the size of the canvas with setSize or setPreferredSize (whichever works). Set it to a size way larger than your frame, and you should get scroll bars.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top