aspose file tools*
The moose likes Java in General and the fly likes how to add a scrollpane... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to add a scrollpane..." Watch "how to add a scrollpane..." New topic
Author

how to add a scrollpane...

vikas de
Ranch Hand

Joined: Feb 07, 2001
Posts: 39
hi huys
did somebody know how to add a scrollPane to JFrame ???
Thanx
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Did you try it? Just declare your frame and scrollpane references and just add as follows...
yourFrame.add(yourPane)

Bosun


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
vikas de
Ranch Hand

Joined: Feb 07, 2001
Posts: 39
i tried this...
ScrollPane js=new ScrollPane();
studentFrame.getContentPane().add(js);
but it doesnt run...it doesnt display any scrollpane...
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Vikas,
You need to place something into the ScrollPane. The ScrollPane by itself will never be visible. Try the following:

Regards,
Manfred.
vikas de
Ranch Hand

Joined: Feb 07, 2001
Posts: 39
the scrollpane is know visible but only from left to right...
is it possible to set the scrollpane that it will be displayed from top to down....???
vikas de
Ranch Hand

Joined: Feb 07, 2001
Posts: 39
i want to add a vertical scrollpane...did somebody know how this is possible...in my code i have add a scrollpane but it displays only horizontal...and i want it vertical
please see the code...
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class SwingWithMe
{
public static void main(String args[])
{
JTextField[] myTextField=new JTextField[30];
JPanel myPanel=new JPanel();
JFrame myFrame=new JFrame("Test");
JScrollPane myPane= new JScrollPane();
for(int i=0;i<30;i++)
{
myTextField[i]=new JTextField(10);
myPanel.add(myTextField[i]);
}
myPane=new JScrollPane(myPanel);
myFrame.getContentPane().add(myPane,BorderLayout.CENTER);
myFrame.setResizable(false);
myFrame.setLocation(0,100);
myFrame.setSize(1030,500);
myFrame.setVisible(true);
}
}
please help as soon as possible...
vikas

[This message has been edited by vikas de (edited March 30, 2001).]
vikas de
Ranch Hand

Joined: Feb 07, 2001
Posts: 39
Come on Guys....Please help its urgent...
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
The main problem with your current program is that myPanel has a default layout manager of FlowLayout. This means when you add things to it, they get added horizontally until you exceed the container's maximum width - only then will components be added to the next row instead. Since myPanel is inside a scroll pane, it has no maximum width - it can keep growing indefinitely. So there's never any reason for the LayoutManager to start a new line.
Why do you create two JScrollPanes? It looks like the first is never used - you should clean up your code to avoid needless confusion. I found your code difficult to follow - names like "myPane" and "myPanel" tell me nothing about the intended relationship between the components. I gave the components slightly more descriptive names in the code below. Note that I also changed the frame size and number of text fields to ensure that the scroll pane would actually be needed both horizontally and vertically. The scroll bar will only appear if the component inside the scroll pane is too large for the scroll pane in that direction (horizontal or vertical).
<code><pre>
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class SwingWithMe
{
public static void main(String args[])
{
JTextField[] textFields = new JTextField[100];
// start with one plain inner panel to hold all the textfields
JPanel innerPanel = new JPanel();
innerPanel.setLayout(new GridLayout(10, 10));
for (int i = 0; i < textFields.length; i++)
{
textFields[i] = new JTextField(10);
innerPanel.add(textFields[i]);
}
// put the inner panel into a scroll pane
JScrollPane scrollPane = new JScrollPane(innerPanel);
// construct an outer frame to hold the scroll pane
JFrame outerFrame = new JFrame("Test");
outerFrame.getContentPane().add(scrollPane);
outerFrame.setResizable(false);
outerFrame.setLocation(0, 100);
outerFrame.setSize(300, 200);
outerFrame.setVisible(true);
System.out.println("innerPanel size is " + innerPanel.getSize());
System.out.println("scrollPane size is " + scrollPane.getSize());
}
}
</pre></code>

[This message has been edited by Jim Yingst (edited April 01, 2001).]


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to add a scrollpane...