Link to home
Start Free TrialLog in
Avatar of Samooramad
Samooramad

asked on

grid centering

Hi experts,

How do you center labels in a gridbaglayout?

have a nice day :)
SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Samooramad
Samooramad

ASKER

no they sort of stay at the far left side
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
oh, I also did the following:

gridBagConstraints.ipadx=20;
  gridBagConstraints.ipady=20;
to space them out a bit..could that be it?
Can you post a small compilable example that demonstrates the problem?
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=20;
  gridBagConstraints.ipady=20;
  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(allHospitals[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(allHospitals[i].getEmployee(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.setForeground(Color.blue);
      panel.add(notSelectedTitle, gridBagConstraints);

if (notSelectedDoc[q] != null)
        {
          JLabel notSelected = new JLabel();
          notSelected.setText(notSelectedDoc[q]);
          gridBagConstraints.gridx = q+1;
          gridBagConstraints.gridy = countFullHospitals +3;
          panel.add(notSelected, gridBagConstraints);
        }

     }
oh did you want me to include main or something like that? If that is not enough let me know
Thanks again :)
Yes, please include main and all declarations.
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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is the link to the Sun Tutorial about GridBagLayout.
http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbag.html


Bye, Giant.
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 ;)
ok this has been open way too long..sorry again
:-)
Thanks