944,010 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 3996
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 13th, 2005
0

GridBag layout problems.

Expand Post »
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.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Jul 13th, 2005
0

Re: GridBag layout problems.

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
Attached Files
File Type: java TestGridBagLayout.java (1.6 KB, 81 views)
Reputation Points: 10
Solved Threads: 0
Light Poster
cheenu78 is offline Offline
45 posts
since Jun 2005
Jul 13th, 2005
0

Re: GridBag layout problems.

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.
Attached Files
File Type: java CalculatorApplet.java (3.4 KB, 36 views)
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Jul 13th, 2005
0

Re: GridBag layout problems.

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SERIAL THRILLA is offline Offline
5 posts
since Jul 2005
Jul 13th, 2005
0

Re: GridBag layout problems.

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.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Jul 13th, 2005
0

Re: GridBag layout problems.

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
Attached Files
File Type: java Calculator.java (4.5 KB, 58 views)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SERIAL THRILLA is offline Offline
5 posts
since Jul 2005
Jul 13th, 2005
0

Re: GridBag layout problems.

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.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Jul 13th, 2005
0

Re: GridBag layout problems.

:-) 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.

Reputation Points: 10
Solved Threads: 0
Newbie Poster
SERIAL THRILLA is offline Offline
5 posts
since Jul 2005
Jul 14th, 2005
0

Re: GridBag layout problems.

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.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Jul 15th, 2005
0

Re: GridBag layout problems.

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bigdevil is offline Offline
1 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Authenticator
Next Thread in Java Forum Timeline: Proxies





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC