I want to view some information about some objects of my program. For that I am creating a JPanel for every Object with it's information. These panels get packed together in a Panel which I add to a ScrollPane.
Works perfect if there are just a few Objects so all Information can get displayed without scrolling. As soon as there are more, the whole Panel just disappears in a way like it is minimized.
I am sorry that I couldn't extract a small codesnippet to reproduce the mistake. I made it as small as possible. So I have to give you three Classes:
Program: Creates a TreeMap of Objects and works on it. Here it is just initializing a List of Integers.
import java.util.TreeMap;
public class Program {
// Set ObjectNumber here!
static int numberObjects = 32;
static TreeMap<Integer, Integer> objects = new TreeMap<Integer, Integer>();
static TestFrame frame;
public static void main(String[] args) {
init();
}
public static void init() {
for (int i=0; i < numberObjects; i++) {
objects.put(i, i);
}
frame = new TestFrame(objects);
frame.repaint();
}
}
TestFrame: It extends JFrame, sets it self to maximimzed and Border Layout. After that, it adds a JPanel from my class TestPanel to the east.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.util.TreeMap;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.util.TreeMap;
import javax.swing.JFrame;
public class TestFrame extends JFrame {
TreeMap<Integer, Integer> objects;
TestPanel testPanel;
public TestFrame(TreeMap<Integer, Integer> objects) {
this.objects = objects;
setTitle("Program");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setExtendedState(Frame.MAXIMIZED_BOTH);
init();
}
public void init() {
setLocationRelativeTo(null);
setLayout(new BorderLayout());
testPanel = new TestPanel(objects);
this.add(testPanel, BorderLayout.EAST);
setVisible(true);
}
}
TestPanel: It extends JPanel. Goes through all objects and add a label with the integer-value to an array names[]. Then goes for every obect again to build an panel just for this object, add a panel with text "Name:" and the label from the names[]-array. With GridBagLayout. Now it packs all panels of the objects in one panel "innerPanel". This is added to the scrollpane. The scrollpane gets added to the class (TestPanel).
import java.util.TreeMap;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Label;
import javax.swing.BorderFactory;
public class TestPanel extends JPanel {
GridBagConstraints gridBagConstraints = new GridBagConstraints();
TreeMap<Integer, Integer> objects;
Label[] names;
JPanel[] objectPanel;
JPanel innerPanel;
Font fontLabelName = new Font("Arial", Font.BOLD, 20);
public TestPanel (TreeMap<Integer, Integer> objects) {
this.objects = objects;
names = new Label[objects.size()];
objectPanel = new JPanel[objects.size()];
this.setLayout(new GridBagLayout());
for(int i=0; i<objects.size(); i++) {
Label tempLabel = new Label(Integer.toString(objects.get(i)), SwingConstants.LEFT);
tempLabel.setFont(fontLabelName);
names[i]=tempLabel;
}
for (int i=0; i<objects.size(); i++) {
JPanel tempPanel = new JPanel();
tempPanel.setLayout(new GridBagLayout());
objectPanel[i] = tempPanel;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.gridwidth = 2;
gridBagConstraints.gridheight = 1;
Label nameLabel = new Label("Name: ", SwingConstants.LEFT);
nameLabel.setFont(fontLabelName);
objectPanel[i].add(nameLabel, gridBagConstraints);
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 0;
gridBagConstraints.gridwidth = 2;
gridBagConstraints.gridheight = 1;
objectPanel[i].add(names[i], gridBagConstraints);
}
innerPanel = new JPanel();
innerPanel.setLayout(new GridLayout(0,1));
for (int i = 0; i<objects.size(); i++) {
innerPanel.add(objectPanel[i]);
}
JScrollPane scrollPane = new JScrollPane(innerPanel);
// scrollPane.setBorder( BorderFactory.createEmptyBorder() );
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
this.add(scrollPane);
}
}
Works perfectly as seen in the screenshot with my screenresolution for 31 objects, but the scrollpane nearly disappears if i use 32 objects.
Would be really thankfull if anybody could help me solve it or link a matching post - I couldn't find one with google/search.