Hey guys, got another question for you.

I have a program that displays a new bean on my jPanel for each piece of data that it picks up from a file. I was wondering if there is an easy way to always have it display my bean to the northwest of the jPanel.

The thing is, I can get it to set my first bean in the northwest corner, but not the rest of the beans.

I've been messing around with gridbaglayout and I have my anchor set to northwest.

I am using netbeans which makes it fairly simple to see, but thus far I can't quite figure out how to get it to display the way I want it to.

Very briefly, this is how I'm getting the bean to display on the jPanel:

GridBagConstraints gridBagConstraints = new GridBagConstraints();
            gridBagConstraints.gridx = 0;
            gridBagConstraints.gridy = i;
            gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
            basePanel.add(mvl, gridBagConstraints);

mvl is my bean.

Any suggestions or help is appreciated as always

Recommended Answers

All 5 Replies

set weightx and weighty fields.

gridBagConstraints.weightx=1.0;
gridBagConstraints.weighty=1.0;

Thanks for the reply, however that leaves gaps in between each bean. (gaps where you could fit about 5 beans)

The funny thing is I tried creating separate panels with weights for x and y earlier and came up with something close to the result of what that gave me.

I've been messing around with those properties for awhile and can't quite figure out how to eliminate that space in between them (vertical space/y). I've also been reading through layout managers to see if there is one that might be more probable for what I'm doing. But so far the only ones that seem reasonable are the gridbaglayout/grid layout and possibly the box layout

Changing the weighty to less doesn't seem to help it and if the weighty is completely removed then the beans all go to the center instead of the northwest corner

Thanks. It is difficult to guess about the problem you are facing. You said that it leaves gaps in between beans. Use fill field of GridBagConstaints.

Sorry for the late reply as I have been out of town.

To clear things up a bit, I have my beans being displayed on a jPanel. When they display with the settings above (including weight) they are placed in the jPanel but they are spaced out to where you see about three times the space of the actual bean in between each bean.

I can post up a picture of it when I get on my other computer (will be a few hours from now so I'll just try and show you what I mean with text)

This is what it looks like at the moment:

[bean]
[jPanel]
[jPanel]
[jPanel]
[bean]
[jPanel]
[jPanel]
[jPanel]
[bean]

So basically instead of them being stacked under each other it leaves jPanel space in between them.

I would like to get them to display like so: http://img89.imageshack.us/img89/729/programcopy.png

Sorry I don't mean to bump this but seeing as it won't let me edit my last post I have to.

I think it has something to do with my for loop.

ArrayList<Mal> malList = new ArrayList();
        for (int i = 0; i < result.length; i++) {
            MyArrayList mal = new MyArrayList(result[i]);   
              
            malList.add(mal);

For some reason I believe that is what is creating all the extra space in between. I'm not sure how to fix this, but I at least know its the problem now.

gridBagConstraints.gridy = i;

By setting it to 'i' it should line them up right under each other every time a new bean is there. I believe its thinking the bean is bigger than it really is, like it has some sort of external padding. Any ideas?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.