Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Flowlayout inside a JScrollPane, components don't go onto new line

797060Jan 14 2011 — edited Jan 14 2011
I have a situation where I have a load of buttons and a textfield (with label). When the user has the window maximized I want the buttons all in a line to save vertical space, however if the user shrinks the width of the window so the buttons no longer fit, I would like them to do the default Flowlayout, and go onto annother line. This works fine, except I need scroll pane for when the window gets so small as to start obscuring the text field.

I think this diagram shows what I need better than I can explain:

http://img441.imageshack.us/img441/1460/54793673.png

Here is an example showing my "problem"
import java.awt.*;
import javax.swing.*;
 
public class Test {
    public static void main(String[] args) {
    	JFrame f = new JFrame();
    	
    	JPanel parentPanel = new JPanel();
    	parentPanel.setLayout(new GridLayout(2,1));
    	
    	JPanel buttonPanel = new JPanel();
    	buttonPanel.add(new JButton("some"));
    	buttonPanel.add(new JButton("large"));
    	buttonPanel.add(new JButton("ammounts"));
    	buttonPanel.add(new JButton("of"));
    	buttonPanel.add(new JButton("buttons"));
    	buttonPanel.add(new JButton("!!!"));
    	
    	parentPanel.add(new JLabel("some short label, but still needs to be seen"));
    	
    	parentPanel.add(buttonPanel);

        //if the scroll pane isnt there it almost does what i want
    	f.add(new JScrollPane(parentPanel));
    	
    	f.pack();
    	f.setVisible(true);
    }
}
Thanks for any help
This post has been answered by camickr on Jan 14 2011
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 11 2011
Added on Jan 14 2011
2 comments
1,165 views