I want to place JLabels as a NxN grids at the upper-left corncer of a JPanel.
And, I want there is no gap between components (JLabels)
I faced few problems
Problem 1:
I have tried use “null” layout and set the location manually that I did what I want. However, it doesn’t work when I add the JPanel onto JScrollPane.
The following is the code about placing the JLabels onto the panelCode:......
JPanel panel = new JPanel();
JPanel paneMain = new JPanel();
panel.setLayout( null ); [COLOR="Lime"]// no layout manager[/COLOR]
panelCenter.setLayout( new BorderLayout() ); [COLOR="Lime"]// use BorderLayout[/COLOR]
.......[COLOR="Lime"]// create others components[/COLOR]
[COLOR="Lime"]// add the panel onto a JScrollPane[/COLOR]
JScrollPane scrollPane = new JScrollPane( panel );
paneMain.add( scrollPane, BorderLayout.CENTER );
......[COLOR="Lime"]// add other components onto the paneMain[/COLOR]
[COLOR="Lime"]// add the paneMain onto the frame[/COLOR]
this.add( panelCenter, BorderLayout.CENTER );
......[COLOR="Lime"]// add other JPanels onto the frame[/COLOR]
labelInWorld[ ][ ] is a 2D-array of JLabel which is initialized already
The size of each JLabel is 50x50 which is set already.
When N is large, the JPanel cannot display all the JLabels. The scrollbar should be appeared but it didn't. What is the problem?Code:
for( int i = 0; i < N; i++ )
{
for( int j = 0; j < N; j++ )
{
labelInWorld[ i ][ j ].setLocation( j*50, i* 50 );
panel.add( labelInWorld[ i ][ j ] );
} [COLOR="Lime"]// end 2nd for loop[/COLOR]
} [COLOR="Lime"]// end 1st for loop[/COLOR]
Is the problem about using "null" layout?
Problem 2:
Besides using “null” layout, what layout manager I can use?
Thank you.

