aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Dynamically adding a panel to a panel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Dynamically adding a panel to a panel" Watch "Dynamically adding a panel to a panel" New topic
Author

Dynamically adding a panel to a panel

Steve Wood
Ranch Hand

Joined: Jan 08, 2003
Posts: 137
Hi,
I'm a bit new to java and swing and have been trying to add a panel dynamically to a panel.
Basically, I want to create a sub-form effect (as used in MS Access for example), so the user can add records to a database using a custom input which repeats for each record. In my case, I have a panel with some dynamic behaviour and I want to have a button which can add this panel to a main panel. Each time the user wants to add a record, another panel is added using the add button... I hope this makes sense.
Anyway, any help would be great as when I create a new panel and add it to the main panel, nothing shows...
All the best,
Steve
ali rafiq
Ranch Hand

Joined: Jan 09, 2003
Posts: 75
panel is like a bag u can add in two panel .. or button .. textfield .. any thing as u want ... u can set panel any where as u want .. there some example

JButton button;
Container contentPane = getContentPane();
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
contentPane.setLayout(gridbag);
c.fill = GridBagConstraints.HORIZONTAL;

button = new JButton("Button 1");
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 0;
gridbag.setConstraints(button, c);
contentPane.add(button);
button = new JButton("2");
c.gridx = 1;
c.gridy = 0;
gridbag.setConstraints(button, c);
contentPane.add(button);
button = new JButton("Button 3");
c.gridx = 2;
c.gridy = 0;
gridbag.setConstraints(button, c);
contentPane.add(button);
button = new JButton("Long-Named Button 4");
c.ipady = 40; //make this component tall
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 1;
gridbag.setConstraints(button, c);
contentPane.add(button);
button = new JButton("Button 5");
c.ipady = 0; //reset to default
c.weighty = 1.0; //request any extra vertical space
c.anchor = GridBagConstraints.SOUTH; //bottom of space
c.insets = new Insets(10,0,0,0); //top padding
c.gridx = 1; //aligned with button 2
c.gridwidth = 2; //2 columns wide
c.gridy = 2; //third row
gridbag.setConstraints(button, c);
contentPane.add(button);
-----------------------------------------------------------
Container contentPane = getContentPane();
contentPane.setLayout(new GridLayout(0,2));
contentPane.add(new JButton("Button 1"));
contentPane.add(new JButton("2"));
contentPane.add(new JButton("Button 3"));
contentPane.add(new JButton("Long-Named Button 4"));
contentPane.add(new JButton("Button 5"));

------------------------------------------------
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(new JButton("Button 1"));
contentPane.add(new JButton("2"));
contentPane.add(new JButton("Button 3"));
contentPane.add(new JButton("Long-Named Button 4"));
contentPane.add(new JButton("Button 5"));
---------------------------------------------------
Container contentPane = getContentPane();
//Use the content pane's default BorderLayout.
//contentPane.setLayout(new BorderLayout()); //unnecessary

contentPane.add(new JButton("Button 1 (NORTH)"),
BorderLayout.NORTH);
contentPane.add(new JButton("2 (CENTER)"),
BorderLayout.CENTER);
contentPane.add(new JButton("Button 3 (WEST)"),
BorderLayout.WEST);
contentPane.add(new JButton("Long-Named Button 4 (SOUTH)"),
BorderLayout.SOUTH);
contentPane.add(new JButton("Button 5 (EAST)"),
BorderLayout.EAST);
--------------------------------------------------------------------------------


if u learn to break the rock .. next day u will be looking to Mountain.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I think the question may have been misunderstood. You could surely clarify your problems by posting some code snippits to illustrate where you are having problems.
Well, I was going to show what I thought you meant, but it seems you may be making things more complicated than you need to. If you are writing an Access-like database tool, I think there are Java components already made that do a lot of the grunt-work for you. You probably should check out JDBC and check to see if Swing has any "data aware" components.
HTH
Layne


Java API Documentation
The Java Tutorial
Steve Wood
Ranch Hand

Joined: Jan 08, 2003
Posts: 137
Hi,
Actually, the previous post is exactly what I needed to see so thanks for the help. Next time I'll put some code with my question as I can imagine it being confusing.
Cheers,
Steve
 
Consider Paul's rocket mass heater.
 
subject: Dynamically adding a panel to a panel