?
Solved

JScrollPane scrolling problem

Posted on 2003-02-20
7
Medium Priority
?
915 Views
Last Modified: 2008-02-01
I 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
  • 2
  • 2
7 Comments
 
LVL 8

Expert Comment

by:Exceter
ID: 7991978
You were not adding the panel to the scrollpane.

>> scrollPane = new JScrollPane();

This line should read,

scrollPane = new JScrollPane( panel );

However, this will only display the scroll bars as needed. So I said the following to show that they are actually there,

scrollPane = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );

-- Test.java --

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(panel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
          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);
     }
}
-- End of Test.java --

Exceter
0
 

Author Comment

by:sultanatif
ID: 7996012
Shouldnt the JScrollPane show scroll bars when the Jpanel size is greater than JScrollPane.
What I want to do is that I want to put a list of lables on the JPanel & when the list grows the scroll bars should show automatically.
When I add awt componet Panel to ScrollPane by giving the same bounds as given in the program, it works fine but when i use swing, it doesnt work.
0
 
LVL 8

Accepted Solution

by:
Exceter earned 120 total points
ID: 7996758
>> Shouldnt the JScrollPane show scroll bars when the Jpanel size is greater than JScrollPane.

The JScrollPane assumes the size of panel when it is instantiated. If the contents of the JPanel will not fit within it the scroll bars will appear IF you have told them to. In my example above I told the scrollpane to always show a vertical scrollbar and never show a horizontal one. Try this,

scrollPane = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED );

Or with the default arguments,

scrollPane = new JScrollPane( panel );

Exceter
0
 

Author Comment

by:sultanatif
ID: 8004625
Can u show an example by adding a component to the Jpanel & show it scrolling please. I have tried doing it but it doesnt scroll.
0
 

Expert Comment

by:CleanupPing
ID: 9474641
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

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
Suggested Courses

762 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