Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to add a scrollpane...

 
vikas de
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi huys
did somebody know how to add a scrollPane to JFrame ???
Thanx
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try it? Just declare your frame and scrollpane references and just add as follows...
yourFrame.add(yourPane)

Bosun
 
vikas de
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tried this...
ScrollPane js=new ScrollPane();
studentFrame.getContentPane().add(js);
but it doesnt run...it doesnt display any scrollpane...
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Come on Guys....Please help its urgent...
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic