Results 1 to 4 of 4
  1. #1
    lisa.abraham is offline Member
    Join Date
    Jul 2012
    Posts
    7
    Rep Power
    0

    Default Help me with alignment. 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
    Help me with alignment. GridBagLayout-sample.png



    Java Code:
    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 at: Help me in alligning . GridBagLayout

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help me with alignment. GridBagLayout

    Do not use setSize(). Instead if possible let the components set their own sizes based on their need and the layout manager. You can use setPreferredSize(...) if you must set a size of a component. Always call pack() on the JFrame after adding all components which tells the layout managers to do their thing, then call setLocationXXX(...) then call setVisible(true) in that order.

  3. #3
    lisa.abraham is offline Member
    Join Date
    Jul 2012
    Posts
    7
    Rep Power
    0

    Default Re: Help me with alignment. GridBagLayout

    k thnx. but i like to make JFrame full screen. Thats why i was asking..

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: Help me with alignment. GridBagLayout

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

Similar Threads

  1. GridBagLayout alignment problem
    By adwart in forum AWT / Swing
    Replies: 2
    Last Post: 08-15-2011, 05:58 AM
  2. Gridbaglayout, hint on label alignment...
    By Marcoita_88 in forum AWT / Swing
    Replies: 1
    Last Post: 08-09-2011, 09:53 PM
  3. GUI alignment
    By africanhacker in forum New To Java
    Replies: 5
    Last Post: 03-13-2011, 10:22 PM
  4. pls... help.... alignment....
    By eiramae in forum Java Applets
    Replies: 0
    Last Post: 02-26-2011, 07:47 AM
  5. Help with alignment
    By firecat318 in forum AWT / Swing
    Replies: 3
    Last Post: 05-25-2010, 04:01 AM

Posting Permissions

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