?
Solved

JScrollPane scrolling problem

Posted on 2003-02-20
7
Medium Priority
?
920 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
  • 2
  • 2
5 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

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

616 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