1

So i've been trying this for some time now and can't seem to be able to make it work.

Ok so i'm making a catalog in netbeans and working with Java. I dragged in a JPanel from the palettes window and named it "panel". The only problem is I can't make the panel scroll vertically.

I was reading up some questions here and searching in the web and found that I can use JScrollPane in Java the only problem is whenever i try to use this, the panel seems to disappear, and when I don't add it, it works fine, the problem is all of my other elements overflow off the panel.

So here's the code i'm using:

initComponents();

//Just adding some elements in the panel
for(int i = 1; i <= 10; i++){
        //Position elements in a new line so they won't overflow to the right
        if(counter == 7){
            locationTop += 350;
            locationLeft = 90;
            locationTopViewMore += 350;
            locationTopAgregar += 550;
            counter = 0;
        }

        //Adding an image and a couple of buttons
        JLabel myLabel= new JLabel();
        myLabel.setIcon(new ImageIcon("" + save)); //Adding an image
        myLabel.setSize(200, 200);
        myLabel.setLocation(locationLeft, locationTop);
        panel.add(myLabel);

        JButton btnViewMore = new JButton("View More"); //Making a button.
        btnViewMore.setSize(100, 50);
        btnViewMore.setLocation(locationLeft + 50, locationTopViewMore);
        panel.add(btnViewMore);

        JButton btnAgregar = new JButton("Another Button"); //Making another button.
        btnAgregar.setSize(150, 50);
        btnAgregar.setLocation(locationLeft + 25, locationTopAgregar);
        panel.add(btnAdd);

        locationLeft += 200;
        counter++;
}
//JScrollPane scrollPane = new JScrollPane(panel);

So this is what happens when i don't use the JScrollPane:

http://i57.tinypic.com/acel3s.png

And this is what happens when i do use the JScrollPane:

http://i57.tinypic.com/2ltlcwj.png

Any help is greatly appreciated. Thanks!

Brian Moreno
  • 109
  • 10
  • Those `setSize()` and `setLocation()` calls look suspiciously like you'd be using `null` layout. *Don't do that*, it leads to no end of trouble. In this case the issue would be that the scroll pane depends on the preferred size of the view, and determining the preferred size is the job is the layout manager. For example `GridLayout` would be more appropriate. – kiheru May 07 '15 at 06:43

2 Answers2

0

I can't tell by looking at that snippet, but did you add your scrollPane to the Frame? This seems too obvious to miss, but just in case. Additionally, you can only add a swing widget to one parent, that explains why the code to add the panel doesn't work neither, but when you uncommented the scrollPane, everything works.

Kentative
  • 105
  • 4
  • No, i just added a normal panel. I tried using the scrollPane buwhen it ran the nothing gets added into it it's just blank. – Brian Moreno May 07 '15 at 01:32
  • "using the scrollPane" means adding it to the parent container right? Did you try setting the minimum dimension of the scroll pane? – Kentative May 07 '15 at 01:54
0

using a GridLayout might work like this (0,7) means 7 columns and make as many rows as needed

JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(0,7));
JScrollPane scrollPane = new JScrollPane(panel1);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

  for(int i = 1; i <= 10; i++){
      JPanel panel = new JPanel();
          //Position elements in a new line so they won't overflow to the right
          if(counter == 7){
              locationTop += 350;
              locationLeft = 90;
              locationTopViewMore += 350;
              locationTopAgregar += 550;
              counter = 0;
          }

          //Adding an image and a couple of buttons
          JLabel myLabel= new JLabel();
          myLabel.setIcon(new ImageIcon("" + save)); //Adding an image
          myLabel.setSize(200, 200);
          myLabel.setLocation(locationLeft, locationTop);
          panel.add(myLabel);

          JButton btnViewMore = new JButton("View More"); //Making a button.
          btnViewMore.setSize(100, 50);
          btnViewMore.setLocation(locationLeft + 50, locationTopViewMore);
          panel.add(btnViewMore);

          JButton btnAgregar = new JButton("Another Button"); //Making another button.
          btnAgregar.setSize(150, 50);
          btnAgregar.setLocation(locationLeft + 25, locationTopAgregar);
          panel.add(btnAgregar);

          locationLeft += 200;
          counter++;
          panel1.add(panel);

  }
  frame.add(scrollPane);
JRowan
  • 6,824
  • 8
  • 40
  • 59