Hi,
I have a JPanel which is having many components than it can display without scrolling. So I added a scrollpane to this panel and it worked well.
But my requirement later changed and the new requirement was that, by default this panel (which has many components) should be invisible and there should be a checkbox provision to make it visible as well as invisible.
I can make the ScrollPane visible and invisible using the check box when I initially show the scrollpane (which the panel with many components). But when I hide the scrollpane before showing it on screen, this check box seem to have no effect whatsoever.
I made a working demo of my problem and I can recreate my problem even with this sample one. I have posted the code below. I am sure that I am doing something wrong and the bug is not in Java but in my code. But I also have a little doubt that whether its a bug in Java, but I am really clueless.
Can anyone help me solving this???
Thank you and here's a sample code that has the problem...
package com.verifone.vfiti.configurator.deviceconfiguration;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class TryJScrollPane implements ActionListener {
JFrame frame = new JFrame();
private JCheckBox cbHide;
private JScrollPane scrollPane;
public TryJScrollPane() {
Container container = frame.getContentPane();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridBagLayout());
for(int i = 0; i < 100; i++) {
addComponentPair(panel, new JTextField("" + i, 10), new JTextField(10));
}
container.add(cbHide = new JCheckBox("Hide"), BorderLayout.NORTH);
cbHide.addActionListener(this);
container.add(scrollPane = new JScrollPane(panel), BorderLayout.CENTER);
//cbHide.doClick(); // When this line is commented
// the code works, but I don't
// want the panel to be visible
// upfront.
frame.setSize(300, 300);
frame.setVisible(true);
}
protected void addComponentPair(JPanel panelGridBag, Component component1, Component component2) {
int row = panelGridBag.getComponentCount() / 2;
// Thanks to KPSeal who created this method once for me.
GridBagConstraints gbc1 = new GridBagConstraints(0, row, 1, 1, 0, 0,
GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0);
GridBagConstraints gbc2 = new GridBagConstraints(1, row, 1, 1, 0, 0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0);
panelGridBag.add(component1, gbc1);
panelGridBag.add(component2, gbc2);
}
public static void main(String[] args) {
new TryJScrollPane();
}
public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
if(source == cbHide) {
if(cbHide.isSelected()) {
scrollPane.setVisible(false);
} else {
scrollPane.setVisible(true);
}
}
}
}