Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > JPanel inside JPanel inside JFrame

Reply
Thread Tools

JPanel inside JPanel inside JFrame

 
 
t1m1976 t1m1976 is offline
Junior Member
Join Date: Nov 2010
Posts: 2
 
      11-07-2010
I've got a program I'm making for an old friend. I'm fairly new to creating GUI with Java and even more new to using LayoutManager but I have been doing great so far. I'm about to release my program to my friend but I am not satisfied with my current layout because there is no horizontal scrollBar he can use to see all the information contained in TextPane inside the JPanel.

The problem is, I have a vertical scrollBar displaying a list and I require JButton on the left side of the JPanel, while corresponding JTextPane will be to the right of each JButton. The list can be quite long so I will require the vertical scrollBar (which is a JPanel inside a JScrollPane). However, when the information, contained in each JTextPane, to the right of each button becomes wider than the JPanel, I would like to see a horizontal scrollBar. Now, that wouldn't be a problem for my current experience with Java except that I want the JButton on the left side NOT to scroll with the horizontal scrollBar while the JPanel with the vertical scrollBar remains capable of scrolling both the JButtons and the JTextPane.

Confused? I hope not. I'd like to offer my friend something he and I can both be proud of. I've written a sample code here, but I am such a novice Java programmer with regards to GUI and LayoutManager that I can't even get one JPanel inside another JPanel. If anyone has any ideas or suggestions for the layout I would gladly accept them.

Here is a crude sample:

//Test (c) 2010

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SpringLayout;

public class Test
{
public static JFrame aJFrame;
public static JPanel aJPanelFirst;
public static JPanel aJPanelSecond;

public Test( )
{
SpringLayout cSpringLayout = new SpringLayout( );

this.aJFrame = new JFrame( );
this.aJFrame.setSize( 800, 600 );
this.aJFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
this.aJFrame.setLayout( cSpringLayout );

this.aJPanelFirst = new JPanel( );
this.aJPanelFirst.setLayout( cSpringLayout );
this.aJPanelFirst.setBackground( Color.BLACK );

JScrollPane cJScrollPaneFirst = new JScrollPane( this.aJPanelFirst );
cSpringLayout.putConstraint( SpringLayout.NORTH, cJScrollPaneFirst, 10, SpringLayout.NORTH, this.aJFrame.getContentPane( ) );
cSpringLayout.putConstraint( SpringLayout.EAST, cJScrollPaneFirst, -10, SpringLayout.EAST, this.aJFrame.getContentPane( ) );
cSpringLayout.putConstraint( SpringLayout.SOUTH, cJScrollPaneFirst, -10, SpringLayout.SOUTH, this.aJFrame.getContentPane( ) );
cSpringLayout.putConstraint( SpringLayout.WEST, cJScrollPaneFirst, 10, SpringLayout.WEST, this.aJFrame.getContentPane( ) );
this.aJFrame.getContentPane( ).add( cJScrollPaneFirst );

JButton[] cJButton = new JButton[50];
for ( int i = 0; i < 50; ++i )
{
cJButton[i] = new JButton( "Button " + i );
cJButton[i].setPreferredSize( new Dimension( 100, 26 ) );
if ( i == 0 )
{
cSpringLayout.putConstraint( SpringLayout.NORTH, cJButton[i], 0, SpringLayout.NORTH, this.aJPanelFirst );
cSpringLayout.putConstraint( SpringLayout.WEST, cJButton[i], 0, SpringLayout.WEST, this.aJPanelFirst );
}
else
{
cSpringLayout.putConstraint( SpringLayout.NORTH, cJButton[i], 0, SpringLayout.SOUTH, cJButton[i-1] );
cSpringLayout.putConstraint( SpringLayout.WEST, cJButton[i], 0, SpringLayout.WEST, this.aJPanelFirst );
}
this.aJPanelFirst.add( cJButton[i] );
this.aJPanelFirst.setPreferredSize( new Dimension( this.aJPanelFirst.getSize( ).width, 26 * ( i + 1 ) ) );
}

this.aJPanelSecond = new JPanel( );
this.aJPanelSecond.setLayout( cSpringLayout );
this.aJPanelSecond.setBackground( Color.WHITE );

JScrollPane cJScrollPaneSecond = new JScrollPane( this.aJPanelSecond );
cSpringLayout.putConstraint( SpringLayout.NORTH, cJScrollPaneSecond, 0, SpringLayout.NORTH, this.aJPanelFirst );
cSpringLayout.putConstraint( SpringLayout.EAST, cJScrollPaneSecond, 0, SpringLayout.EAST, this.aJPanelFirst );
cSpringLayout.putConstraint( SpringLayout.SOUTH, cJScrollPaneSecond, 0, SpringLayout.SOUTH, this.aJPanelFirst );
cSpringLayout.putConstraint( SpringLayout.WEST, cJScrollPaneSecond, 100, SpringLayout.EAST, this.aJPanelFirst );
this.aJPanelFirst.add( cJScrollPaneSecond );

JTextPane[] cJTextPane = new JTextPane[50];
for ( int i = 0; i < 50; ++i )
{
cJTextPane[i] = new JTextPane( );
cJTextPane[i].setBackground( Color.WHITE );
cJTextPane[i].setForeground( Color.BLACK );
cJTextPane[i].setText( "This is default text" );
cJTextPane[i].setPreferredSize( new Dimension( 400, 26 ) );
if ( i == 0 )
{
cSpringLayout.putConstraint( SpringLayout.NORTH, cJTextPane[i], 0, SpringLayout.NORTH, this.aJPanelSecond );
cSpringLayout.putConstraint( SpringLayout.WEST, cJTextPane[i], 0, SpringLayout.WEST, this.aJPanelSecond );
}
else
{
cSpringLayout.putConstraint( SpringLayout.NORTH, cJTextPane[i], 0, SpringLayout.NORTH, cJTextPane[i-1] );
cSpringLayout.putConstraint( SpringLayout.WEST, cJTextPane[i], 0, SpringLayout.WEST, this.aJPanelSecond );
}
this.aJPanelSecond.add( cJTextPane[i] );
this.aJPanelSecond.setPreferredSize( new Dimension( 400, 26 * ( i + 1 ) ) );
}

this.aJFrame.setVisible( true );
}

public static void main( String[] a )
{
new Test( );
}
}
 

Last edited by t1m1976; 11-07-2010 at 08:17 PM..
Reply With Quote
 
 
 
 
t1m1976 t1m1976 is offline
Junior Member
Join Date: Nov 2010
Posts: 2
 
      11-07-2010
This project is pretty fun to work on so I couldn't help making a better test. I did finally get the horizontal and vertical scrollBars to do what I wanted. Now the only issue I'm concerned with is keeping that horizontal scrollBar visible at the bottom of the JFrame no matter were the vertical scrollBar is positioned.

Anyways, I seem to have solved my own issue. I will leave this thread here in hopes that it helps someone else, or in hopes that someone offers me some better advice in the future for these types of layouts.

Humble Regards,
t1m1976





//Test (c) 2010

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SpringLayout;

public class Test
{
public static SpringLayout aSpringLayout;
public static JFrame aJFrame;

public static JPanel aJPanelFirst;
public static JScrollPane aJScrollPaneFirst;

public static JPanel aJPanelSecond;
public static JScrollPane aJScrollPaneSecond;

public Test( )
{
this.aSpringLayout = new SpringLayout( );

this.aJFrame = new JFrame( "Test" );
this.aJFrame.setSize( 800, 600 );
this.aJFrame.setLayout( this.aSpringLayout );
this.aJFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

this.aJPanelFirst = new JPanel( this.aSpringLayout );
this.aJPanelFirst.setBackground( Color.BLUE );
this.aJScrollPaneFirst = new JScrollPane( this.aJPanelFirst );
this.aJScrollPaneFirst.setHorizontalScrollBarPolic y( JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
this.aSpringLayout.putConstraint( SpringLayout.NORTH, this.aJScrollPaneFirst, 0, SpringLayout.NORTH, this.aJFrame.getContentPane( ) );
this.aSpringLayout.putConstraint( SpringLayout.EAST, this.aJScrollPaneFirst, 0, SpringLayout.EAST, this.aJFrame.getContentPane( ) );
this.aSpringLayout.putConstraint( SpringLayout.SOUTH, this.aJScrollPaneFirst, 0, SpringLayout.SOUTH, this.aJFrame.getContentPane( ) );
this.aSpringLayout.putConstraint( SpringLayout.WEST, this.aJScrollPaneFirst, 0, SpringLayout.WEST, this.aJFrame.getContentPane( ) );
this.aJFrame.getContentPane( ).add( this.aJScrollPaneFirst );

this.aJPanelSecond = new JPanel( this.aSpringLayout );
this.aJPanelSecond.setBackground( Color.RED );
this.aJScrollPaneSecond = new JScrollPane( this.aJPanelSecond );
this.aJScrollPaneSecond.setVerticalScrollBarPolicy ( JScrollPane.VERTICAL_SCROLLBAR_NEVER );
this.aSpringLayout.putConstraint( SpringLayout.NORTH, this.aJScrollPaneSecond, 0, SpringLayout.NORTH, this.aJPanelFirst );
this.aSpringLayout.putConstraint( SpringLayout.EAST, this.aJScrollPaneSecond, 0, SpringLayout.EAST, this.aJPanelFirst );
this.aSpringLayout.putConstraint( SpringLayout.SOUTH, this.aJScrollPaneSecond, 0, SpringLayout.SOUTH, this.aJPanelFirst );
this.aSpringLayout.putConstraint( SpringLayout.WEST, this.aJScrollPaneSecond, 100 , SpringLayout.WEST, this.aJPanelFirst );
this.aJPanelFirst.add( this.aJScrollPaneSecond );

this.aJFrame.setVisible( true );
}

public static void widgets( )
{
JButton[] cJButton = new JButton[50];
for ( int i = 0; i < 50; ++i )
{
cJButton[i] = new JButton( "Button " + i );
cJButton[i].setPreferredSize( new Dimension( 100, 26 ) );
if ( i == 0 )
{
Test.aSpringLayout.putConstraint( SpringLayout.NORTH, cJButton[i], 0, SpringLayout.NORTH, Test.aJPanelFirst );
Test.aSpringLayout.putConstraint( SpringLayout.WEST, cJButton[i], 0, SpringLayout.WEST, Test.aJPanelFirst );
}
else
{
Test.aSpringLayout.putConstraint( SpringLayout.NORTH, cJButton[i], 0, SpringLayout.SOUTH, cJButton[i-1] );
Test.aSpringLayout.putConstraint( SpringLayout.WEST, cJButton[i], 0, SpringLayout.WEST, Test.aJPanelFirst );
}
Test.aJPanelFirst.add( cJButton[i] );
Test.aJPanelFirst.setPreferredSize( new Dimension( Test.aJPanelFirst.getSize( ).width, 26 * i + 1 ) );
}

JTextPane[] cJTextPane = new JTextPane[50];
for ( int i = 0; i < 50; ++i )
{
cJTextPane[i] = new JTextPane( );
cJTextPane[i].setPreferredSize( new Dimension( 2048, 26 ) );
if ( i == 0 )
{
Test.aSpringLayout.putConstraint( SpringLayout.NORTH, cJTextPane[i], 0, SpringLayout.NORTH, Test.aJPanelSecond );
Test.aSpringLayout.putConstraint( SpringLayout.WEST, cJTextPane[i], 0, SpringLayout.WEST, Test.aJPanelSecond );
}
else
{
Test.aSpringLayout.putConstraint( SpringLayout.NORTH, cJTextPane[i], 0, SpringLayout.SOUTH, cJTextPane[i-1] );
Test.aSpringLayout.putConstraint( SpringLayout.WEST, cJTextPane[i], 0, SpringLayout.WEST, Test.aJPanelSecond );
}
Test.aJPanelSecond.add( cJTextPane[i] );
Test.aJPanelSecond.setPreferredSize( new Dimension( 2048, Test.aJPanelFirst.getSize( ).height ) );
}
}

public static void main( String[] a )
{
new Test( );
Test.widgets( );
}
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How Can I Get One JFrame To Appear Above Another JFrame clusardi2k@aol.com Java 2 08-27-2012 03:01 PM
start a new JFrame from an existing one, and when old JFrame closes new one does not jakester Java 3 04-02-2007 11:13 PM
Jpanel, jframe & jscrollpane related problems ludaekipa Java 2 05-04-2004 08:03 PM
get the value of the position of JPanel inside the JFrame Frédéric Colombero Java 0 11-05-2003 07:25 AM
Re: Invisible JPanel within parent JFrame Roedy Green Java 0 07-30-2003 10:58 PM



Advertisments