#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    55
    Rep Power
    9

    Need help with Scrollbars


    Hello everybody... i need your help once again....

    My problem is that i have to make this grid of labels, so i made a Jframe and a JPanel. The JFrame contains the JPanel and in the JPanel i made the grid of labels.

    Everything works fine, but when i display de grid there is not enough space on the window to show it, so I have to make a JScrollPane, but i've been trying to, and i can't make it work, so if someone can tell me how to do it, i will appreciate it.

    Here's the code of the panel i got....

    Code:
    public class Panel extends JPanel {
    
        static JLabel[][] label = new JLabel[31][31];
        ImageIcon imagen1=new ImageIcon("image.gif");
    
    
      public Panel(){
    
            JPanel panel = new JPanel();
            JScrollPane scrollpane = new JScrollPane(panel);
            this.setLayout(null);
    	this.setSize(975,925);
    	this.setBackground(Color.RED);
    
    	for(int i=0;i<31;i++){
    		for(int j=0;j<31;j++){
    
                        label[i][j] = new JLabel() ;
                        this.add(label[i][j]);
                        label[i][j].setBounds(j*25+25, i*25+25, 25, 25);
                        label[i][j].setVisible(true);
                        label[i][j].setText("si");
                        label[i][j].setIcon(imagen1);
                }
            }
      }
    }
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2007
    Posts
    1,939
    Rep Power
    3122
    I think you are going about this the wrong way. Have you thought about just adding the labels to a GridLayout instead?

    Comments on this post

    • Hugh of Borg agrees
    • codeJ agrees
  4. #3
  5. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2004
    Location
    Switzerland
    Posts
    1,152
    Rep Power
    1906
    Don't position components manually. Java has those beautiful Layout Managers for a reason.

    Comments on this post

    • codeJ agrees
    - Hugh of Borg

    The first thing young borg are taught: Keep away from Microsoft software!

IMN logo majestic logo threadwatch logo seochat tools logo