I'm trying to create a calculator using the gridbag layout, but I can't even get started. This layout managers keeps starting stuff in the direct center of the frame and wont put anything anywhere else. I tried this:

c.anchor = GridBagConstraints.SOUTH;


I just can't seem to get this thing working. Could you guys help me out. I hate to ask for some code, but it would really be helpful in this situation.

Hi servercrash,

I believe your problem is related to all components getting added to the center. Buy default if you are using gridbaglayout as a layout manager to a container, the components in the container will get added to the center. You will have to specify the constraints while adding. I have attached an example hope this helps.

all the best
srinivas

Thanks man, but I'm still have tons of problems with this. I'll attach the code that I have, please see if you can at least help me get this thing started. By the way, this is NOT homework. I'm just trying to learn the gridbag layout while making a calculator but can't get this thing going.

hey i am attempting the same thing, after being introduced to java at my IBM work experiance i'm now really intrested in making GUI's etc. So far i have built the interface, there is no system code assigned to the buttons yet. I would just like to know if there is any way of creating arrays with the buttons. Because then i would be able to assign an index to each button. This would then mean that i would only need one action listener. I think.

Sorry if none of the above makes sense, i have only been doing this for 2 weeks.

Does anyone know of any good step by step books, or online help. This is the only place i have found that gives help on little snippets of code, such as alligning text in a JTEXTFIELD

Ok, I finally figured it out. It's not as hard as it looks. Thrilla, I'll help you out, but I'm really busy at the moment so just give me a little while and I'll see what I can do.

this is what i have managed so far.

For someone who has only been doing all this for 2 weeks i hope it aint too bad

That is EXCELLENT for a beginner. The example you gave really helped me out because I just could not figure it out. So, thank you very much.
I personally would much rather use the setBounds() method which gives you more freedom(in my opinion) and is much easier to use.

As for your question:

Yes you can use button arrays(if that's what you were asking). I guess you looked at my code. At first I was trying to use arrays but I don't think that's the best way to go using the gridbaglayout.

Good resources:

java.sun.com has the best tutorials.
javaalmanac.com has some really good stuff.

I think searching google is the most helpful. I personally don't like going to one site and looking around for a tutorial. Just search google for what you want to know, and I gurantee you, you will find great resources that way. One more thing, posting in forums are very helpful. If you ever have any questions or need anything, just post and I will see what I can do. If you will be more specific as to what you need, I can look for some resources for you.

:-) thankyou

actually i havent looked at your code yet, not quite sure what part of mine helped you, i tryed doing the interface using actual coordinates and sizes etc, but i found that took too long. If you need any more help on gridbaglayout then let me know, i aint an expert but it worked first time for me.

Just need to figure out more basic commands, its real annoying at the moment not knowing enough to progress, i find it takes me 20 mins or so to find the command i need, such as text aligning in the JTextField, or how to increase its font size with in the field.

Gonna do some more work on it tommorrow, let me know how you get on.

:)

I finished the calculator with added scientific buttons, but it doesn't have operator presidence. This is one thing I'm working on right now. I'll send you the calc via PM if I ever finish it to let you see what I got.

Hi everyone,
While using GridBagLayout, how can we add components to the layout. i mean, if i have a choice menu and the user selects one of the options from it, then another choice menu should be added to its side.
i wrote some code for it but instead of appearing at the required place, the new choice menu was appearing on the top left corner inspite of setting the proper constraints. a code snippet of the action() method for handling the user inputs is:

public boolean action(Event e,Object arg)
{
if(e.target instanceof Choice)
{
if(((String)arg).equals("South"))
{
GridBagLayout gridbag=(GridBagLayout)getLayout();
GridBagConstraints gbc=new GridBagConstraints();
Choice c=new Choice();
buildConstraints(gbc,2,23,1,2,100,0); //sets constraints in gbc
c.addItem("South-East");
c.addItem("South-West");
gridbag.setConstraints(c,gbc);
add(c);


}
}
return true;
}

when the 'South' option is selected a new Choice menu shud appear with 'South-East' and 'South-West'the constraints set are correct according to the desired output. but the new menu still appears at top-left corner. i dont understand why.
btw, this is an applet and i've used awt components not swing ones.
any help in this matter will be appreciated.
thanx

I didnt even know java existed 2 weeks ago so please bare with me, take a look at how i have coded the positions for the buttons for a calculator. There is repeated code for each button position, this may seem silly but i have done this so that it is easy to see what the properties for each button is.

I have no idea if this helps, let me know if it does, i am very new to this, but i am learning quickly.

Serial, I loved the way you used the properties for the gridbaglayout on each button. That's what helped me out. I've known Java for a year and am very good at it, but I've never used the gridbaglayout and to tell you the truth, the code you showed me really helped out.

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.