Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

GridBagLayout

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After setting the LayoutManager to 'GridBackLayout', the component (JLabel) I add to the panel on row 0 (y = 0), column 0 (x = 0) centers on the screen; so the screen origin seems to be in the middle and not in the lefthand top corner of the screen as I expected it to be. Is this as it should be and can I (re)set the origin of the screen?  
 
Saloon Keeper
Posts: 6039
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have a small stand alone program that demonstrates this?

I'm going to guess that the dimension of the cell expands to fill all available space and then the label is centered in that.
 
Rancher
Posts: 3063
23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the default behaviour unless you specify a weightx or weighty constraint.

Read the section from the Swing tutorial on Specifying Constraints for more information.
 
Marshal
Posts: 65041
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please find a copy of Core Java by Horstmann. Older editions were called  Core Java II by Horstmann.and Cornell, and I don't think it matters which edition you use. Look for Volume I, which has a class called GBC in; Horstmann explains how you can use that to make GirdBag much easier to use.
 
Richard Legué
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Carey Brown: Here is the code that I used and comes up with the centered components on the JPanel. Thanks for looking at it.


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


public class FutureValueFrame extends JFrame {

     
   public FutureValueFrame(){
               initComponents();
   }
   
   private void initComponents(){
       setTitle("future Value Calculator");
       setSize(400, 400);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setLocationByPlatform(true);
       
           try{
           UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
           }
           catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e){
           System.out.println(e);
           }
       
      JPanel pane = new JPanel();
      pane.setLayout(new GridBagLayout());
      GridBagConstraints c = new GridBagConstraints();
      add(pane);
     
      JButton button = new JButton("Button1");
      c.gridx = 0;
      c.gridy = 0;
      c.ipadx=0;
      c.ipady = 0;
      pane.add(button,c);
     
      JButton button1 = new JButton("Button2");
      c.gridx = 1;
      c.gridy = 0;
      c.ipadx=0;
      c.ipady = 0;
      pane.add(button1,c);
     
      JButton button2 = new JButton("Button3");
      c.gridx = 0;
      c.gridy = 1;
      c.ipadx = 0;
      c.ipady = 0;
      pane.add(button2,c);
     
      setVisible(true);
   }
       

   public static void main(String[] args) {
   java.awt.EventQueue.invokeLater(new Runnable() {
       @Override
       public void run(){
       JFrame frame = new FutureValueFrame();
       }
   });
   }
       
       
}
   
 
Carey Brown
Saloon Keeper
Posts: 6039
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a way of doing it. Specify the layout heights/widths & weights, leaving an extra row and column that will expand to fill any unused space.
 
Rob Camick
Rancher
Posts: 3063
23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Richard Legué wrote: Here is the code that I used and comes up with the centered components on the JPanel.
   



So where did you set the weightx/weighty constraints???

Did you read the tutorial? What part of the tutorial do you not understand?

Did you download the demo code to see why it works in the tutorial?

Don't expect people to write the code for you.
 
Richard Legué
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Carey Brown: thanks for your help. Very clear. I get it now!

@ Rob Camick: 1. I didn't set them obviously (duh..); 2. Yes, more than once? 3. that's exactly what I tried to find out; 4. No I did not. I used other tutorial material and examples (more than a couple); Ref you last remark: I don't expect anything of other people. I'm just asking your platform for help. Don't offer it if you don't want to, but much appreciated if you do. Thank you Carey Brown.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!