Samooramad
asked on
grid centering
Hi experts,
How do you center labels in a gridbaglayout?
have a nice day :)
How do you center labels in a gridbaglayout?
have a nice day :)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
yes your code works fine and I did it in a very similar way but it seems my problem occured when I added different size labels
Im not sure if maybe that was the problem
Im not sure if maybe that was the problem
ASKER
oh, I also did the following:
gridBagConstraints.ipadx=2 0;
gridBagConstraints.ipady=2 0;
to space them out a bit..could that be it?
gridBagConstraints.ipadx=2
gridBagConstraints.ipady=2
to space them out a bit..could that be it?
Can you post a small compilable example that demonstrates the problem?
ASKER
yes. here is part of the code...everything is where I want it but the are just a bit of center.. hope it makes sence :
GridBagConstraints gridBagConstraints =new GridBagConstraints();
gridBagConstraints.ipadx=2 0;
gridBagConstraints.ipady=2 0;
int highestPoint=2;
for (int i = 0; i < 1; i++)
{
JLabel titleH = new JLabel ("Hospital");
titleH.setForeground(Color .BLUE);
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.anchor = GridBagConstraints.WEST;
panel.add(titleH, gridBagConstraints);
}//for
//first row titles
for (int i = 1; i < highestCapacity+1; i++)
{
JLabel titleD = new JLabel (" Position " +(i));
titleD.setForeground(Color .darkGray) ;
gridBagConstraints.gridx = i;
gridBagConstraints.gridy = 0;
gridBagConstraints.anchor = GridBagConstraints.WEST;
panel.add(titleD, gridBagConstraints);
}
for (int i = 0; i < allHospitals.length; i++)
{
JLabel showResultH[] = new JLabel[100];
JLabel showResultD[] = new JLabel[100];
if ((allHospitals[i] != null) && (allHospitals[i].full == true))
{
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = i+1;
showResultH[i]= new JLabel();
showResultH[i].setText(all Hospitals[ i].getName ());
panel.add(showResultH[i], gridBagConstraints);
for (int x = 0; x < allHospitals[i].capacity; x++)
{
int f=0;
gridBagConstraints.gridx = x + 1;
gridBagConstraints.gridy = i+1;
showResultD[f]= new JLabel();
showResultD[f].setText(all Hospitals[ i].getEmpl oyee(x));
gridBagConstraints.anchor = GridBagConstraints.WEST;
panel.add(showResultD[f], gridBagConstraints);
f++;
} //for capacity
} //if
}//for hospitals
for (int q= 0; q < notSelectedDoc.length; q++)
{
JLabel notSelectedTitle = new JLabel("Not Selected:");
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = countFullHospitals +3;
notSelectedTitle.setForegr ound(Color .blue);
panel.add(notSelectedTitle , gridBagConstraints);
if (notSelectedDoc[q] != null)
{
JLabel notSelected = new JLabel();
notSelected.setText(notSel ectedDoc[q ]);
gridBagConstraints.gridx = q+1;
gridBagConstraints.gridy = countFullHospitals +3;
panel.add(notSelected, gridBagConstraints);
}
}
GridBagConstraints gridBagConstraints =new GridBagConstraints();
gridBagConstraints.ipadx=2
gridBagConstraints.ipady=2
int highestPoint=2;
for (int i = 0; i < 1; i++)
{
JLabel titleH = new JLabel ("Hospital");
titleH.setForeground(Color
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.anchor = GridBagConstraints.WEST;
panel.add(titleH, gridBagConstraints);
}//for
//first row titles
for (int i = 1; i < highestCapacity+1; i++)
{
JLabel titleD = new JLabel (" Position " +(i));
titleD.setForeground(Color
gridBagConstraints.gridx = i;
gridBagConstraints.gridy = 0;
gridBagConstraints.anchor = GridBagConstraints.WEST;
panel.add(titleD, gridBagConstraints);
}
for (int i = 0; i < allHospitals.length; i++)
{
JLabel showResultH[] = new JLabel[100];
JLabel showResultD[] = new JLabel[100];
if ((allHospitals[i] != null) && (allHospitals[i].full == true))
{
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = i+1;
showResultH[i]= new JLabel();
showResultH[i].setText(all
panel.add(showResultH[i], gridBagConstraints);
for (int x = 0; x < allHospitals[i].capacity; x++)
{
int f=0;
gridBagConstraints.gridx = x + 1;
gridBagConstraints.gridy = i+1;
showResultD[f]= new JLabel();
showResultD[f].setText(all
gridBagConstraints.anchor = GridBagConstraints.WEST;
panel.add(showResultD[f], gridBagConstraints);
f++;
} //for capacity
} //if
}//for hospitals
for (int q= 0; q < notSelectedDoc.length; q++)
{
JLabel notSelectedTitle = new JLabel("Not Selected:");
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = countFullHospitals +3;
notSelectedTitle.setForegr
panel.add(notSelectedTitle
if (notSelectedDoc[q] != null)
{
JLabel notSelected = new JLabel();
notSelected.setText(notSel
gridBagConstraints.gridx = q+1;
gridBagConstraints.gridy = countFullHospitals +3;
panel.add(notSelected, gridBagConstraints);
}
}
ASKER
oh did you want me to include main or something like that? If that is not enough let me know
Thanks again :)
Thanks again :)
Yes, please include main and all declarations.
ASKER
Im sorry girionis, Im having a hard time doing that without including ALL the code which is over 1000 lines. Could you suggest anything?
Ehm.. Nope, I can't think of anything. GBL should place the components in the middle automatically. Do you hace your gbl layout embedded into another layout manager? Also take a look here: https://www.experts-exchange.com/questions/20330430/swing-dimension-question.html, read the second accepted comment and follow the links. They should give you some clues.
> Do you hace
should be
Do you have
should be
Do you have
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This is the link to the Sun Tutorial about GridBagLayout.
http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbag.html
Bye, Giant.
http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbag.html
Bye, Giant.
ASKER
Sorry Ive been having masive problems with my PC. just give me 2 more days to review the examples the experts posted since I have not been able to access them before and I will get this question closed
Sorry for any inconvineince :) and not replying promptly experts
Sorry for any inconvineince :) and not replying promptly experts
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
oooh... I thought that was for placing the layout :)
can you tell I'm new to this?
well that clears it up for me...thank you
yeah I know it seems useless.. dont you ever go for the stupid solutions when you get tired zzynx ;)
can you tell I'm new to this?
well that clears it up for me...thank you
yeah I know it seems useless.. dont you ever go for the stupid solutions when you get tired zzynx ;)
ASKER
ok this has been open way too long..sorry again
:)
:-)
Thanks
ASKER