GridBagLayout question

D

Daniel Tahin

Hi newsgroup folks,

can everybody alter the applet code below that the buttons and labels
will not appear in the middle but at the beginning? Can`t do the task
with GridBagLayout...
Thanks in advance, Daniel


import java.applet.*;
import java.awt.*;

public class TestApplet extends Applet
{
public void stop()
{
System.out.println("stop called");
}

public void init ()
{

ScrollPane sp = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);
sp.setSize(200,200);
GridBagLayout g = new GridBagLayout();

GridBagConstraints c = new GridBagConstraints();
c.weightx=1.0;
c.weighty=0.0;

Panel p = new Panel(g);

for (int i=0; i<3; i++)
{

c.gridy++;

Button button1 = new Button("test");
g.setConstraints(button1,c);
p.add (button1);

Label l = new Label("test");
g.setConstraints(l,c);
p.add (l);

Button button2 = new Button("test");
g.setConstraints(button2,c);
p.add (button2);
}

sp.add(p);
add(sp);

}

}



The .html file:

<html>
<body>
<applet code = "TestApplet.class" width=400 height=400></applet>
</body>
</html>
 
R

Ryan Dillon

Trying anchoring the components:

c.anchor = GridBagConstraints.NORTH_WEST;


Also, instead of:

g.setConstraints(button1,c);
p.add(button1);

You can use:

p.add(button1, c);


Cheers
 
R

Ryan Dillon

Oh, and though I haven't compiled your example, you will probably need
to set the weighty to something non-zero and set the c.fill to
GridBagConstraints.BOTH. GridBadLayout is really quite complex and
there are lots of "gotchas" to be wary of. For this example you might
be able to get away with GridLayout, which is much simpler (though of
course less flexible).

Ryan
 
S

Sudsy

Ryan said:
Oh, and though I haven't compiled your example, you will probably need
to set the weighty to something non-zero and set the c.fill to
GridBagConstraints.BOTH. GridBadLayout is really quite complex and
there are lots of "gotchas" to be wary of. For this example you might
be able to get away with GridLayout, which is much simpler (though of
course less flexible).

Ryan,
While I and the OP obviously appreciate your investment of time and
effort, I find myself forced to take exception to your categorization
of GridBagLayout as containing 'lots of "gotchas"'.
The fact is that no matter what technology you choose to render
text and/or graphics, it's going to be necessarily complex. Even HTML
tables and the like can be "challenging". Then you've got frames and
all the browser incompatibilities...
That being said, I use GridBagLayout almost exclusively for GUIs.
It provides a lot of power and flexibility. There's a lot of meat on
the bone and it does take time to get familiar with all the details.
But have you ever coded X11/Motif?
I'm just saying that the more flexible layout managers take some
time to master. It's a good investment of effort, IMHO.
 
R

Ryan Dillon

Point taken :) I too find GridBagLayout very flexible and use it most
of the time also. However I also remember the difficulties I had
learning the ropes with it, and how in simple cases it was possible to
get away with Grid or Border Layouts. But certainly to make your GUI's
look good (or even decent), most of the time you will need to use
GridBagLayout or similar.

OT: Here's a little flick related to GridBagLayout that you may find
amusing :)
http://madbean.com/blog/2004/17/totallygridbag.html

Cheers
Ryan
 
D

Daniel Tahin

Hello Ryan,

thanks for the hints, I`ve tried them but no success. The best I can
reach is to display the components not to fill the whole paint area, but
they`ll appear in the middle. Can you please give a sample code? As
mentioned the layout is OK with the exception that they should appear in
the top of the window.

Thank you.
Daniel
 
D

Daniel Tahin

Hello Ryan,

thanks for the hints, I`ve tried them but no success. The best I can
reach is to display the components not to fill the whole paint area, but
they`ll appear in the middle. Can you please give a sample code? As
mentioned the layout is OK with the exception that they should appear in
the top of the window.

Thank you.
Daniel
 
S

steve

Point taken :) I too find GridBagLayout very flexible and use it most
of the time also. However I also remember the difficulties I had
learning the ropes with it, and how in simple cases it was possible to
get away with Grid or Border Layouts. But certainly to make your GUI's
look good (or even decent), most of the time you will need to use
GridBagLayout or similar.

OT: Here's a little flick related to GridBagLayout that you may find
amusing :)
http://madbean.com/blog/2004/17/totallygridbag.html

Cheers
Ryan

very funny.
 
R

Ryan Dillon

Hi Daniel

Maybe this will help?


public void init() {

setLayout(new BorderLayout());

ScrollPane sp = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);

Panel p = new Panel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.NORTH;
c.weightx = 1.0;
c.weighty = 0.0;

for (int i = 0; i < 3; i++) {

c.gridwidth = 1;
if (i==2) {
c.weighty = 1.0; // Give last row left over y space
}

Button button1 = new Button("test");
p.add(button1, c);

Label l = new Label("test");
p.add(l, c);

Button button2 = new Button("test");
c.gridwidth = GridBagConstraints.REMAINDER;
p.add(button2, c);

}

sp.add(p);
add(sp);

}

Cheers
Ryan
 
R

Ryan Dillon

Hi Daniel

I'm not sure exactly how you want it to look, but try this:

public void init() {

setLayout(new BorderLayout());

ScrollPane sp = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);

Panel p = new Panel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.NORTH;
c.weightx = 1.0;
c.weighty = 0.0;

for (int i = 0; i < 3; i++) {

c.gridwidth = 1;
if (i==2) {
c.weighty = 1.0; // Give last row left over y space
}

Button button1 = new Button("test");
p.add(button1, c);

Label l = new Label("test");
p.add(l, c);

Button button2 = new Button("test");
c.gridwidth = GridBagConstraints.REMAINDER;
p.add(button2, c);

}

sp.add(p);
add(sp);

}

Cheers
Ryan
 
R

Ryan Dillon

Hi Daniel,

(If this message appears three times, blame Google Groups beta!).

Try the following, it might be what you are looking for?


public void init() {

setLayout(new BorderLayout());

ScrollPane sp = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);

Panel p = new Panel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.NORTH;
c.weightx = 1.0;
c.weighty = 0.0;

for (int i = 0; i < 3; i++) {

c.gridwidth = 1;
if (i==2) {
c.weighty = 1.0; // Give last row left over y space
}

Button button1 = new Button("test");
p.add(button1, c);

Label l = new Label("test");
p.add(l, c);

Button button2 = new Button("test");
c.gridwidth = GridBagConstraints.REMAINDER;
p.add(button2, c);

}

sp.add(p);
add(sp);

}

Cheers
Ryan
 
A

Andrew McDonagh

Ryan said:
Point taken :) I too find GridBagLayout very flexible and use it most
of the time also. However I also remember the difficulties I had
learning the ropes with it, and how in simple cases it was possible to
get away with Grid or Border Layouts. But certainly to make your GUI's
look good (or even decent), most of the time you will need to use
GridBagLayout or similar.

OT: Here's a little flick related to GridBagLayout that you may find
amusing :)
http://madbean.com/blog/2004/17/totallygridbag.html

Cheers
Ryan

That is the funniest!
 
R

Ryan Dillon

Hi Daniel,

(If this message appears three times, blame Google Groups beta!).

Try the following, it might be what you are looking for?


public void init() {

setLayout(new BorderLayout());

ScrollPane sp = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);

Panel p = new Panel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.NORTH;
c.weightx = 1.0;
c.weighty = 0.0;

for (int i = 0; i < 3; i++) {

c.gridwidth = 1;
if (i==2) {
c.weighty = 1.0; // Give last row left over y space
}

Button button1 = new Button("test");
p.add(button1, c);

Label l = new Label("test");
p.add(l, c);

Button button2 = new Button("test");
c.gridwidth = GridBagConstraints.REMAINDER;
p.add(button2, c);

}

sp.add(p);
add(sp);

}

Cheers
Ryan
 
R

Ryan Dillon

Hi Daniel,

(If this message appears three times, blame Google Groups beta!).

Try the following, it might be what you are looking for?


public void init() {

setLayout(new BorderLayout());

ScrollPane sp = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);

Panel p = new Panel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.NORTH;
c.weightx = 1.0;
c.weighty = 0.0;

for (int i = 0; i < 3; i++) {

c.gridwidth = 1;
if (i==2) {
c.weighty = 1.0; // Give last row left over y space
}

Button button1 = new Button("test");
p.add(button1, c);

Label l = new Label("test");
p.add(l, c);

Button button2 = new Button("test");
c.gridwidth = GridBagConstraints.REMAINDER;
p.add(button2, c);

}

sp.add(p);
add(sp);

}

Cheers
Ryan
 
D

Daniel Tahin

Hello Ryan,

all of your messages appear ;-).
Great, this works and does exactly what I need!

Thank you for the support ;-).

Kind regards
Daniel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top