?
Solved

JScrollPane problem with JPanel with setLayout(null)

Posted on 2003-02-20
5
Medium Priority
?
2,232 Views
Last Modified: 2008-01-09
want to use JScrollPane with JPanel which has setLayout as null.
Can anyone please tell me why the scrolling is not working.
I dont want to change the Layout.
This is the code:
import javax.swing.*;
import java.awt.*;

public class test extends JFrame
{

static JLabel v1, v2, v3, v4, v5, v6, v7, v8, v9,v10,v11;
public ImageIcon images;
static JDesktopPane main;
JPanel panel;
JScrollPane scrollPane;
test()
{


main = new JDesktopPane();



setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(720,590);

setResizable(false);
setMaximizedBounds(new Rectangle(720,560));
//setVisible(false);
main = new JDesktopPane();
panel = new JPanel();
panel.setLayout(null);
panel.setBackground(Color.red);
panel.setBounds(100,100,400,400);


JLabel lab = new JLabel("atif");
lab.setBounds(300,10,30,20);
panel.add(lab);
setContentPane(main);

scrollPane = new JScrollPane();
scrollPane.setBounds(100,100,100,100);
scrollPane.getViewport().add(panel);
//panel.add( scrollPane);
main.add(scrollPane);

}

public static void main(String [] args)
{
test t = new test();
t.setVisible(true);
}




}
0
Comment
Question by:sultanatif
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 

Accepted Solution

by:
omom earned 200 total points
ID: 7989645
I'm curious as to why you want to set the layout to null.

Also, even if you remove the line
panel.setLayout(null);
it won't scroll because there's only one label in the panel, nothing to scroll through.
So you would have to add more labels (or longer) labels to get the scroll bar
(or set it to always have scroll bars:

setHorizontalScrollBarPolicy

public void setHorizontalScrollBarPolicy(int policy)

    Determines when the horizontal scrollbar appears in the scrollpane. The options are:

        * JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED
        * JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
        * JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS

Parameters:
    policy - one of the three values listed above
Throws:
    IllegalArgumentException - if policy is not one of the legal values shown above
See Also:
    getHorizontalScrollBarPolicy()
0
 
LVL 1

Expert Comment

by:membersh
ID: 7991725
If u don't want to change the panel's layout, u have get ur task done in indirect way.

Take another Panel [boo], set its Layout to BorderPanel, add ur Panel to boo, & then add boo to ScrollPane. Don't set any layout for ScrollPane. ie.,

JPanel boo = new JPanel() ;
boo.setLayout(new BorderLayout());
panel = new JPanel();
panel.setLayout(null);
panel.setBounds(10,10,400,400);
panel.setPreferredSize(new Dimension(400,400));
..
boo.add(panel) ;
scrollPane = new JScrollPane(boo);

Most IMP: do setPreferredSize() for ur Panel.
0
 

Expert Comment

by:CleanupPing
ID: 9059206
sultanatif:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 7

Expert Comment

by:tomboshell
ID: 9553695
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Accept omom's comment as answer.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Thomas Boshell
EE Cleanup Volunteer
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month7 days, 23 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question