Solved

grid centering

Posted on 2004-08-21
21
208 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 

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
 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

696 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