Results 1 to 7 of 7
Thread: Radio Button
- 02-04-2011, 06:32 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 7
- Rep Power
- 0
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:
Thanks in advance..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"); }Last edited by S-NESH; 02-04-2011 at 06:36 AM.
- 02-04-2011, 07:17 AM #2
Member
- Join Date
- Nov 2010
- Location
- New Delhi
- Posts
- 50
- Rep Power
- 0
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.
- 02-04-2011, 07:18 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-04-2011, 03:55 PM #4
Member
- Join Date
- Dec 2010
- Posts
- 7
- Rep Power
- 0
- 02-04-2011, 03:59 PM #5
Member
- Join Date
- Dec 2010
- Posts
- 7
- Rep Power
- 0
How do I set the GridLayouts position? Like for example, in BorderLayout we set it
. But I've no idea whats the code in GridLayout. I triedJava Code:p2.add([COLOR="Red"]BorderLayout.NORTH[/COLOR], new JButton ("north"));but it doesnt work. It has errors. Can you help me?Java Code:add (p2, GridLayout. WEST);
- 02-04-2011, 04:16 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-04-2011, 05:03 PM #7
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
-
Disable Radio button
By AJG in forum New To JavaReplies: 3Last Post: 05-10-2011, 11:09 AM -
Customized Radio Button
By Javified in forum AWT / SwingReplies: 6Last Post: 12-13-2010, 07:06 AM -
Clear Radio Button
By Reborn in forum New To JavaReplies: 6Last Post: 07-25-2010, 05:21 PM -
Radio Button help!
By javanator in forum New To JavaReplies: 3Last Post: 04-25-2010, 08:01 PM -
How to use SWT Radio Button
By Java Tip in forum SWTReplies: 0Last Post: 07-25-2008, 02:25 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks