Results 1 to 7 of 7

Thread: Radio Button

  1. #1
    S-NESH is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

    Exclamation Radio Button

    Hello there,

    Im trying to have my radio buttons in rows like in the picture below


    But I dont know how to put my second radio button right under the 1st one. I thought of having 4 panels for each rows and the 1st panel is set to 'BorderLayout. NORTH' and 2nd one is set to 'BorderLayout. WEST'. What should I put for the 3rd one or is there any other ways for me to radio buttons like in the picture?

    My code:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class GUI extends JFrame
    {
    	JLabel lbl1;
    	JTextField txt1;
    	JRadioButton rdb1;
    	JTextField txt2;
    	JRadioButton rdb2;
    	JRadioButton rdb3;
    
    	GUI()
    	{
    		setLayout (new BorderLayout());
    		lbl1 = new JLabel("Enter New Message: ");
    		txt1 = new JTextField (20);
    		rdb1 = new JRadioButton("RED");
    		txt2 = new JTextField (25);
    		txt2.setEditable (false);
    		rdb2 = new JRadioButton ("GREEN");
    		rdb3 = new JRadioButton ("BLUE");
    
    		JPanel p1 = new JPanel ();
    		JPanel p2 = new JPanel ();
    		JPanel p3 = new JPanel ();
    		JPanel p4 = new JPanel ();
    		JPanel p5 = new JPanel ();
    
    		p1.add (lbl1);
    		p1.add (txt1);
    		p2.add (rdb1);
    		p2.add (txt2);
    		//p3.add (rdb2);
    		//p4.add (rdb3);
    
    
    		add (p1, BorderLayout. NORTH);
    		add (p2, BorderLayout. WEST);
    		//add (p3, BorderLayout. WEST);
    		//add (p4, BorderLayout. CENTER);
    
    
    	}
    
    	public static void main(String[]args)
    	{
    		GUI g = new GUI();
    		g.pack();
    		g.setSize(400,150);
    		g.setVisible(true);
    		g.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    		g.setTitle("CompleteGUI");
    
    	}
    Thanks in advance..
    Last edited by S-NESH; 02-04-2011 at 07:36 AM.

  2. #2
    baloda is offline Member
    Join Date
    Nov 2010
    Location
    New Delhi
    Posts
    50
    Rep Power
    0

    Default

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class GUI extends JFrame
    {
    JLabel lbl1;
    JTextField txt1;
    JRadioButton rdb1;
    JTextField txt2;
    JRadioButton rdb2;
    JRadioButton rdb3;
    static JPanel p1;
    static JPanel p2 ;

    GUI()
    {
    setLayout (new BorderLayout());
    lbl1 = new JLabel("Enter New Message: ");
    txt1 = new JTextField (20);
    rdb1 = new JRadioButton("RED");
    txt2 = new JTextField ("25");
    txt2.setEditable (false);
    rdb2 = new JRadioButton ("GREEN");
    rdb3 = new JRadioButton ("BLUE");

    p1 = new JPanel ();
    p2 = new JPanel ();

    p1.setBounds(30,30,400,200);
    p2.setBounds(400,200,100,20);
    p1.add (lbl1);
    p1.add (txt1);
    //p1.add (rdb1);
    p1.add (txt2);

    lbl1.setBounds(10,10,150,20);
    txt1.setBounds(160,10,70,20);
    txt2.setBounds(90,90,70,20);
    rdb1.setBounds(10,90,70,15);
    rdb2.setBounds(10,110,70,15);
    rdb3.setBounds(10,130,70,15);

    add(rdb1, BorderLayout.CENTER);
    add(rdb2, BorderLayout.CENTER);
    add(rdb3, BorderLayout.CENTER);
    add(lbl1, BorderLayout.CENTER);
    add(txt1, BorderLayout.CENTER);
    add(txt2, BorderLayout.CENTER);
    }
    public static void main(String[]args)
    {
    GUI g = new GUI();
    g.add(p1);
    g.add(p2);
    g.setSize(420,250);
    g.setVisible(true);
    g.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    g.setTitle("CompleteGUI");

    }
    }

    I think this will work.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by S-NESH View Post
    Hello there,

    Im trying to have my radio buttons in rows like in the picture below


    But I dont know how to put my second radio button right under the 1st one. I thought of having 4 panels for each rows and the 1st panel is set to 'BorderLayout. NORTH' and 2nd one is set to 'BorderLayout. WEST'. What should I put for the 3rd one or is there any other ways for me to radio buttons like in the picture?
    I'd do it like this: at the top level stick three JComponents in a BorderLayout:

    NORTH: a JTextField (Welcome to JAVA)
    WEST: a JPanel (1)
    CENTER: a JPanel (2)

    (1) a JPanel with a GridLayout having three rows: one for each JRadioButton.
    (2) a JPanel with a GridLayout having three rows; only the first row filled with a JLabel (Welcome to JAVA)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    S-NESH is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by baloda View Post
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class GUI extends JFrame
    {
    JLabel lbl1;
    JTextField txt1;
    JRadioButton rdb1;
    JTextField txt2;
    JRadioButton rdb2;
    JRadioButton rdb3;
    static JPanel p1;
    static JPanel p2 ;

    GUI()
    {
    setLayout (new BorderLayout());
    lbl1 = new JLabel("Enter New Message: ");
    txt1 = new JTextField (20);
    rdb1 = new JRadioButton("RED");
    txt2 = new JTextField ("25");
    txt2.setEditable (false);
    rdb2 = new JRadioButton ("GREEN");
    rdb3 = new JRadioButton ("BLUE");

    p1 = new JPanel ();
    p2 = new JPanel ();

    p1.setBounds(30,30,400,200);
    p2.setBounds(400,200,100,20);
    p1.add (lbl1);
    p1.add (txt1);
    //p1.add (rdb1);
    p1.add (txt2);

    lbl1.setBounds(10,10,150,20);
    txt1.setBounds(160,10,70,20);
    txt2.setBounds(90,90,70,20);
    rdb1.setBounds(10,90,70,15);
    rdb2.setBounds(10,110,70,15);
    rdb3.setBounds(10,130,70,15);

    add(rdb1, BorderLayout.CENTER);
    add(rdb2, BorderLayout.CENTER);
    add(rdb3, BorderLayout.CENTER);
    add(lbl1, BorderLayout.CENTER);
    add(txt1, BorderLayout.CENTER);
    add(txt2, BorderLayout.CENTER);
    }
    public static void main(String[]args)
    {
    GUI g = new GUI();
    g.add(p1);
    g.add(p2);
    g.setSize(420,250);
    g.setVisible(true);
    g.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    g.setTitle("CompleteGUI");

    }
    }

    I think this will work.
    Yeah, it works actually. Thanks for your help :)

  5. #5
    S-NESH is offline Member
    Join Date
    Dec 2010
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    I'd do it like this: at the top level stick three JComponents in a BorderLayout:

    NORTH: a JTextField (Welcome to JAVA)
    WEST: a JPanel (1)
    CENTER: a JPanel (2)

    (1) a JPanel with a GridLayout having three rows: one for each JRadioButton.
    (2) a JPanel with a GridLayout having three rows; only the first row filled with a JLabel (Welcome to JAVA)

    kind regards,

    Jos
    How do I set the GridLayouts position? Like for example, in BorderLayout we set it
    Java Code:
    p2.add([COLOR="Red"]BorderLayout.NORTH[/COLOR], new JButton ("north"));
    . But I've no idea whats the code in GridLayout. I tried
    Java Code:
    add (p2, GridLayout. WEST);
    but it doesnt work. It has errors. Can you help me?

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by S-NESH View Post
    How do I set the GridLayouts position? Like for example, in BorderLayout we set it
    Java Code:
    p2.add([COLOR="Red"]BorderLayout.NORTH[/COLOR], new JButton ("north"));
    . But I've no idea whats the code in GridLayout. I tried
    Java Code:
    add (p2, GridLayout. WEST);
    but it doesnt work. It has errors. Can you help me?
    No, no, no, you add another JPanel to your top level JPanel. That other JPanel has a GridLayout. JPanels by themself aren't visible but they are Containers so they can contain other JComponents.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    and if you want to add multiple jpanels to the jframe your must first get the contentPane of the jframe like

    Container contentPane = getContentPane();

    and then you can add your different panels with

    contentPane.add(yourPanel, BorderLayout.Direction)

    where Direction must be one of them here

Similar Threads

  1. Disable Radio button
    By AJG in forum New To Java
    Replies: 3
    Last Post: 05-10-2011, 12:09 PM
  2. Customized Radio Button
    By Javified in forum AWT / Swing
    Replies: 6
    Last Post: 12-13-2010, 08:06 AM
  3. Clear Radio Button
    By Reborn in forum New To Java
    Replies: 6
    Last Post: 07-25-2010, 06:21 PM
  4. Radio Button help!
    By javanator in forum New To Java
    Replies: 3
    Last Post: 04-25-2010, 09:01 PM
  5. How to use SWT Radio Button
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-25-2008, 03:25 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
  •