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( );
}
}
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: