Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

JScrollPane problem with JPanel with setLayout(null)

Posted on 2003-02-20
5
Medium Priority
?
2,380 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
4 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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month10 days, 18 hours left to enroll

571 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