473,376 Members | 1,041 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,376 software developers and data experts.

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 12311
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

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

Similar topics

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...
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...
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 (...
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...
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...
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...
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...
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....
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...
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.