| | |
GridBag layout problems.
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
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.
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
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
•
•
Join Date: Jul 2005
Posts: 5
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
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.
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.
•
•
Join Date: Jul 2005
Posts: 5
Reputation:
Solved Threads: 0
:-) 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.
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.
•
•
Join Date: Jun 2005
Posts: 1
Reputation:
Solved Threads: 0
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
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
![]() |
Other Threads in the Java Forum
- Previous Thread: Authenticator
- Next Thread: Proxies
| Thread Tools | Search this Thread |
Tag cloud for Java
affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth businessintelligence chat class classes client code component database desktop draw ebook eclipse encode equation error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer intersect j2me java javaexcel javaprojects jmf jni jpanel julia linked linux list loop mac main map method methods mobile netbeans newbie number object online open-source oracle parameter print problem program programming project properties recursion reference replaysolutions rotatetext scanner score screen scrollbar server set size sms socket sort sql string superclass swing template test threads time tree windows xstream






