Results 1 to 9 of 9

Thread: Layout Manager

  1. #1
    globo is offline Member
    Join Date
    Sep 2010
    Posts
    61
    Rep Power
    0

    Default Layout Manager

    Hello
    I am new to java and got stock in this GUI.
    I need to get this :

    I am half way done this my code so far.

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.GridBagLayout;
    //import javax.swing.JPanel;
     
    public class GUIQuestion extends JFrame
    {
      
    	JCheckBox b1 = new JCheckBox("one", false);
       JCheckBox b2 = new JCheckBox("two", false);
       JCheckBox b3 = new JCheckBox("three", true);
       JCheckBox b4 = new JCheckBox("four", false);
       
    	JButton ok = new JButton("OK");
     
       JTextField tf = new JTextField("sample");
    		
       public static void main(String [] args)
       {
    
          GUIQuestion gq = new GUIQuestion();
    
    	 }
       public GUIQuestion()
       {
          super("GUI Question");
    		
         setLayout(new FlowLayout());
    		setLayout(new GridLayout(3,3));
    
    		
    			JPanel b1panel = new JPanel();////income panel and text field
    			JPanel b2panel = new JPanel();
    			JPanel b3panel = new JPanel();
    			JPanel b4panel = new JPanel();
    			JPanel okpanel = new JPanel();//(new FlowLayout(FlowLayout.RIGHT)); 
    			JPanel tfpanel = new JPanel(); 
    			
    			
    			//add checkbox to the panels
    			b1panel.add(b1);		
    			b2panel.add(b3);
    			b2panel.add(b2);
    			b2panel.add(b4);
    			okpanel.add(ok);
    			//okpanel.setLayout(new FlowLayout(FlowLayout.NORTH));
    			okpanel.setAlignmentX(Component.LEFT_ALIGNMENT);
    			
    			tfpanel.add(tf);
    			tfpanel.setLayout(new FlowLayout(FlowLayout.LEFT));
    
    				
    			
    			//add the panel to the content panel			
    			add(b1panel);
    			add(b3panel);		
    			add(b2panel);
    			add(b4panel);
    			add(okpanel);
    			
    			
    			add(tfpanel);	
    		
    		
    	
    		
    		
    		
    		pack();
          setVisible(true);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    		
    		}
    	
    	}

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

    Default

    I would recommend that you get rid of all those JPanels and use of only one extra JPanel, one to hold the JRadioButtons in, and that JPanel should use what layout?

    The other components and the radiobutton's JPanel can be added directly onto the JFrame's contentPane which uses BorderLayout by default.

    Also, there's no place for FlowLayout here.

    For more on layouts, please look here: http://download.oracle.com/javase/tu...ut/visual.html

  3. #3
    globo is offline Member
    Join Date
    Sep 2010
    Posts
    61
    Rep Power
    0

    Default

    You mean to use 1 JPanel that will hold all JRadioButtons, 1 JPanel to hold button ok and one to hold text field?

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

    Default

    Quote Originally Posted by globo View Post
    You mean to use 1 JPanel that will hold all JRadioButtons, 1 JPanel to hold button ok and one to hold text field?
    No, I mean one JPanel to hold the four JRadioButtons and that's it, no more. You add that JPanel and the ok JButton and the tf JTextField directly to the JFrame's contentPane using BorderLayout constants.

    You may be over-complicating matters a bit.

  5. #5
    globo is offline Member
    Join Date
    Sep 2010
    Posts
    61
    Rep Power
    0

    Default

    How can you do that?
    could you give a code hint please.

  6. #6
    globo is offline Member
    Join Date
    Sep 2010
    Posts
    61
    Rep Power
    0

    Default

    would be something like this

    JPanel w = new JPanel();
    w.setLayout(new GridLayout (2, 1));
    w.add(b1);
    w.add(b2);
    w.add(b3);
    w.add(b4);

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

    Default

    Quote Originally Posted by globo View Post
    would be something like this

    Java Code:
    JPanel w = new JPanel();
    w.setLayout(new GridLayout (2, 1));
    Check out the layout manager tutorial and the GridLayout tutorial to see what parameters you should be passing into GridLayout. Currently you're telling that the JPanel will have two rows and one column. Are you sure this is what you want?

  8. #8
    globo is offline Member
    Join Date
    Sep 2010
    Posts
    61
    Rep Power
    0

    Default

    I did that but how to make the button show on the right side?

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

    Default

    Quote Originally Posted by globo View Post
    I did that but how to make the button show on the right side?
    Again, the JPanel's contentPane uses BorderLayout by default. Knowing this information and knowing how to use BorderLayout (as per the BorderLayout tutorial), you should be able to figure this out. Give it a go, and post your latest code after doing so.

    Luck!
    Last edited by Fubarable; 11-01-2010 at 01:21 AM. Reason: Link to tutorial provided

Similar Threads

  1. Replies: 3
    Last Post: 01-11-2010, 05:34 PM
  2. Layout manager
    By Nicholas Jordan in forum Java Applets
    Replies: 2
    Last Post: 11-24-2008, 04:02 PM
  3. Layout Manager of JApplet
    By Peter_APIIT in forum Java Applets
    Replies: 10
    Last Post: 07-19-2008, 06:37 AM
  4. How to create a customized layout manager
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-26-2008, 08:41 PM
  5. MiG Layout Manager 2.4
    By levent in forum Java Software
    Replies: 0
    Last Post: 05-16-2007, 06:11 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
  •