Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help me in alligning . GridBagLayout

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    28
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Help me in alligning . GridBagLayout

    hi. i want to align heading to the top centre and i want to align rest of my fields and labels to the left after heading. i have tried alot but cant do it.

    i want to do it like this

    gH5Wq.png



    public class Main {
     
    	public static void main(String[] args) {
    		Entry test = new Entry();
     
    	}
     
     
    }
    --------------------------
     
    import java.awt.*;
     
    import javax.swing.*;
    public class Objects {
     
    	JLabel top;
    // TextInput Fields
    	JTextField SerialNo;
    	JTextField RecDate;
    	JTextField GatePassNo;
    	JTextField BoxNo;
    	public GridBagConstraints gbc = new GridBagConstraints();
    	public JPanel EntryPanel = new JPanel(new GridBagLayout());
    	JPanel HeadingPanel= new JPanel();
    	Objects()
    	{
    		top = new JLabel("Text1");
    		BoxNo = new JTextField(10);
    		SerialNo = new JTextField(10);
    	}
     
    }
     
     
    --------------------------
     
    import java.awt.*;
     
    import javax.swing.*;
     
    public class Entry extends Objects{
     
    	public Entry()
    	{
    		JFrame entry= new JFrame("Text1");
    		entry.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		entry.setLocationRelativeTo(null);
    		entry.setVisible(true);
    		top.setFont(new Font("Serif",Font.BOLD,30));
    		entry.getContentPane().add(HeadingPanel,BorderLayout.PAGE_START);
    		HeadingPanel.add(top);
    		entry.getContentPane().add(EntryPanel,BorderLayout.WEST);
    		gbc.gridx = 1;
    		gbc.gridy=5;
    		EntryPanel.add(BoxNo);
    		entry.setSize(600,600);
     
     
    	}
     
    }


    cross-posted: http://www.coderanch.com/t/590070/GU...-GridBagLayout
    http://www.java-forums.org/awt-swing...baglayout.html
    Last edited by fredsilvester93; August 16th, 2012 at 05:23 PM.


  2. #2
    Junior Member psabbate's Avatar
    Join Date
    Aug 2012
    Posts
    20
    My Mood
    Amused
    Thanks
    0
    Thanked 5 Times in 4 Posts

    Cool Re: Help me in alligning . GridBagLayout

    Quote Originally Posted by fredsilvester93 View Post
    hi. i want to align heading to the top centre and i want to align rest of my fields and labels to the left after heading. i have tried alot but cant do it.

    i want to do it like this

    gH5Wq.png

    public class Main {
     
    	public static void main(String[] args) {
    		Entry test = new Entry();
     
    	}
     
     
    }
    --------------------------
     
    import java.awt.*;
     
    import javax.swing.*;
    public class Objects {
     
    	JLabel top;
    // TextInput Fields
    	JTextField SerialNo;
    	JTextField RecDate;
    	JTextField GatePassNo;
    	JTextField BoxNo;
    	public GridBagConstraints gbc = new GridBagConstraints();
    	public JPanel EntryPanel = new JPanel(new GridBagLayout());
    	JPanel HeadingPanel= new JPanel();
    	Objects()
    	{
    		top = new JLabel("Text1");
    		BoxNo = new JTextField(10);
    		SerialNo = new JTextField(10);
    	}
     
    }
     
     
    --------------------------
     
    import java.awt.*;
     
    import javax.swing.*;
     
    public class Entry extends Objects{
     
    	public Entry()
    	{
    		JFrame entry= new JFrame("Text1");
    		entry.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		entry.setLocationRelativeTo(null);
    		entry.setVisible(true);
    		top.setFont(new Font("Serif",Font.BOLD,30));
    		entry.getContentPane().add(HeadingPanel,BorderLayout.PAGE_START);
    		HeadingPanel.add(top);
    		entry.getContentPane().add(EntryPanel,BorderLayout.WEST);
    		gbc.gridx = 1;
    		gbc.gridy=5;
    		EntryPanel.add(BoxNo);
    		entry.setSize(600,600);
     
     
    	}
     
    }

    cross-posted: Help me with alignment. GridBagLayout (Swing / AWT / SWT forum at JavaRanch)
    Help me with alignment. GridBagLayout
    Did you read the documentation? How to Use GridBagLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

    Anyway, here you have a small code which uses GridBagLayout ... hope it helps.

    private void buildGui() {
    		Container contentPane = this.getContentPane();
     
    		contentPane.setLayout(new GridBagLayout());
     
    		contentPane.add(ngFiltersPanel, new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.CENTER,
    				GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
    		contentPane.add(table, new GridBagConstraints(0, 1, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
    				new Insets(0, 5, 5, 5), 0, 0));
    		contentPane.add(buttonVolver, new GridBagConstraints(0, 2, 1, 1, 1, 0, GridBagConstraints.EAST, GridBagConstraints.NONE,
    				new Insets(0, 5, 5, 5), 0, 0));
    	}
    Everyone wants to go to heaven ... but nobody wants to die

    Nissi Group, Servicios IT,
    Diseño Web, Desarrollo de Software, SEO,
    LinkedIn

Similar Threads

  1. Help with GridBagLayout
    By mikejr76 in forum AWT / Java Swing
    Replies: 1
    Last Post: February 20th, 2012, 07:52 AM
  2. GridBagLayout can't seem to get it right.
    By ToyYoda in forum AWT / Java Swing
    Replies: 1
    Last Post: August 24th, 2011, 02:19 AM
  3. GridBagLayout help
    By mjpam in forum AWT / Java Swing
    Replies: 1
    Last Post: May 8th, 2011, 10:51 AM
  4. GridBagLayout help
    By mjpam in forum Java Theory & Questions
    Replies: 2
    Last Post: August 19th, 2010, 06:35 PM
  5. [SOLVED] Problem with GridBagLayout
    By lumpy in forum AWT / Java Swing
    Replies: 3
    Last Post: February 28th, 2010, 12:58 PM