Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / June 2004

Tip: Looking for answers? Try searching our database.

Flow layout problem

Thread view: 
angussc2 - 25 Jun 2004 17:30 GMT
Hi, I have what I think is a relitavely simple problem:

I have a bigger application with multiple JPanels etc but have scaled it
down for the example below...

When I resize the window so that it is too small to fit all the buttons on
screen horizontally I would like the buttons to re-organise themselves on
screen in the way they would do if they were just directly on the JFrame
rather than in a couple of JPanels.

If possible I would like a reason for this behaviour and a way to make the
buttons react like this:
http://java.sun.com/docs/books/tutorialJWS/uiswing/layout/example-1dot4/FlowLayo
utDemo.jnlp

when they are underneath multiple JPanels.

Thanks
Angus

public class Application extends javax.swing.JFrame {

   /** Creates new form Application */
   public Application() {
       initComponents();
   }

   private void initComponents() {
       jPanel2 = new javax.swing.JPanel();
       jPanel1 = new javax.swing.JPanel();
       jButton1 = new javax.swing.JButton();
       jButton2 = new javax.swing.JButton();
       jButton3 = new javax.swing.JButton();
       jButton4 = new javax.swing.JButton();
       jButton6 = new javax.swing.JButton();
       jButton5 = new javax.swing.JButton();
       jButton7 = new javax.swing.JButton();
       jButton8 = new javax.swing.JButton();
       jButton9 = new javax.swing.JButton();

       setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
       jPanel2.setLayout(new java.awt.BorderLayout());

       jButton1.setText("jButton1");
       jPanel1.add(jButton1);

       jButton2.setText("jButton2");
       jPanel1.add(jButton2);

       jButton3.setText("jButton3");
       jPanel1.add(jButton3);

       jButton4.setText("jButton4");
       jPanel1.add(jButton4);

       jButton6.setText("jButton6");
       jPanel1.add(jButton6);

       jButton5.setText("jButton5");
       jPanel1.add(jButton5);

       jButton7.setText("jButton7");
       jPanel1.add(jButton7);

       jButton8.setText("jButton8");
       jPanel1.add(jButton8);

       jButton9.setText("jButton9");
       jPanel1.add(jButton9);

       jPanel2.add(jPanel1, java.awt.BorderLayout.CENTER);

       getContentPane().add(jPanel2, java.awt.BorderLayout.NORTH);

       pack();
   }

   /**
    * @param args the command line arguments
    */
   public static void main(String args[]) {
       new Application().show();
   }

   // Variables declaration - do not modify
   private javax.swing.JButton jButton1;
   private javax.swing.JButton jButton2;
   private javax.swing.JButton jButton3;
   private javax.swing.JButton jButton4;
   private javax.swing.JButton jButton5;
   private javax.swing.JButton jButton6;
   private javax.swing.JButton jButton7;
   private javax.swing.JButton jButton8;
   private javax.swing.JButton jButton9;
   private javax.swing.JPanel jPanel1;
   private javax.swing.JPanel jPanel2;
   // End of variables declaration

}
VisionSet - 25 Jun 2004 18:42 GMT
> Hi, I have what I think is a relitavely simple problem:
>
[quoted text clipped - 5 lines]
> screen in the way they would do if they were just directly on the JFrame
> rather than in a couple of JPanels.

The default layout manager for JFrame is BorderLayout, a Jpanel's is
FlowLayout
So you simply need to set the layout manager:

myJFrame..getContentPane().setLayout(new FlowLayout());

--
Mike W
angussc2 - 25 Jun 2004 21:54 GMT
> > Hi, I have what I think is a relitavely simple problem:
> >
[quoted text clipped - 14 lines]
> --
> Mike W

Thanks but I don't think you have understood my problem, if you run my code
from my first post you'll see my problem.  I've tried
this.getContentPane().setLayout(new FlowLayout()); but nothing changes.

I don't want to change the layout of the JFrame, I want the layout to work
correctly in the JPanel
VisionSet - 25 Jun 2004 22:37 GMT
> > "angussc2" <a@b.com> wrote in message
> news:2k32aeF16mj7uU1@uni-berlin.de...
[quoted text clipped - 22 lines]
> from my first post you'll see my problem.  I've tried
> this.getContentPane().setLayout(new FlowLayout()); but nothing changes.

Sorry.

> I don't want to change the layout of the JFrame, I want the layout to work
> correctly in the JPanel

Tough, BorderLayout is not designed to work correctly with FlowLayout
containers (Except CENTER).  NORTH for instance can be stretched in width
but the height maintains a preferred height of the child, this will be the
height of one row in FlowLayout. Suggest you use a different
LayoutManager/Component nesting organisation.

--
Mike W
angussc2 - 25 Jun 2004 22:44 GMT
> Tough, BorderLayout is not designed to work correctly with FlowLayout
> containers (Except CENTER).  NORTH for instance can be stretched in width
[quoted text clipped - 4 lines]
> --
> Mike W

Ok, thought that might be the case, I'll probably write my own Layout
Manager

Thanks
Angus


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.