469,328 Members | 1,276 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

Layoutmanagers, JScrollPane and setLayout(null)

Hey everyone,

I'll cut right to the case. I have a JPanel and i call setLayout(null) on it. Simply because I would like to position everything myself and haven't seen a handy manager yet. Now i need to add this panel to a JScrollPane and this is where all the trouble starts. The JScrollPane simply doesn't like the absence of a LayoutManager.

I have been trying to puzzle through this for hours now and could use some help. I have tried to write my own LayoutManager which only implements the preferredSize method completely from the LayoutManager interface, but couldn't find a way to make the JScrollPane notice the changes in size.

I'd like to see a solution without a LayoutManager because I am really not too fond of these things. However I don't think there is one. Also if I am missing out a LayoutManager that would be perfect to have everything floating around the way I want it, feel free to point me in that direction. If anyone could show me a working preferredSize method that would also be fine.

Thanks in advance,

Jan 22 '09 #1
6 11901
11,448 Expert 8TB
Did you use a JViewport for your own JComponent? (i.e. wrap your component in the viewport and set the viewport in the scroll pane).

kind regards,

Jan 23 '09 #2
I have done the following after your response:

Expand|Select|Wrap|Line Numbers
  1. JViewport viewport = new JViewport();
  2. viewport.setSize(frameWidth, frameHeight);
  3. viewport.setView(panel);
  5. JScrollPane scroll = new JScrollPane(viewport);
This does display the upper elements of the panel but still does not scroll. I am not really that familiar with the JViewport and googling on it doesn't really give great examples.

I did notice the method of JScrollPane called setViewport, however i have no idea what to fill in the constructor of JScrollPane if i use the viewport for the setViewport method.

Am I doing something wrong?
Jan 25 '09 #3
I really could use some help. Still haven't been able to find a solution for this.

Does anyone perhaps have a sample of a preferredSize method for a custom LayoutManager which would work with a JScrollPane?

Desperately in need of help,

Jan 29 '09 #4
11,448 Expert 8TB
We can't tell what your error is without seeing the relevant parts of your actual code. Have a look at this snippet:

Expand|Select|Wrap|Line Numbers
  1.         JFrame frame= new JFrame("test frame");
  2.         JPanel panel= new JPanel() {
  3.             public void paintComponent(Graphics g) {
  5.                 super.paintComponent(g);
  6.                 g.setColor(Color.BLACK);
  7.                 g.drawOval(0, 0, 200, 200);
  8.             }
  9.         };
  11.         JLabel label= new JLabel("test");
  13.         panel.add(label);
  14.         label.setBounds(400, 400, 60, 20);
  16.         panel.setPreferredSize(new Dimension(500, 500));
  17.         panel.setLayout(null);
  19.         JScrollPane sp= new JScrollPane(panel);
  21.         frame.getContentPane().add(sp, BorderLayout.CENTER);
  22.         frame.pack();
  23.         frame.setVisible(true);
This code runs and it draws a small circle at the top left and draws a JLabel with the text "test" near the bottom right corner. Resize the frame and see the scroll bars appear. Note that the panel doesn't have a layout manager.

kind regards,

Jan 30 '09 #5
Thank you very much Jos. It was the preferredSize of the panel which was obviously way to small. Because if the panel is bigger then its preferredSize it doesn't have to scroll even tho there are object outside of the screen.

Thanks alot!!!!

Kind regards,

Jan 30 '09 #6
11,448 Expert 8TB
That should teach you to set the layout manager to null ;-) Think twice before you do that because those layout managers can handle most (if not all) of that darn layout stuff for you. Think of different screen resolutions, *shudder*.

kind regards,

Jan 30 '09 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Jim Katz | last post: by
10 posts views Thread by JulianP | last post: by
1 post views Thread by cozsmin | last post: by
2 posts views Thread by true blue | last post: by
9 posts views Thread by aleplgr | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.