JPanel inside JPanel inside JFrame

Discussion in 'Java' started by t1m1976, Nov 7, 2010.

  1. t1m1976

    t1m1976

    Joined:
    Nov 7, 2010
    Messages:
    2
    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 = new JButton( "Button " + i );
    cJButton.setPreferredSize( new Dimension( 100, 26 ) );
    if ( i == 0 )
    {
    cSpringLayout.putConstraint( SpringLayout.NORTH, cJButton, 0, SpringLayout.NORTH, this.aJPanelFirst );
    cSpringLayout.putConstraint( SpringLayout.WEST, cJButton, 0, SpringLayout.WEST, this.aJPanelFirst );
    }
    else
    {
    cSpringLayout.putConstraint( SpringLayout.NORTH, cJButton, 0, SpringLayout.SOUTH, cJButton[i-1] );
    cSpringLayout.putConstraint( SpringLayout.WEST, cJButton, 0, SpringLayout.WEST, this.aJPanelFirst );
    }
    this.aJPanelFirst.add( cJButton );
    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 = new JTextPane( );
    cJTextPane.setBackground( Color.WHITE );
    cJTextPane.setForeground( Color.BLACK );
    cJTextPane.setText( "This is default text" );
    cJTextPane.setPreferredSize( new Dimension( 400, 26 ) );
    if ( i == 0 )
    {
    cSpringLayout.putConstraint( SpringLayout.NORTH, cJTextPane, 0, SpringLayout.NORTH, this.aJPanelSecond );
    cSpringLayout.putConstraint( SpringLayout.WEST, cJTextPane, 0, SpringLayout.WEST, this.aJPanelSecond );
    }
    else
    {
    cSpringLayout.putConstraint( SpringLayout.NORTH, cJTextPane, 0, SpringLayout.NORTH, cJTextPane[i-1] );
    cSpringLayout.putConstraint( SpringLayout.WEST, cJTextPane, 0, SpringLayout.WEST, this.aJPanelSecond );
    }
    this.aJPanelSecond.add( cJTextPane );
    this.aJPanelSecond.setPreferredSize( new Dimension( 400, 26 * ( i + 1 ) ) );
    }

    this.aJFrame.setVisible( true );
    }

    public static void main( String[] a )
    {
    new Test( );
    }
    }
    Last edited: Nov 7, 2010
    t1m1976, Nov 7, 2010
    #1
    1. Advertising

  2. t1m1976

    t1m1976

    Joined:
    Nov 7, 2010
    Messages:
    2
    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.setHorizontalScrollBarPolicy( 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 = new JButton( "Button " + i );
    cJButton.setPreferredSize( new Dimension( 100, 26 ) );
    if ( i == 0 )
    {
    Test.aSpringLayout.putConstraint( SpringLayout.NORTH, cJButton, 0, SpringLayout.NORTH, Test.aJPanelFirst );
    Test.aSpringLayout.putConstraint( SpringLayout.WEST, cJButton, 0, SpringLayout.WEST, Test.aJPanelFirst );
    }
    else
    {
    Test.aSpringLayout.putConstraint( SpringLayout.NORTH, cJButton, 0, SpringLayout.SOUTH, cJButton[i-1] );
    Test.aSpringLayout.putConstraint( SpringLayout.WEST, cJButton, 0, SpringLayout.WEST, Test.aJPanelFirst );
    }
    Test.aJPanelFirst.add( cJButton );
    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 = new JTextPane( );
    cJTextPane.setPreferredSize( new Dimension( 2048, 26 ) );
    if ( i == 0 )
    {
    Test.aSpringLayout.putConstraint( SpringLayout.NORTH, cJTextPane, 0, SpringLayout.NORTH, Test.aJPanelSecond );
    Test.aSpringLayout.putConstraint( SpringLayout.WEST, cJTextPane, 0, SpringLayout.WEST, Test.aJPanelSecond );
    }
    else
    {
    Test.aSpringLayout.putConstraint( SpringLayout.NORTH, cJTextPane, 0, SpringLayout.SOUTH, cJTextPane[i-1] );
    Test.aSpringLayout.putConstraint( SpringLayout.WEST, cJTextPane, 0, SpringLayout.WEST, Test.aJPanelSecond );
    }
    Test.aJPanelSecond.add( cJTextPane );
    Test.aJPanelSecond.setPreferredSize( new Dimension( 2048, Test.aJPanelFirst.getSize( ).height ) );
    }
    }

    public static void main( String[] a )
    {
    new Test( );
    Test.widgets( );
    }
    }
    t1m1976, Nov 7, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roedy Green
    Replies:
    0
    Views:
    3,041
    Roedy Green
    Jul 30, 2003
  2. Frédéric Colombero
    Replies:
    0
    Views:
    1,081
    Frédéric Colombero
    Nov 5, 2003
  3. ludaekipa
    Replies:
    2
    Views:
    2,167
    ludaekipa
    May 4, 2004
  4. jakester
    Replies:
    3
    Views:
    1,803
    jakester
    Apr 3, 2007
  5. clusardi2k@aol.com
    Replies:
    2
    Views:
    400
    markspace
    Aug 27, 2012
Loading...

Share This Page