473,461 Members | 1,553 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Layoutmanagers, JScrollPane and setLayout(null)

BSCode266
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,

BSCode266
Jan 22 '09 #1
6 12322
JosAH
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,

Jos
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);
  4.  
  5. JScrollPane scroll = new JScrollPane(viewport);
  6.  
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,

~BSCode266
Jan 29 '09 #4
JosAH
11,448 Expert 8TB
@BSCode266
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) {
  4.  
  5.                 super.paintComponent(g);
  6.                 g.setColor(Color.BLACK);
  7.                 g.drawOval(0, 0, 200, 200);
  8.             }
  9.         };
  10.  
  11.         JLabel label= new JLabel("test");
  12.  
  13.         panel.add(label);
  14.         label.setBounds(400, 400, 60, 20);
  15.  
  16.         panel.setPreferredSize(new Dimension(500, 500));
  17.         panel.setLayout(null);
  18.  
  19.         JScrollPane sp= new JScrollPane(panel);
  20.  
  21.         frame.getContentPane().add(sp, BorderLayout.CENTER);
  22.         frame.pack();
  23.         frame.setVisible(true);
  24.  
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,

Jos
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,

BSCode266
Jan 30 '09 #6
JosAH
11,448 Expert 8TB
@BSCode266
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,

Jos
Jan 30 '09 #7

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

Similar topics

0
by: Jim Katz | last post by:
I think I have found a bug in the behavior of the TreeCellRenderer setTextNonSelectionColor method. Instead of setting the text color for the current cell, it sets the color of the following row...
10
by: JulianP | last post by:
Hi I am working on a Turtle Program and I have put in all the fancy schmancy stuff in I just cant seem to figure out how to print the T, which is my turtle, on the screen. Here is the code for my...
1
by: cozsmin | last post by:
hello, my program is a simple banking application, i need it for my resumee, but i have a little problem i got stuck trying to maek some TextField -s visible , it seems that setting the layout (...
14
by: spruce moose | last post by:
Hey all. My program reads from a file which contains information about employees(ID, name, address, salary, HourlyRate, etc...). It then stores them in an array of objects and writes and displays the...
1
by: bruce628 | last post by:
I want to use SWT tab compnent and make it be multiline,but I fail.please see the class TabFolderExample. Can aneone help me? import java.awt.BorderLayout; import...
2
by: true blue | last post by:
hey ppl plz can any one help me with this code im gettin a null pointer exception when i use graphics class object (g.drawString)in action performed functio eventhough i hava declared the object...
9
by: aleplgr | last post by:
Hi! I'm trying to let the end-user select the language of the menues I'm showing him. To do that I've got this Selector class that shows the end user a combo box, a label and a button. In the combo...
1
by: rainman33 | last post by:
I am getting this error message in the program I'm working on (line 150), and am wondering what it means? This is an exercise for a college class; just want to declare that; this is not my own work....
6
by: pankajs | last post by:
Hello freinds! I m making a game but I m facing a problem that when I click New Game button(defined by me) animation starts. But in the animation keys are not working even the Close button at the...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.