CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2010
    Posts
    161

    GridBagLayout problems

    Hi there

    I need a layout manager that allows me to resize each component and that gives me the ability to set desired positiong of each component withing the frame, therefore I chose to use the GridBagLayout.

    However I am having probelms even after reading the tutorials and can't get my head around it.

    I have a frame where I am putting a lbl and textlabel and I will be inserting a table and other components later on.

    First of all I am trying to put the label (title) and the textbox close to each other at the top of the frame but they stay in the centre. for some reason.

    here is the code

    Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.Toolkit;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    
    public class MainInterface extends JFrame {
    
      // Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize().;
      // Screen height and width
      int       screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
      int       screenWidth  = Toolkit.getDefaultToolkit().getScreenSize().width;
    
      JPanel    UIPanel      = new JPanel(new GridBagLayout());
      JTextArea txtQuery     = new JTextArea();
      
      GridBagConstraints c = new GridBagConstraints();
    
      public MainInterface() {
        setInterface();
        setUIPanel();
        setTxtQuery();
        
    	this.setVisible(true);
      }
      
      void setUIPanel(){
        UIPanel.setPreferredSize(new Dimension(this.getPreferredSize()));
        UIPanel.setBackground(Color.gray);
        UIPanel.setLayout(new GridBagLayout());
        this.add(UIPanel);
       
      }
       
      void setInterface() {
        setTitle("LSA Analysis");
        setSize((screenWidth - 100), (screenHeight - 100));
        UIPanel.setBackground(Color.white);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocation((getCentre()), 0);
    
      }
    
      private int getCentre() {
        int centre = screenWidth - this.getSize().width;
        return centre = centre / 2;
      }
    
      public void setTxtQuery(){
        //-------------------------------------
        
        JLabel lblQry = new JLabel();
        
        lblQry.setText("SQL Search Query :");
        lblQry.setForeground(Color.white);
        lblQry.setFont(new Font("Serif", Font.BOLD, 16));
        
        
        c.weightx = 0.5;   //request any extra vertical space
        
        c.gridx=0;
        c.gridy=0;
        c.ipady = 10;       //reset to default
        c.ipadx=5;
        
        //c.anchor = GridBagConstraints.PAGE_START; //bottom of space
        c.insets = new Insets(0,0,0,0);  //top padding
        
        UIPanel.add(lblQry,c);
        //----------------------------------------------
        
        JScrollPane scrollPane = new JScrollPane(txtQuery);
        scrollPane.setVerticalScrollBarPolicy(
                    JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        
        txtQuery.setFont(new Font("Serif", Font.BOLD, 24));
        //txtQuery.setPreferredSize(new Dimension(400,16));
        txtQuery.setWrapStyleWord(true);
        c.weightx = 0.5; 
        c.gridx=0;
        c.gridy=0;
        c.ipady = 10;       //reset to default
        c.ipadx=600;
          //request any extra vertical space
       // c.anchor = GridBagConstraints.PAGE_START; //bottom of space
        c.insets = new Insets(0,0,0,0);  //top padding
    
        UIPanel.add(scrollPane,c);
        
      }
            
      void setDemoTable(){
        DataTable dt = new DataTable(); 
        c.ipady = 10;       //reset to default
        c.ipadx=600;
        c.gridx=1;
        c.gridy=2;
        //c.weighty=100;
        //c.weightx=10;
        c.anchor= GridBagConstraints.LINE_START;
        this.UIPanel.add(dt,c);
        
        
      }
    
      /**
       * @param args
       */
      public static void main(String[] args) {
        // TODO Auto-generated method stub
        //MainInterface maininterface = new MainInterface();
        new MainInterface();
      }
    
    }


    Another question, is it there any program that allows me to put all the components like it's possible in csharp and .net?

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: GridBagLayout problems

    I need a layout manager that allows me to resize each component
    When you use a layout manager you hand over positioning and sizing of the components to the layout manager - that's one of the reasons for using a layout manager ie to save you the hassle of calculating the positions and sizes without overlapping components etc especially when you first add the components and/or the panel size changes.

    Each layout manager has it's own set of rules on how it lays out components and some, such as GridBagLayout, also allow you to specify additional layout constraints (rules). If you can't find a layout manager or combination of layout managers (you can have panels within panels each with their own layout manager) to do what you want you have 2 choices:

    1. Write your own layout manager - not as difficult as it sounds as long as you have a clear idea of what you are trying to achieve.
    2. Set the panels layout manager to null and use absolute positioning - but I really wouldn't recommend this approach, there is generally a way of doing it with layout managers.

    First of all I am trying to put the label (title) and the textbox close to each other at the top of the frame but they stay in the centre.
    In that case use a BorderLayout, add a JPanel to the NORTH area with a FlowLayout and add your label and textbox to this panel.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Jan 2010
    Posts
    161

    Re: GridBagLayout problems

    Yeah I have used no layout on one of my previous applications, it was a lot to do as size and positions were dynamic and calculated according to the size of the main frame etc.

    I've heard about creating a layout,if is not hard I might do that.

  4. #4
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: GridBagLayout problems

    Quote Originally Posted by cpu2007 View Post
    First of all I am trying to put the label (title) and the textbox close to each other at the top of the frame but they stay in the centre. for some reason.
    When I run it (after fixing the DataTable not found error) I only see the JTextArea. I don't even see the 'SQL Search Query :' label. That is because you put them both in the same GridBagLayout cell:

    Code:
      public void setTxtQuery(){
        JLabel lblQry = new JLabel();
        
        lblQry.setText("SQL Search Query :");
    
        ...
    
        c.gridx=0;
        c.gridy=0;
    
        ...
    
        UIPanel.add(lblQry,c);
        //----------------------------------------------
        
        JScrollPane scrollPane = new JScrollPane(txtQuery);
    
        ...
    
        c.gridx=0; // Bad. You are already using cell 0,0 for the lblQry. Change this to 1.
        c.gridy=0;
    
        ...
    
        UIPanel.add(scrollPane,c);
        
      }
    The way you figure these things out is to break the problem down into its smallest, simplest state. Remove anything that is not necesarry to demonstrate the problem. Create an SSCCE.

    I commented out the UIPanel.add(scrollPane,c); line and then I saw the label. Then I un commented it & figured out why the JTextArea appered on top of the JLabel. First I got rid of about 80% of unnecesarry code that only added to the confusion.

    http://sscce.org/

    http://docs.oracle.com/javase/tutori...t/gridbag.html

  5. #5
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: GridBagLayout problems

    Oh and change 'UIPanel' to 'uiPanel' so that you are using correct java naming conventions. Especially important when you are asking other people to read your code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured