Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: GridBagLayout

  1. #1
    Razion is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default GridBagLayout

    Hello!

    Having some odd issues using a GridBagLayout, where I have components in odd places, or on top of other components.

    Any suggestions would be brilliant. :]

    Java Code:
    public void addComponent(Container contentPane){
        	setContentPane(contentPane);
        	GridBagConstraints c = new GridBagConstraints();
        	contentPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        	
        	//display = new JTextArea("k");
        	//display.setSize(FRAME_WIDTH, FRAME_HEIGHT / 2);
        	//display.setVisible(true);
        	//c.fill = GridBagConstraints.HORIZONTAL;
        	//c.gridx = 0;
        	//c.gridy = 0;
        	//c.gridwidth = 3;
        	
        	
        	loanAmountLabel = new JLabel("Loan Amount");
        	c.fill = GridBagConstraints.HORIZONTAL;
        	c.insets = new Insets(10, 10, 10, 10);
        	c.gridx = 0;
        	c.gridy = 1;
        	c.gridwidth = 1;
        	contentPane.add(loanAmountLabel, c);
        	
        	interestRateLabel = new JLabel("Interest Rate");
        	c.fill = GridBagConstraints.HORIZONTAL;
        	c.gridx = 1;
        	c.gridy = 1;
        	c.gridwidth = 1;
        	contentPane.add(interestRateLabel, c);
        	
        	loanPeriodLabel = new JLabel("Loan Period");
        	c.fill = GridBagConstraints.HORIZONTAL;
        	c.gridx = GridBagConstraints.RELATIVE;
        	c.gridy = 2;
        	c.gridwidth = 1;
        	contentPane.add(loanPeriodLabel, c);
        	
        	loanAmount = new JTextField();
        	c.fill = GridBagConstraints.HORIZONTAL;
        	c.gridx = 0;
        	c.gridy = 2;
        	c.gridwidth = 1;
        	contentPane.add(loanAmount, c);
        	
        	interestRate = new JTextField();
        	c.fill = GridBagConstraints.HORIZONTAL;
        	c.gridx = 1;
        	c.gridy = 2;
        	c.gridwidth = 1;
        	contentPane.add(interestRate, c);
        	
        	loanPeriod = new JTextField();
        	c.fill = GridBagConstraints.HORIZONTAL;
        	c.gridx = 2;
        	c.gridy = 2;
        	c.gridwidth = 1;
        	contentPane.add(loanPeriod, c);
        }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: GridBagLayout

    Moved from New to Java.

    Nowhere in the posted code is the layout set to a GridBagLayout. To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Razion is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Re: GridBagLayout

    Thanks for the tip. This one should be compilable.

    Java Code:
    package mortgagecalc;
    
    import java.awt.ComponentOrientation;
    import java.awt.Container;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.LayoutManager;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.*;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    
    public class MortgageCalc extends JFrame implements ActionListener{
    	
    	JTextField loanAmount, interestRate, loanPeriod;
    	JLabel loanAmountLabel, interestRateLabel, loanPeriodLabel;
    
            public static void main( String[] args ) {
        	    MortgageCalc frame = new MortgageCalc();
        	    frame.setVisible(true);
            }
    
            public MortgageCalc(){
        	    setSize(FRAME_WIDTH, FRAME_HEIGHT);
        	    setResizable(false);
        	    setDefaultCloseOperation(EXIT_ON_CLOSE);
        	    setLocationRelativeTo(null);
        	    JPanel content = new JPanel(new GridBagLayout());
        	    addComponent(content);
            }
    
    public void addComponent(Container contentPane){
        	setContentPane(contentPane);
        	GridBagConstraints c = new GridBagConstraints();
        	contentPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        	
        	//display = new JTextArea("k");
        	//display.setSize(FRAME_WIDTH, FRAME_HEIGHT / 2);
        	//display.setVisible(true);
        	//c.fill = GridBagConstraints.HORIZONTAL;
        	//c.gridx = 0;
        	//c.gridy = 0;
        	//c.gridwidth = 3;
        	
        	
        	loanAmountLabel = new JLabel("Loan Amount");
        	c.fill = GridBagConstraints.HORIZONTAL;
        	c.insets = new Insets(10, 10, 10, 10);
        	c.gridx = 0;
        	c.gridy = 1;
        	c.gridwidth = 1;
        	contentPane.add(loanAmountLabel, c);
        	
        	interestRateLabel = new JLabel("Interest Rate");
        	c.fill = GridBagConstraints.HORIZONTAL;
        	c.gridx = 1;
        	c.gridy = 1;
        	c.gridwidth = 1;
        	contentPane.add(interestRateLabel, c);
        	
        	loanPeriodLabel = new JLabel("Loan Period");
        	c.fill = GridBagConstraints.HORIZONTAL;
        	c.gridx = GridBagConstraints.RELATIVE;
        	c.gridy = 2;
        	c.gridwidth = 1;
        	contentPane.add(loanPeriodLabel, c);
        	
        	loanAmount = new JTextField();
        	c.fill = GridBagConstraints.HORIZONTAL;
        	c.gridx = 0;
        	c.gridy = 2;
        	c.gridwidth = 1;
        	contentPane.add(loanAmount, c);
        	
        	interestRate = new JTextField();
        	c.fill = GridBagConstraints.HORIZONTAL;
        	c.gridx = 1;
        	c.gridy = 2;
        	c.gridwidth = 1;
        	contentPane.add(interestRate, c);
        	
        	loanPeriod = new JTextField();
        	c.fill = GridBagConstraints.HORIZONTAL;
        	c.gridx = 2;
        	c.gridy = 2;
        	c.gridwidth = 1;
        	contentPane.add(loanPeriod, c);
        }
    
    	@Override
    	public void actionPerformed(ActionEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    }

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: GridBagLayout

    Quote Originally Posted by Razion View Post
    Thanks for the tip. This one should be compilable.
    Nope.
    Java Code:
    E:\Java\examples\src\swing\layout\MortgageCalc.java:17: cannot find symbol
    symbol  : variable FRAME_WIDTH
    location: class swing.layout.MortgageCalc
                setSize(FRAME_WIDTH, FRAME_HEIGHT);
    E:\Java\examples\src\swing\layout\MortgageCalc.java:17: cannot find symbol
    symbol  : variable FRAME_HEIGHT
    location: class swing.layout.MortgageCalc
                setSize(FRAME_WIDTH, FRAME_HEIGHT);
    2 errors
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: GridBagLayout

    After fixing that, the code does pretty much what I would expect.

    -- The JTextFields are constructed using the no-arg constructor, so the preferredSize.width is a few pixels.
    -- Those text fields that are in the same column as a JLabel adopt the preferred width of the label
    -- loanPeriodLabel is added with x=RELATIVE, which evaluates to 2 (relative to 1 for interestRateLabel) and y=2, so occupies the same cell as loanPeriod

    A note: posting several commented lines only adds clutter, as does an empty implemention of an interfacethat has nothing to do with your question. You should take the trouble to remove them when posting on a forum. Also, there's no point repeatedly assigning the same value to the same field of your GridBagConstraints. Here's your code correctly formatted/indented and with all redundant lines removed; no other changes, except to change the fields to local variables (this is a layout problem; they aren't needed at the class level) and launch the GUI on the EDT (see Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)).

    Notice how much shorter it is.
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class MortgageCalc extends JFrame {
    
      public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
    
          @Override
          public void run() {
            new MortgageCalc().setVisible(true);
          }
        });
      }
    
      public MortgageCalc() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        JPanel content = new JPanel(new GridBagLayout());
        addComponent(content);
        pack();
        setLocationRelativeTo(null);
      }
    
      public void addComponent(Container contentPane) {
        setContentPane(contentPane);
        GridBagConstraints c = new GridBagConstraints();
        contentPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    
        JLabel loanAmountLabel = new JLabel("Loan Amount");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.insets = new Insets(10, 10, 10, 10);
        c.gridx = 0;
        c.gridy = 1;
        contentPane.add(loanAmountLabel, c);
    
        JLabel interestRateLabel = new JLabel("Interest Rate");
        c.gridx = 1;
        contentPane.add(interestRateLabel, c);
    
        JLabel loanPeriodLabel = new JLabel("Loan Period");
        c.gridx = GridBagConstraints.RELATIVE;
        c.gridy = 2;
        contentPane.add(loanPeriodLabel, c);
    
        JTextField loanAmount = new JTextField();
        c.gridx = 0;
        contentPane.add(loanAmount, c);
    
        JTextField interestRate = new JTextField();
        c.gridx = 1;
        contentPane.add(interestRate, c);
    
        JTextField loanPeriod = new JTextField();
        c.gridx = 2;
        contentPane.add(loanPeriod, c);
      }
    }
    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. GridBagLayout vs me: 1 - 0
    By JosAH in forum AWT / Swing
    Replies: 9
    Last Post: 08-19-2011, 09:18 PM
  2. GridBagLayout Help
    By Zman3359 in forum AWT / Swing
    Replies: 0
    Last Post: 02-21-2011, 08:39 PM
  3. [AWT] GridBagLayout Help.
    By Sandia_man in forum AWT / Swing
    Replies: 2
    Last Post: 05-23-2010, 08:54 PM
  4. help with gridbaglayout
    By robertbob in forum AWT / Swing
    Replies: 5
    Last Post: 05-18-2010, 04:14 AM
  5. GridBagLayout
    By carderne in forum New To Java
    Replies: 8
    Last Post: 01-25-2009, 02:06 PM

Posting Permissions

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