|
-
Oct 26th, 2008, 02:56 PM
#1
Thread Starter
Member
[RESOLVED] TitledBorders around RadioButton Group?
Hello,
I am struggling to figureout how to create a titled border around a group of Radio Buttons. Here is my code so far. Any assistence will be greatly appreciated. Thanks in advance.
Code:
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.border.LineBorder;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import java.awt.*;
class borderPractice extends JFrame
{
public borderPractice()
{
setTitle("Border Test");
setSize(350, 350);
setLocation(300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel content = (JPanel) getContentPane();
content.setLayout(new GridLayout(6,2,3,3));
JPanel p = new JPanel();
p = new JPanel();
p.setBorder(new LineBorder(Color.black, 5));
p.add(new JLabel("Trying to get the 2 RadioButtons inside here!"));
JRadioButton rb1 = new JRadioButton("Option 1", true);
JRadioButton rb2 = new JRadioButton("Option 2", false);
ButtonGroup group = new ButtonGroup();
group.add(rb1);
group.add(rb2);
content.add(rb1);
content.add(rb2);
content.add(p);
setVisible (true);
}
public static void main( String args[])
{
borderPractice bp = new borderPractice();
}
}
-
Oct 26th, 2008, 03:53 PM
#2
Re: TitledBorders around RadioButton Group?
Always put your classes in packages, start your class with a capital letter, and never assign unused values to variables
Code:
package testing;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
public class BorderPractice extends JFrame
{
private static final long serialVersionUID = 8927395846549342658L;
public static void main(final String args[])
{
new BorderPractice();
}
public BorderPractice()
{
setTitle("Border Test");
setSize(350, 350);
setLocation(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Container content = getContentPane();
JPanel p = new JPanel();
p = new JPanel();
p.setBorder(new LineBorder(Color.black, 6));
p.add(new JLabel("Trying to get the 2 RadioButtons inside here!"));
final JPanel p2 = new JPanel();
p2.setBorder(new TitledBorder("Border"));
p2.setLayout(new GridLayout(2, 1, 3, 3));
final JRadioButton rb1 = new JRadioButton("Option 1", true);
final JRadioButton rb2 = new JRadioButton("Option 2", false);
final ButtonGroup group = new ButtonGroup();
group.add(rb1);
group.add(rb2);
p2.add(rb1);
p2.add(rb2);
p.add(p2);
content.add(p, BorderLayout.CENTER);
setVisible(true);
}
}
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Oct 26th, 2008, 04:16 PM
#3
Thread Starter
Member
Re: [RESOLVED] TitledBorders around RadioButton Group?
Thanks for your very helpful reply ComputerJy. One question though: I googled the following code snippet but got no replies, why should the following be included in the program? Thanks in advance:
Code:
private static final long serialVersionUID = 8927395846549342658L;
-
Oct 26th, 2008, 06:01 PM
#4
Re: [RESOLVED] TitledBorders around RadioButton Group?
A serialVersionUID must be specified in all Serializable classes, and since your class extends JFrame which implements the Serializable interface, you must provide one.
BTW it's a randomly generated UID. Doesn't mean anything
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|