GridBagLayout

Well I managed to get my GridBagLayout to work exactly as I wanted in my java app, but when I converted it to an applet, I cant seem to get it to work

Essentially I have 3 methods which return a JPanel. I have topPanel (which is on the top and spans the entire width), addStatPanel (which is below the topPanel and to the left) and sigPanel (which is below topPanel and to the right)

In html this is what it would look like

<table border="1">

<tr>

<td colspan="2">topPanel()</td>

<tr>

<tr>

<td>addStatPanel()</td>

<td>sigPanel()</td>

</tr>

</table>

And thats how I got it to work in my app. This is my code I have in my applet, and I'm not sure where its going wrong. Only topPanel() and addStatPanel() show. If I remove addStatPanel(), sigPanel() will show.

package statsigs;

import java.awt.*;

import javax.swing.*;

publicclass MainApplextends JApplet{

private JPanel p_topPanel, p_addStatPanel, p_sigPanel;

private JButton b_chooseBackground, b_chooseColor;

private JList lst_stats;

private String[] stats =new String[1];

publicvoid init()

{

stats[0] = getParameter("username");

setLayout(new GridBagLayout());

GridBagConstraints c =new GridBagConstraints();

c.gridx = 0;

c.gridy = 0;

c.gridwidth = 2;

c.fill = c.REMAINDER;

add(topPanel(), c);

c.gridx = 0;

c.gridy = 1;

add(addStatPanel(), c);

c.gridx = 1;

c.gridy = 1;

add(sigPanel(), c);

setSize(500, 500);

}

private JPanel topPanel()

{

p_topPanel =new JPanel();

b_chooseBackground =new JButton("Select Background");

p_topPanel.add(b_chooseBackground);

b_chooseColor =new JButton("Select Font Color");

p_topPanel.add(b_chooseColor);

return p_topPanel;

}

public JPanel addStatPanel()

{

p_addStatPanel =new JPanel();

//Create a new JList which contains the parameters passed in at runtime

lst_stats =new JList(stats);

//Using the Decorator Pattern, add a ScrollPane to the JList

JScrollPane sp_scroller =new JScrollPane(lst_stats);

sp_scroller.setPreferredSize(new Dimension(120, 200));

//Add the ScrollPane to the panel

p_addStatPanel.add(sp_scroller);

return p_addStatPanel;

}

public JPanel sigPanel(){

p_addStatPanel =new JPanel();

JLabel hello =new JLabel("Hello!");

p_addStatPanel.add(hello);

return p_addStatPanel;

}

publicvoid start(){

System.out.println("Applet starting.");

}

publicvoid stop(){

System.out.println("Applet stopping.");

}

publicvoid destroy(){

System.out.println("Destroy method called.");

}

}

Thanks.

[4826 byte] By [SidewinderXa] at [2007-11-27 11:16:19]
# 1

> I managed to get my GridBagLayout to work exactly as I wanted in my

> java app, but when I converted it to an applet, I cant seem to get it to work

Then the GUI building code is different.

Read replies 27 and 30 from this posting for a better design that allows you to share the GUI building code between an application and an applet:

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=5145907&start=27

Also, its probably easier to use a BorderLayout do do what your want. Add the top panel to the NORTH and then add your other panels the WEST or CENTER or EAST depending on exactly how you want the GUI to work.

Finally, the problem with the posted code is that you don't reset the gridwidth constraint.

camickra at 2007-7-29 14:18:28 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thank you very much!

SidewinderXa at 2007-7-29 14:18:28 > top of Java-index,Desktop,Core GUI APIs...