The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

two JScrollPane in one JFrame problem

hiiii i have a jframe .i have created one panel in it named 'totalGUI'.now i have two panels inside 'totalGUI' named 'panelprices' and 'jpanel'..'panelprices is put inside jscrollpane named 'scrollpane' and 'panelprices' is put inside jscrollpane named 'scrollpane2'.Boths the scrollpanes are added inside totalGUI. 'totalGUI' is added inside jframe. But the problem is while displaying i jst see one vertical scrollbar for panelprices jpanel.i cannot see any other scrollbar.
This is the output what i get.But i want scrollbar on both the sides.This is my code.Please help me solve the problem

//while calling initialize is called first
//and then makeBar is called
//and i have other functions which go on adding jlabel in jpanel named 'jpanel' which i m not //displaying to avoid showing unnecessary code

Re: two JScrollPane in one JFrame problem

I prefer to help someone get their code running, but yours contains a number of bad habits and/or errors that I prefer to steer you away from rather than encourage. The points I'm trying to make in the code study below include:

1. Learn to appreciate and use the provided layout mangers.
2. Use static variables (or "class variables") sparingly, favoring none at all. Have a good reason for using them.
3. Declare variables as needed for visibility outside the constructor, again sparingly.
4. Initialize and build the class in its constructor.
5. Set a component's size or preferred size for a reason.
6. Let Swing do its job by using the pack() method.
7. Launch Swing applications on the EDT as shown in the main() method.

I've also demonstrated some points about JScrollPanes using code that has been commented out or could be commented out to change the results. Please explore those options to gain greater understanding.

Let me know if you have any questions.

import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class TwoScrollPanes
{
// note that there are no 'class' or static variables here
// the default constructor
public TwoScrollPanes()
{
JFrame twoScrollPanes = new JFrame( "Two Scroll Panes" );
// experiment with using the default BorderLayout, putting the
// scroll panes in various locations
twoScrollPanes.setLayout( new FlowLayout() );
twoScrollPanes.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
twoScrollPanes.add( scrollPaneCreator( 0, 1000 ) );
twoScrollPanes.add( scrollPaneCreator( 1001, 1000 ) );
twoScrollPanes.pack();
twoScrollPanes.setVisible( true );
} // end default constructor TwoScrollPanes()
// method panelCreator() creates a JPanel that contains 'items' sequential
// numbers beginning with 'begin'
private JScrollPane scrollPaneCreator( int begin, int items )
{
// *** APPROACH 1 *** comment out this code to try APPROACH 2
JScrollPane scrollPane = new JScrollPane();
scrollPane.setPreferredSize( new Dimension( 200, 500 ) );
JTextArea text = new JTextArea();
// this line is not only unnecessary, but it limits the area shown in
// the JScrollPane to the size specified. the lines of data exist, but
// they cannot be viewed because they're outside the area shown in the
// scroll pane. uncomment it and see what happens
// text.setPreferredSize( new Dimension( 200, 500) );
// fill the text area with lines of numbers
while ( items-- >= 0 )
{
text.append( "" + begin++ + "\n" );
}
// note that the following line sets the viewport of an existing
// JScrollPane. an alternative that accomplishes the same thing is to
// do the above steps in a different order as shown in the commented
// code below labeled APPROACH 2
scrollPane.setViewportView( text );
// *** END OF APPROACH 1 ***
/*
// *** APPROACH 2 *** comment out this code to try APPROACH 1
JTextArea text = new JTextArea();
// this line is not only unnecessary, but it limits the area shown in
// the JScrollPane to the size specified. while the lines of data exist,
// they cannot be viewed because they're outside the area shown in the
// scroll pane. uncomment it and see what happens
// text.setPreferredSize( new Dimension( 200, 500) );
// fill the text areas with lines of numbers
while ( items-- >= 0 )
{
text.append( "" + begin++ + "\n" );
}
JScrollPane scrollPane = new JScrollPane( text );
scrollPane.setPreferredSize( new Dimension( 200, 500 ) );
// *** END OF APPROACH 2 ***
*/
// note what happens if the following line is commented out
// move the cursor to the top of the text
text.setCaretPosition( 0 );
return scrollPane;
} // end method panelCreator()
// a main() method that runs the application on the EDT for test and
// demonstration purposes
public static void main( String[] args )
{
SwingUtilities.invokeLater( new Runnable()
{
@Override
public void run()
{
new TwoScrollPanes();
}
} );
} // end method main()
} // end class TwoScrollPanes