GridLayout : Why this happens?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kalar
    New Member
    • Aug 2007
    • 82

    GridLayout : Why this happens?

    I have got a JLabel : label1 with an image.Then i add a GridLayout on the Label. Then i make a new label: label2 and i add it in the label1.Then i make a new label:label3 and when i add it goes down from the label2 and not near.Why this happens? Look and my pictures(especi ally the second)

    Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class GridLayoutDemo1 extends JFrame
    {
        public static final int WIDTH = 468;
        public static final int HEIGHT = 468;
    
        public static void main(String[] args)
        {
            GridLayoutDemo1 gui1 = new GridLayoutDemo1();
            gui1.setVisible(true);
        }
    
        public GridLayoutDemo1()
        {
            setSize(WIDTH, HEIGHT);
            addWindowListener(new WindowDestroyer());
    
            Container content = getContentPane();
    
           ImageIcon icon1 = new ImageIcon("tes.jpg");
    
            JLabel label1 = new JLabel(icon1);
            
            content.add(label1);
            label1.setLayout(new GridLayout(10, 10));
    
            JLabel label2 = new JLabel("First");
            label1.add(label2);
            JLabel label3 = new JLabel("Second");
            label1.add(label3);
    
        }
    }

    first picture with only label1

  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    The first part of the specs for GridLayout has a detailed explanation for this.

    Comment

    • kalar
      New Member
      • Aug 2007
      • 82

      #3
      Thanks !!!!
      If i understand good the number of columns is ignored.
      So i must fill the hole gridLayout whith 100 Labels. Then i can put what i want in any position i.e. in 99 . Am i right?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by kalar
        Code:
                JLabel label1 = new JLabel(icon1);
                
                content.add(label1);
                label1.setLayout(new GridLayout(10, 10));
        You are using a label as if it were a regular Container. Officially it is (because it
        inherits from that class) but don't use it as such: the way it is intended to be
        used is to display a single String and/or an Icon; on top of each other or next
        to each other. Use a JPanel instead that uses that GridLayout.

        kind regards,

        Jos

        Comment

        • kalar
          New Member
          • Aug 2007
          • 82

          #5
          Originally posted by JosAH
          You are using a label as if it were a regular Container. Officially it is (because it
          inherits from that class) but don't use it as such: the way it is intended to be
          used is to display a single String and/or an Icon; on top of each other or next
          to each other. Use a JPanel instead that uses that GridLayout.

          kind regards,

          Jos
          yes but i don't know how to insert an icon on JPanel(without draw it)? do you know?
          I want first have a picture and over it put other smaller pictures(icons)

          Comment

          Working...