Skip to main content
gamedev.net
🔒 Locked

[java] Scrollbar in JPanel

Started by Halsafar Dec 20, 2005 at 2:12 PM 2 replies 1k views
Original Post
Halsafar
Halsafar
Okay I am creating a two panels and adding them to a JFrame's contentPane. - one panel to the far right is a bunch of tabs - the other is the rest of the JFrame, this panel is used to draw on. The panel I wish to draw on is displaying a map which at times may be bigger than the screen of the user. So I need to be able able to scroll around the JPanel. I found JScrollPane and made it work with a JTextArea. However I cannot get the JScrollPane to work how I want for anything else. See if I set it up to have a JScrollPane parent the draw Panel, scroll bars NEVER appear no matter how much I resize the frame. If I start the Panel with a ridiculous preferred size (like 3000, 3000) I still see no ScrollBar even though the Panel is clearly not all being seen. Any advice? Edit: Also, I cannot get the two panels to stay together nicely or size properly. I cannot figure out how to in english tell it: "tab pane stay to the right, take up like 30% of the space, draw pane take up everything else but never extend past the tab pane, if you must then use scroll bars." -- That sums up my problem heh.
Son of Cain
Son of Cain
Take a look at the methods:

- setViewportView()
- setHorizontalScrollBarPolicy()

And the counterpart for vertical scroll bar. I think you get around this by reading the docs, but then again, I might have misunderstood your problem :p

Son Of Cain
a.k.a javabeats at yahoo.ca
Halsafar
Halsafar
Okay, I've tried so much...

I cannot get it to work...
Its tough to snippet this one, I might have to right a basic program to just do this task to get it working.

If you can gather whats going on, all displays okay except for 2 things:

- the m_drawPane which scrollPane becomes the parent of doesn't seem to follow the constraint rules very well, he is always to big vertically.
- the m_drawPane displays empty scroll bars no matter how big the getMaximumSize() and how small getPreferredSize() is. I have no idea how to force it to want to display some scroll bars.

Just to note, the maximum size allowed of the component is set dynamically whenever the frame is resized. Same with the tabbed pane, but it seems to be working great.
[source lang=java]	   //Add the pane to conentpane of the frame	   m_drawPane = new JPanel();	   m_drawPane.setPreferredSize(new Dimension(1000,1000));	   JScrollPane scrollPane = new JScrollPane(m_drawPane);	   scrollPane.setViewportView(m_drawPane);	   scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);	   scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);	   SpringLayout layout = new SpringLayout();	   JPanel masterPane = new JPanel();	   masterPane.setOpaque(true);	   masterPane.setLayout(layout);	   masterPane.add(m_layerTabs);	   masterPane.add(scrollPane);	   layout.putConstraint(SpringLayout.EAST, m_layerTabs, 0, SpringLayout.EAST, masterPane);	   layout.putConstraint(SpringLayout.NORTH, m_layerTabs, 5, SpringLayout.NORTH, masterPane);	   layout.putConstraint(SpringLayout.SOUTH, m_layerTabs, 0, SpringLayout.SOUTH, masterPane);	   layout.putConstraint(SpringLayout.WEST, scrollPane, 0, SpringLayout.WEST, masterPane);	   layout.putConstraint(SpringLayout.EAST, scrollPane, 0, SpringLayout.WEST, m_layerTabs);	   layout.putConstraint(SpringLayout.NORTH, scrollPane, 5, SpringLayout.NORTH, masterPane);	   layout.putConstraint(SpringLayout.SOUTH, scrollPane, 0, SpringLayout.SOUTH, masterPane);



Edit: Just to note, the map the m_drawFrame needs to display is a grid, with lines being drawing via Java2D to represent a map. Its an old school Doom style map editor. The scrolling I need to impliment is so the user can move the "view rect" around, not necessarily scroll around a panel... hmmmm... me thinks I might just need a scroll bar which can slide so I can handle all the messages myself for changing the view rect.
Halsafar
Halsafar
Apparently it all lied with the SpringLayout.

I got it all working once I switched to a nice and simple BorderLayout. Looks better to with less code.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.