By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,812 Members | 856 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,812 IT Pros & Developers. It's quick & easy.

GridBagLayout

P: n/a
Hi everyone
I'm trying to develop an applet using JApplet. As the layout manager I
decided to use GridBagLayout. My layout will consist mainly of 7 JComboBox's
in a single row and one JTable beneath them.
My problem is, I can't even put the JComboBox's on the top of the
Applet. Everytime I do the add method, they stay put in the middle of the
Applet. I've tried setting GridBagConstraints.gridy = 0 but it didn't do me
any good.

Any help would be welcome, especially code examples or links
Thank you very much
Rui Pacheco
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Rui Pacheco" <ru*********@viaverde.pt_NOSPAM> wrote in message news:<10***************@news.oninet.pt>...
Hi everyone
I'm trying to develop an applet using JApplet. As the layout manager I
decided to use GridBagLayout. My layout will consist mainly of 7 JComboBox's
in a single row and one JTable beneath them.
My problem is, I can't even put the JComboBox's on the top of the
Applet. Everytime I do the add method, they stay put in the middle of the
Applet. I've tried setting GridBagConstraints.gridy = 0 but it didn't do me
any good.

Any help would be welcome, especially code examples or links
Thank you very much
Rui Pacheco


Wow, I guess having experience with other clunky GUI toolkits (like Xt
and Motif), GridBagLayout didn't seem too bad. I did like the way you
"attached" widgets to each other in those toolkits, though, like
"attachLeft" and "attachOppositeRight", stuff like that. I don't
really remember, it's been years.

Anyhow, from your description, I have to ask if you really need a
gridbag layout? Why would you want it? You know the way to getting
the interface exactly the way you want it is to make it as object
oriented as the code.

For example, assuming you want to use gridbag to make all the
comboboxes the same width, you might want to make two gridbags... one
for the top component, which might be a panel with a gridbag layout
with dimensions of 1 row and seven columns - make sure you set the
weights and so forth equally, and your fill is horizontal.

Then you have another gridbag, 2 rows by 1 column. The first row is
the jpanel you just created. This should also have a horizontal fill
to stretch to the width when the user resizes the window. The second
row would have the jtable - with fill being "BOTH", so that it
stretches to width and height. You may also need to set the weights
(top would have a Y weight of 0, the bottom 1, I think).

There's other ways to do it. Boxes is a nice way - create a parent
vertical box, then add a horizontal box with the comboboxes as the top
component, then the JTable below. You may need to set some options so
that it doesn't "wrap."

Now, I'm beginning to see your problem might be that the applet window
is larger than the UI you are creating, in which case your UI is
appearing in the center. Make sure your applet has the same
dimensions as you've defined in the web page. Also, don't judge the
layout until you have your jtable. I haven't really done that much
with applets, I usually do full applications, so the interaction with
the webbrowser might be an issue.

Seriously, though, what I find with "easy" layouts/toolkits is that
the developer takes short cuts and does things like making interfaces
that can't be resized - or if they are, you get unexpected behavior
(like a Jtable doesn't expand to fit, or a file selection doesn't
expand to fit - you just get blank space). As a user I find those
things most annoying, so as a developer I avoid them. Most of the
toolkits I've used that allow really flexible UI's, UI's where you can
define the behavior exactly the way you want it (and behave exactly
the way a user should reasonably expect, especially w.r.t. resizing)
are necessarily complex because they need to be flexible. Most of
them start to make sense after you've used them a few times, and
figured out how to do something that seemed awkward.

The best thing is to keep in mind that you can use multiple layouts in
a single application - one overall layout with a bunch of smaller
layouts, and often even smaller layouts within those. Always keep in
mind user behavior. I've have users whose habit is to make every
window fullscreen, wether it's needed or not. You just never know.
You can take the cheap way out and make it so your window can't be
resized, but that's pretty bad style, IMO.

Also, one thing I did discover with applets (use at your own risk), is
you can open a new window on the users machine from within the applet.
That way the user gets the flexibility of a native window, instead of
being confined in the browser. Use at your own risk, though, strange
things can happen when they open a new page while your application is
running, or hit the back button or something. Not very pretty.

Maybe I'll make some web pages about GridBag. I know some others have
said they're working on them. Regardless, let me know if you can't
get it to work.
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.