Hi,
I have a problem with the JScrollPane.
When adding some components to a panel inside a JScrollPane whereby most components are invisible at the start and made visible by user actions, the scrollbars disappear when making components visible.
More precisely, I always resize the whole frame when making a component visible. If the frame is too big to fit on the screen, they first appear, but when making one more component visible, they disappear again.
Very strange is also, that when I minimize the dialog and then maximize it again, they reappear again. This sounds like a bug for me...
Any help would be appreciated. BTW, I'm using Java 1.6.0.
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
public class ScrollBarTest
{
public static void main(String[] args)
{
final JFrame frame = new JFrame("ScrollBar Test");
frame.setLayout(new BorderLayout());
Font font = new Font(Font.DIALOG, Font.PLAIN, 80);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints cons = new GridBagConstraints();
cons.gridwidth = GridBagConstraints.REMAINDER;
final List<JLabel> labels = new ArrayList<JLabel>(20);
for (int i = 0; i < 20; i++)
{
JLabel label = new JLabel("Label " + i);
label.setFont(font);
labels.add(label);
if (i > 0)
label.setVisible(false);
panel.add(label, cons);
}
frame.add(new JScrollPane(panel), BorderLayout.CENTER);
JToolBar toolbar = new JToolBar();
JButton button = new JButton("Klick");
button.addActionListener(new ActionListener()
{
int firstInvisible = 1;
@Override
public void actionPerformed(ActionEvent e)
{
labels.get(firstInvisible).setVisible(true);
firstInvisible++;
frame.pack();
}
});
toolbar.add(button);
frame.add(toolbar, BorderLayout.NORTH);
frame.pack();
frame.setVisible(true);
}
}