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 / First Aid / April 2005

Tip: Looking for answers? Try searching our database.

GridBagLayout question

Thread view: 
Daniel Tahin - 27 Mar 2005 20:38 GMT
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>
Ryan Dillon - 28 Mar 2005 00:19 GMT
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

--
Ryan Dillon
Code Canvas Technologies
Sign up for the RapidJ beta!
http://www.codecanvas.com.au/rapidj/
Ryan Dillon - 28 Mar 2005 02:23 GMT
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
Daniel Tahin - 30 Mar 2005 18:17 GMT
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

> Trying anchoring the components:
>
[quoted text clipped - 16 lines]
> Sign up for the RapidJ beta!
> http://www.codecanvas.com.au/rapidj/
Ryan Dillon - 30 Mar 2005 23:37 GMT
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
Ryan Dillon - 30 Mar 2005 23:44 GMT
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
Ryan Dillon - 30 Mar 2005 23:53 GMT
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
Daniel Tahin - 01 Apr 2005 13:17 GMT
Hello Ryan,

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

Thank you for the support ;-).

Kind regards
Daniel

> Hi Daniel,
>
[quoted text clipped - 40 lines]
> Cheers
> Ryan
Ryan Dillon - 31 Mar 2005 00:11 GMT
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
Daniel Tahin - 30 Mar 2005 18:18 GMT
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

> Trying anchoring the components:
>
[quoted text clipped - 16 lines]
> Sign up for the RapidJ beta!
> http://www.codecanvas.com.au/rapidj/
Ryan Dillon - 31 Mar 2005 00:22 GMT
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


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.