0

I have a JPanel in a JScrollPane. After the Panel and the ScrollPane are created I fill the Panel with JButtons. But I don't get the ScrollPane to work. It just won't scroll my Panel.

Maybe it's something very easy but I'm not this experienced in GUI programming. I've already searched and tried some things I found out there... but I didn't get my GUI to work.

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import java.awt.CardLayout;
import java.util.ArrayList;

import javax.swing.JLabel;

public class GUI extends JFrame {
    private Container pinnwand;

    private JPanel Team = new JPanel();
    private JPanel MonsterListe = new JPanel();

    private JScrollPane cntAvailableMonster = new JScrollPane();
    private ArrayList<JLabel> AvailableMonstersLabels = new ArrayList<JLabel>();


    public GUI() {
        this.pinnwand = getContentPane();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setBounds(100, 100, 530, 443);
        this.pinnwand.setLayout(new CardLayout(0,0));


        this.Team.setBackground(new Color(44,44,44));
        this.Team.setLayout(null);


        this.MonsterListe.setMinimumSize(new Dimension(200,400));
        this.cntAvailableMonster.setBounds(300, 0, 200, 400);
        this.cntAvailableMonster.setPreferredSize(new Dimension(200,1000));
        this.cntAvailableMonster.createVerticalScrollBar();
        this.cntAvailableMonster.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        this.cntAvailableMonster.setViewportView(this.MonsterListe);    
        this.MonsterListe.setLayout(null);


        this.Team.add(this.cntAvailableMonster);


        this.pinnwand.add(this.Team,"Team");

        this.setAvailableMonsters();
    }

    private void setAvailableMonsters() {
        for(int i = 0,amount = 20;i < amount;i++) {
            this.AvailableMonstersLabels.add(new JLabel("monster"+i));
            this.AvailableMonstersLabels.get(i).setBounds(0, i*30, 300, 30);
            this.AvailableMonstersLabels.get(i).setBackground(new Color(255,0,0));
            this.AvailableMonstersLabels.get(i).setOpaque(true);
            this.MonsterListe.add(this.AvailableMonstersLabels.get(i));
            this.MonsterListe.setBackground(new Color(0,0,0));
        }
    }

    public static void main(String[] args) {
        new GUI().setVisible(true);
    }
}

Thanks in advance :)

Sindhara
  • 1,423
  • 13
  • 20
  • btw it is not recommended to use null layout, did you solve your issue? – nachokk Jun 30 '13 at 19:42
  • Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or combinations of them, along with layout padding & borders for white space. – Andrew Thompson Jul 01 '13 at 01:10

1 Answers1

-1

Ah OK, I found the question. I had to set the PreferredSize() to MonsterListe and not to cntAvailableMonster.

Sindhara
  • 1,423
  • 13
  • 20
  • 1
    See also [*Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods*](http://stackoverflow.com/q/7229226/230513)? – trashgod Jun 30 '13 at 21:58