reconstructing JPanel by an event

Hai there'i have an app , when a button clicked it should add a set of components in row wise in JPanel which is in a JScrollPane.I have problem that JPanel losts the previous components at each click.How could i make the Viewport to show components added previously.
[289 byte] By [HelpGrandixa] at [2007-10-1 22:45:15]
# 1

The choice of layout manager can be crucial.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class AddingComponents extends JPanel

{

GridBagConstraints gbc;

public AddingComponents()

{

setLayout(new GridBagLayout());

gbc = new GridBagConstraints();

gbc.insets = new Insets(5,5,5,5);

gbc.weightx = 1.0;

gbc.weighty = 1.0;

}

private JPanel getUIPanel()

{

JButton add = new JButton("add components");

add.addActionListener(new ActionListener()

{

int count = 0;

public void actionPerformed(ActionEvent e)

{

for(int j = 0; j < 4; j++)

{

if((j + 1) % 4 == 0)

gbc.gridwidth = gbc.REMAINDER;

else

gbc.gridwidth = 1;

add(new JButton("button " + ++count), gbc);

}

revalidate();

}

});

JPanel panel = new JPanel();

panel.add(add);

return panel;

}

public static void main(String[] args)

{

AddingComponents ac = new AddingComponents();

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(ac.getUIPanel(), "North");

f.getContentPane().add(new JScrollPane(ac));

f.setSize(400,400);

f.setLocation(200,200);

f.setVisible(true);

}

}

74philipa at 2007-7-13 14:49:50 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...