tags:

views:

119

answers:

4

Ok, this will be rather noobish - I'm making a GUI with the Eclipse Visual Editor (a JFrame with stuff in it), basically on the left side there are some buttons and textfields etc. and the right side contains 4 JPanels (with some graphics in them).

The problem is, when I resize the gui (the JFrame) during runtime the components inside stay the same (size wise) and I'd want those 4 JPanels to resize with the JFrame, is it possible?

+1  A: 

Simple answer - use layout management, including nesting multiple panels with different layouts to get the result you want. You've essentially got two choices: (a) use a layout manager to manage component location and sizing, or (b) do it all manually. You really don't want to do (b).

You might want to start with the layout manager tutorial.

Using a table based layout can make layout significantly easier than the standard layout managers distributed with Java; there are many available for free. Mine is at www.SoftwareMonkey.org.

Here's an example (from the help in MatrixLayout, but I don't recall if I ever actually compiled the example code... I expect I did once):

+--------------------------------------------------------------------------------+
+                                                                                |
+     Name : |________________________________________________________________|  |
+                                                                                |
+  Address : |________________________________________________________________|  |
+                                                                                |
+            |________________________________________________________________|  |
+                                                                                |
+            |________________________________________________________________|  |
+                                                                                |
+     City : |____________________| State |__| Zip |_____| - |____|              |
+                                                                                |
+    Phone : |___|-|___|-|____|                                                  |
+                                                                                |
+    Notes : |                              |  |                              |  |
+            |                              |  |                              |  |
+            |                              |  |                              |  |
+            |                              |  |                              |  |
+            |                              |  |                              |  |
+            |______________________________|  |______________________________|  |
+            [BOTTOM-LEFT]                                     [ BOTTOM-RIGHT ]  |
+                                                                                |
+                       [      Yes      ]   [      No      ]   [    Abort     ]  |
+                                                                                |
+--------------------------------------------------------------------------------+
private void createContent(Container main){
    String[]                            rows,cols;                                  // row/column specification arrays

    JPanel                              phnpnl,cszpnl,btnpnl;                       // special nested panels

    // create components here...

    // CREATE MAIN PANEL WITH DESIRED ROWS AND COLUMNS
    rows=MatrixLayout.arrayOf(10,"Size=Pref CellAlign=Middle CellInsets=5,0");   // standard row spec
    rows[6]                     ="Size=100% CellAlign=Top    CellInsets=5,0";    // note: row 7 ([6] is index)
    rows[7]                     ="Size=Pref CellAlign=Top    CellInsets=5,0";    // note: row 8 ([7] is index)
    rows[8]                     ="Size=Pref CellAlign=Top    CellInsets=5,0";    // note: row 9 ([8] is index)
    cols=MatrixLayout.arrayOf(3 ,"size=Pref CellAlign=Right  CellInsets=5,0");   // standard column spec
    cols[1]                     ="Size=50%  CellAlign=Left   CellInsets=5,0";    // note: col 2 ([1] is index)
    cols[2]                     ="Size=50%  CellAlign=Left   CellInsets=5,0";    // note: col 3 ([2] is index)
    con.setLayout(new MatrixLayout(rows,cols,"Row=Cur Col=Next"));

    // CREATE SPECIAL NESTED PANELS
    phnpnl=MatrixLayout.singleRowBar(5,false,new DctComponent[]{phnPart1,phnPart2,phnPart3                                   });
    cszpnl=MatrixLayout.singleRowBar(5,1    ,new DctComponent[]{city,createLabel("State"),state,createLabel("Zip"),zip,zipext});
    btnpnl=MatrixLayout.singleRowBar(5,true ,new DctComponent[]{yes,no,cancel                                                });
    phnpnl.setName("PhonePanel");
    cszpnl.setName("CityStateZipPanel");
    btnpnl.setName("ButtonPanel");

    // ADD COMPONENTS TO MAIN PANEL
    con.add(createLabel(   "Name :"),"row=Next col=1"); con.add(name    ,"               hAlign=Fill  hSpan=2                               ");
    con.add(createLabel("Address :"),"row=Next col=1"); con.add(address1,"               hAlign=Fill  hSpan=2                               ");
                                                        con.add(address2,"Row=Next Col=2 hAlign=Fill  hSpan=2                               ");
                                                        con.add(address3,"Row=Next Col=2 hAlign=Fill  hSpan=2                               ");
    con.add(createLabel(   "City :"),"row=Next col=1"); con.add(cszpnl  ,"                            hSpan=2                               ");
    con.add(createLabel(  "Phone :"),"row=Next col=1"); con.add(phnpnl  ,"                            hSpan=2                               ");
    con.add(createLabel(  "Notes :"),"row=Next col=1"); con.add(notes1  ,"Row=Cur  Col=2 hAlign=Fill          vAlign=Fill                   ");
                                                        con.add(notes2  ,"Row=Cur        hAlign=Fill          vAlign=Fill                   ");
                                                        con.add(notes3  ,"Row=Next Col=2 hAlign=Left                      hGroup=NoteButtons");
                                                        con.add(notes4  ,"Row=Cur        hAlign=Right                     hGroup=NoteButtons");
    con.add(btnpnl                  ,"row=Next col=1 hAlign=Right hSpan=3");
    main.setBorder(new DctEmptyBorder(10));
    main.setBackground(SystemColor.window);
    }
Software Monkey
I know it will sound stupid, but I'm not using a layout manager (because it's easier for me) and I was thinking is there any other way to link those JPanels to the JFrame (I think there was something like that in Qt when I was coding in C++ which I really liked).
Zenzen
Layout management is the way to (effectively) achieve that; but it is a different approach than attaching things to the edges of the frame (I wrote a layout manager some years ago to layout in that manner, but later moved to table-based layout since it's both simpler and more powerful).
Software Monkey
zenzen, your problem is that you aren't using a layout manager. you are not coding c++.
pstanton
+1 For DSL LayoutManager
OscarRyz
A: 

It depends on how you add your components to the frame, and what layout manager you use. in this example, the four buttons will resize with the window.

JFrame frame = new JFrame();
Container cp = frame.getContentPane();
cp.setLayout(new GridLayout(2, 2));
cp.add(new JButton("one"));
cp.add(new JButton("two"));
cp.add(new JButton("three"));
cp.add(new JButton("four"));
frame.pack();
frame.setVisible(true);

you can use many different layout managers however. read this.

pstanton
A: 

Use a layout manager. If you don't use it you'll have to resize it your self ( which would include setting the new location each time )

LayoutManagers seems problematic and actually they are.

In Java 1.6 the GroupLayout manager was added and I think it is quite useful and simple enough.

alt text

The power of the layout managers is released when used in conjunctions with others. Here's a A Visual Guide to Layout Managers.

If you post an screenshot of your application I'm pretty sure we can came up with a basic layout that you can work from.

OscarRyz
A: 

Ok, yeah I know I should use a layout manager etc. but still I chose to build it with the Eclipse Visual Editor (which doesn't have all the layout managers there are) is because I don't have much time for this project and I thought it would be faster that way.

Basically this screen shows how the program should look - some random input/buttons on the left (added random ones for the time being) and the 4 JPanels on the right - they are quite important to me.

screen

Zenzen
1. don't post an answer unless it is an answer, this is not a forum. edit your question if you think you have more info to share.2. use a layout manager. it is the only way.
pstanton