Solved

grid centering

Posted on 2004-08-21
21
197 Views
Last Modified: 2010-03-31
Hi experts,

How do you center labels in a gridbaglayout?

have a nice day :)
0
Comment
Question by:Samooramad
  • 9
  • 6
  • 3
  • +1
21 Comments
 
LVL 35

Assisted Solution

by:girionis
girionis earned 100 total points
ID: 11859254
Hi Samooramad,
 are they not centered automatically? Try this:

import java.awt.*;
import javax.swing.*;

public class GridBag
{
    public static void main(String [] arguments)
    {
        JFrame frame = new JFrame();
        Container container = frame.getContentPane();
        JLabel label = new JLabel("hello");
        // Create the layout
        GridBagLayout gbl = new GridBagLayout();
       
        // Set layout on container
        container.setLayout(gbl);
       
        // Place a component at cell location (1,1)
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 1;
        // Add other gridbag constraints here
       
        // Associate the gridbag constraints with the component
        gbl.setConstraints(label, gbc);
       
        // Add the component to the container
        container.add(label);
       
        // Show the frame
        frame.pack();
        frame.setVisible(true);
    }
}

Regards
0
 

Author Comment

by:Samooramad
ID: 11859404
no they sort of stay at the far left side
0
 

Author Comment

by:Samooramad
ID: 11859432
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
0
 

Author Comment

by:Samooramad
ID: 11859439
oh, I also did the following:

gridBagConstraints.ipadx=20;
  gridBagConstraints.ipady=20;
to space them out a bit..could that be it?
0
 
LVL 35

Expert Comment

by:girionis
ID: 11859480
Can you post a small compilable example that demonstrates the problem?
0
 

Author Comment

by:Samooramad
ID: 11863783
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);
        }

     }
0
 

Author Comment

by:Samooramad
ID: 11863788
oh did you want me to include main or something like that? If that is not enough let me know
Thanks again :)
0
 
LVL 35

Expert Comment

by:girionis
ID: 11863985
Yes, please include main and all declarations.
0
 

Author Comment

by:Samooramad
ID: 11875703
Im sorry girionis, Im having a hard time doing that without including ALL the code which is over 1000 lines. Could you suggest anything?
0
 
LVL 35

Expert Comment

by:girionis
ID: 11878990
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: http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20330430.html, read the second accepted comment and follow the links. They should give you some clues.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 35

Expert Comment

by:girionis
ID: 11878994
> Do you hace

should be

Do you have
0
 
LVL 12

Accepted Solution

by:
Giant2 earned 200 total points
ID: 11881002
Try this to center your component in the GridBagLayout:
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.CENTER;

and then:
thePanelWhereYouAddTheComponents.add(yourComponent1, c);
thePanelWhereYouAddTheComponents.add(yourComponent2, c);
...
...
...
thePanelWhereYouAddTheComponents.add(yourComponent113, c);
thePanelWhereYouAddTheComponents.add(yourComponent114, c);

Hope this help you.
Bye, Giant.
0
 
LVL 12

Expert Comment

by:Giant2
ID: 11881037
This is the link to the Sun Tutorial about GridBagLayout.
http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbag.html


Bye, Giant.
0
 

Author Comment

by:Samooramad
ID: 11982216
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
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 200 total points
ID: 11989515
Hi Samooramad,

>> Could you suggest anything?
1) Certainly. In your code I see

          gridBagConstraints.anchor = GridBagConstraints.WEST;
   
     That's why
>>they sort of stay at the far left side
   
     If you want them to be centered you have to replace those lines by:

           gridBagConstraints.anchor = GridBagConstraints.CENTER;

2) The loop

          for (int i = 0; i < 1; i++)

    seems rather useless to me since it is only executed once ;°)
0
 

Author Comment

by:Samooramad
ID: 12010238
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 ;)
0
 

Author Comment

by:Samooramad
ID: 12010253
ok this has been open way too long..sorry again
0
 
LVL 35

Expert Comment

by:girionis
ID: 12010799
:)
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12013497
:-)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12013559
Thanks
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now