How to make flowlayout available in its caller?

Dear friends:

Good weekends.

I met a problem when I tried to call a Flowlayout Program from another program.

I have a Flowlayout Program called FlowLayoutPane.java, it can display 20 buttons in FlowLayout, run successes, I write another program called FlowLayoutPaneCall.java to call FlowLayoutPane, these 20 icons display also, but not what I want, I need these 20 buttons display in FlowLayoutPaneCall exactly as same as they display in FlowLayoutPane.java, How can I do that?

[1]. FlowLayoutPane.java

import java.awt.BorderLayout;

import java.awt.FlowLayout;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

publicclass FlowLayoutPaneextends JPanel{

public FlowLayoutPane(){

this.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));

String spaces ="";

for (int i = 1; i <= 20; i++){

this.add(new JButton("Button #" + i + spaces));

spaces +=" ";

}

}

publicstaticvoid main(String[] args){

JFrame frame =new JFrame();

frame.addWindowListener(new WindowAdapter(){

publicvoid windowClosing(WindowEvent e){

System.exit(0);

}

});

frame.getContentPane().add(new FlowLayoutPane(), BorderLayout.CENTER);

frame.setSize(600, 400);

frame.setVisible(true);

}

}

[2]. FlowLayoutPaneCall.java

import java.awt.*;

import javax.swing.*;

publicclass FlowLayoutPaneCallextends JPanel

{

FlowLayoutPane sftp =new FlowLayoutPane();

public FlowLayoutPaneCall()

{

add(sftp);

}

publicstaticvoid main(String[] args){

FlowLayoutPaneCall dm =new FlowLayoutPaneCall();

JFrame f =new JFrame();

f.setSize(400,400);

f.setLocation(200,200);

f.setVisible(true);

f.getContentPane().add(dm, BorderLayout.CENTER);

f.pack();

f.setVisible(true);

}

}

Thanks so much!!!

[3892 byte] By [sunnymanmana] at [2007-11-27 8:38:24]
# 1

import java.awt.*;

import javax.swing.*;

public class FLPCall extends JPanel {

FLP sftp = new FLP();

public FLPCall() {

super(new BorderLayout());

add(sftp);

}

public static void main(String[] args) {

FLPCall dm = new FLPCall();

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(dm, BorderLayout.CENTER);

f.setSize(400,400);

f.setLocation(200,200);

//f.pack();

f.setVisible(true);

}

}

crwooda at 2007-7-12 20:36:04 > top of Java-index,Desktop,Core GUI APIs...
# 2

it works, thanks, but If I add following code to add one more panel etc, It still not work, see below,

after call, the layout of 20 buttons in main program

is different from the one of original, how can I keep them same for this new updating?

Thanks

import java.awt.*;

import javax.swing.*;

public class FLPCall extends JPanel {

FlowLayoutPane sftp = new FlowLayoutPane();

public FLPCall() {

super(new BorderLayout());

JLabel jl =new JLabel("My New Test Start here");

JPanel panel =new JPanel();

panel.add(sftp);

add(jl, BorderLayout.NORTH);

add(panel, BorderLayout.CENTER);

}

public static void main(String[] args) {

FLPCall dm = new FLPCall();

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(dm, BorderLayout.CENTER);

f.setSize(400,400);

f.setLocation(200,200);

//f.pack();

f.setVisible(true);

}

}

sunnymanmana at 2007-7-12 20:36:04 > top of Java-index,Desktop,Core GUI APIs...
# 3

I highly recommend that you nest panels within panels using different layouts in each panel as necessary to acheive the layout you desire.

I guess I can see the need to keep it flowlayout if you want to allow the user to resize the window and allow the buttons to wrap to a new line (or unwrap) as the need arises.

So I guess it boils down to, exactly what do you need -- and I don't mean layout, but rather what functionality do you need -- and why do you need it?

Message was edited by:

petes1234

petes1234a at 2007-7-12 20:36:04 > top of Java-index,Desktop,Core GUI APIs...
# 4

FlowLayout is weak in certain respects. It isn't very smart about reporting a proper/useful

size under certain conditions such as how you are using it here (loading it into a new

FlowLayout). Another case that it has trouble with is when not all of its child components

are inside the parent bounds. So, as petes1234 is suggesting, you may have to try a

different approach.

public FLPCall() {

super(new BorderLayout());

JLabel jl =new JLabel("My New Test Start here");

JPanel panel = new JPanel(new BorderLayout()); // try either this

panel.add(sftp);

add(jl, BorderLayout.NORTH);

add(//panel, BorderLayout.CENTER);

sftp, BorderLayout.CENTER);// or this

}

crwooda at 2007-7-12 20:36:04 > top of Java-index,Desktop,Core GUI APIs...