Results 1 to 6 of 6
  1. #1
    danpotter is offline Member
    Join Date
    May 2011
    Posts
    43
    Rep Power
    0

    Default GridBagLayout Component Problem...

    well i finally selected GridBagLayout for my purpose...as it suits for it best than any layout as all other layouts wont allow us for placing components in required place in JFrame..

    But..still components are not getting placed in position which i set...all text fied,button,label are appearing in center at same place even after placing with gridx and grid y..so once pls check my code and tell me where is the problem..
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    class lms extends JFrame implements ActionListener{
    
    	static JFrame frm;
    	JPanel pan;
    	JPanel pan1;
    JPanel pan2;
    	JTextField name,pswd;
    	JLabel lbl1,lbl2,lbl3,lbl4,lbl5;
    	JButton b1;
    	JComponent ac;
    	
    
    
    public lms(){
    
    GridBagLayout g=new GridBagLayout();
    GridBagConstraints c=new GridBagConstraints();
    
    frm=new JFrame("LMS");
    pan=new JPanel();
    pan1=new JPanel();
    
    
    frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frm.getContentPane().add(pan,BorderLayout.NORTH);
    frm.getContentPane().add(pan1);
    
    
    
    pan.setLayout(g);
    pan1.setLayout(g);
    
    
    
    
    
    	lbl1=new JLabel("Name : ",JLabel.LEFT);
    	c.gridx=1;
    	c.gridy=0;
    	c.gridwidth = 1;
        	c.gridheight = 1;
    	c.weighty = 1.0;
        c.weightx = 1.0;
    	c.fill=GridBagConstraints.EAST;
    	c.insets=new Insets(2,5,5,5);
    g.setConstraints(lbl1, c);
    
    
    	lbl2=new JLabel("Password : ");
    	c.gridx=2;
    	c.gridy=0;
    	c.gridwidth = 1;
        	c.gridheight = 1;
    c.fill=GridBagConstraints.EAST;
    g.setConstraints(lbl2, c);
    	lbl3=new JLabel("Welcome To LMS");
    	lbl4=new JLabel();
    	c.gridx=5;
    	c.gridy=0;
    	c.gridwidth = 1;
        	c.gridheight = 1;
    
    		name=new JTextField(10);
    		c.gridx=1;
    		c.gridy=3;
    		c.gridwidth = 1;
        	c.gridheight = 1;
    
    		pswd=new JTextField(10);
                    c.gridx=2;
    		c.gridy=3;
    		c.gridwidth = 1;
        		c.gridheight = 1;
    		
                            b1=new JButton("OK");
    			c.gridx=6;
    			c.gridy=0;
    			c.gridwidth = 1;
        			c.gridheight = 1;
    
    		name.setBackground(Color.pink);
    		name.setForeground(Color.blue);
    		Font f=new Font("Arial",Font.BOLD,15);
    		Font z=new Font("Arial",Font.ITALIC+Font.BOLD,15);
    
    		name.setFont(f);
    		lbl1.setFont(f);
    		lbl2.setFont(f);
    		lbl3.setFont(z);
    		lbl4.setFont(f);
    		lbl4.setForeground(Color.RED);
    
    
    		
    		pswd.setFont(f);
    		b1.setFont(f);
    		
    
    	}
    
    	public void actionPerformed(ActionEvent ae){
         
                    String s1=name.getText();
    			String s2=pswd.getText();
    				
    if(ae.getSource()=="OK" && s1.isEmpty() && s2.isEmpty())
    {
    lbl4.setText("Please Enter Username and Password!!!");
    
    }
    
    else if(s1.equals(s2)){
    			GetFrame2 f2=new GetFrame2();
    			f2.setSize(500,500);
    			f2.setVisible(true);
    			frm.setVisible(false);}
    else
    {
    lbl4.setText("Enter Correct Username and password!!!");
    }
    		
    }
    	public static void main(String args[]){
    
    lms l1=new lms();
    	
    
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    frm.setSize(screenSize.width, screenSize.height);  //This one is for full screen
    frm.setVisible(true);
    	}
    }
    only lbl3 is appearing perfectly in NORTH as it is set by me..all other labels,button and textfields are getting placed in center of frame even though i used gridx and gridy...

    Please help me....
    Last edited by danpotter; 05-09-2012 at 08:06 PM.

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

    Default Re: GridBagLayout Component Problem...

    Code Conventions for the Java Programming Language: Contents

    -- Use consistent indents
    -- Remove excess vertical whitespace (empty lines)

    Also remove multiple lines that are commented out, and any code such as listeners that isn't relevant to the layout.

    In other words, to get better help sooner post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.

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

  3. #3
    danpotter is offline Member
    Join Date
    May 2011
    Posts
    43
    Rep Power
    0

    Default Re: GridBagLayout Component Problem...

    yah removed unrequired code...but i used white spaces..just to show various fields in code...now please help me on my problem...leaving other things in code....

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

    Default Re: GridBagLayout Component Problem...

    In future, please don't edit a post after there are responses, as that makes the thread difficult to follow. Just post a new response instead.

    Your indenting is still all over the place, so I'm not going to try to read your code. If you're lucky, someone else may step in to help.

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

  5. #5
    danpotter is offline Member
    Join Date
    May 2011
    Posts
    43
    Rep Power
    0

    Default Re: GridBagLayout Component Problem...

    hope this makes u read my code...

    Java Code:
    class lms extends JFrame implements ActionListener{
    
    	static JFrame frm;
    	JPanel pan;
    	JPanel pan1;
    JPanel pan2;
    	JTextField name,pswd;
    	JLabel lbl1,lbl2,lbl3,lbl4,lbl5;
    	JButton b1;
    	JComponent ac;
    	
    
    
    public lms(){
    
    GridBagLayout g=new GridBagLayout();
    GridBagConstraints c=new GridBagConstraints();
    
    frm=new JFrame("LMS");
    pan=new JPanel();
    pan1=new JPanel();
    
    
    frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frm.getContentPane().add(pan,BorderLayout.NORTH);
    frm.getContentPane().add(pan1);
    
    
    
    pan.setLayout(g);
    pan1.setLayout(g);
    
    
    
    
    
    	lbl1=new JLabel("Name : ",JLabel.LEFT);
    	c.gridx=1;
    	c.gridy=0;
    	c.gridwidth = 1;
        	c.gridheight = 1;
    	c.weighty = 1.0;
        c.weightx = 1.0;
    	c.fill=GridBagConstraints.EAST;
    	c.insets=new Insets(2,5,5,5);
    g.setConstraints(lbl1, c);
    
    
    	lbl2=new JLabel("Password : ");
    	c.gridx=2;
    	c.gridy=0;
    	c.gridwidth = 1;
        	c.gridheight = 1;
    c.fill=GridBagConstraints.EAST;
    g.setConstraints(lbl2, c);
    	lbl3=new JLabel("Welcome To LMS");
    	lbl4=new JLabel();
    	c.gridx=5;
    	c.gridy=0;
    	c.gridwidth = 1;
        	c.gridheight = 1;
    
    		name=new JTextField(10);
    		c.gridx=1;
    		c.gridy=3;
    		c.gridwidth = 1;
        	c.gridheight = 1;
    
    		pswd=new JTextField(10);
                    c.gridx=2;
    		c.gridy=3;
    		c.gridwidth = 1;
        		c.gridheight = 1;
    		
                            b1=new JButton("OK");
    			c.gridx=6;
    			c.gridy=0;
    			c.gridwidth = 1;
        			c.gridheight = 1;
    
    		
    
    	}

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

    Default Re: GridBagLayout Component Problem...

    Your indentation is still quite random and distracting making your code very hard to read and easily understand. Consider reading or re-reading the link that Darryl provided that explains what your code should look like. Best of luck!

Similar Threads

  1. GridBagLayout alignment problem
    By adwart in forum AWT / Swing
    Replies: 2
    Last Post: 08-15-2011, 04:58 AM
  2. GridBagLayout problem!
    By canersen in forum AWT / Swing
    Replies: 9
    Last Post: 03-06-2011, 11:35 PM
  3. Problem with GridBagLayout
    By ze snow in forum New To Java
    Replies: 20
    Last Post: 02-28-2010, 06:08 AM
  4. Java Swing GridBagLayout Problem
    By hemanthjava in forum AWT / Swing
    Replies: 1
    Last Post: 06-29-2008, 08:51 PM
  5. Problem with GridBagLayout
    By Daniel in forum SWT / JFace
    Replies: 2
    Last Post: 07-01-2007, 06:57 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
  •