Results 1 to 7 of 7
  1. #1
    hei1233212000 is offline Member
    Join Date
    Jul 2010
    Posts
    36
    Rep Power
    0

    Default Question about Layout

    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.

    Java Code:
    ......
    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]
    The following is the code about placing the JLabels onto the panel
    labelInWorld[ ][ ] is a 2D-array of JLabel which is initialized already
    The size of each JLabel is 50x50 which is set already.
    Java 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]
    When N is large, the JPanel cannot display all the JLabels. The scrollbar should be appeared but it didn't. What is the problem?
    Is the problem about using "null" layout?

    Problem 2:
    Besides using “null” layout, what layout manager I can use?

    Thank you.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    GridBagLayout and set the borders of the JLabels to an EmptyBorder of size 0.

  3. #3
    hei1233212000 is offline Member
    Join Date
    Jul 2010
    Posts
    36
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    GridBagLayout and set the borders of the JLabels to an EmptyBorder of size 0.
    What is the purpose of setting the borders of the JLabels to an EmptyBorder of size 0? is it used to fixed the size of JLabel?

    Thank you for your reply.

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    It used to reduce the size of the label (since you say you want no space between them I would assume those 10 pixels they leave between the message and the edge of the label to also be too much).

  5. #5
    hei1233212000 is offline Member
    Join Date
    Jul 2010
    Posts
    36
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    It used to reduce the size of the label (since you say you want no space between them I would assume those 10 pixels they leave between the message and the edge of the label to also be too much).
    Thank you and I am trying to use the GridBagLayout.
    And, do you know the what's wrong in problem 1?

    I guess that the JScrollPane cannot be used without layout manager, is that correct?

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Don't know. Don't care. I do not use null layout. It is the "I give up" layout manager and leads to brittle, non-platform-independent GUIs.

  7. #7
    hei1233212000 is offline Member
    Join Date
    Jul 2010
    Posts
    36
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    Don't know. Don't care. I do not use null layout. It is the "I give up" layout manager and leads to brittle, non-platform-independent GUIs.
    Thank you.

Similar Threads

  1. Please Help me to do a layout
    By neway003 in forum AWT / Swing
    Replies: 3
    Last Post: 11-03-2010, 06:28 PM
  2. looking for a layout
    By imorio in forum AWT / Swing
    Replies: 3
    Last Post: 08-13-2010, 05:02 PM
  3. Question Card Layout, Card Management
    By lrichil in forum AWT / Swing
    Replies: 1
    Last Post: 04-22-2010, 10:11 AM
  4. Layout Question
    By vittoire in forum New To Java
    Replies: 6
    Last Post: 01-19-2010, 02:11 PM
  5. Edit layout Layout please help me
    By manhtungtnk28@gmail.com in forum New To Java
    Replies: 4
    Last Post: 11-23-2009, 08:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •