Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JCheckBox Border?

 
Chris Shepherd
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know if there is an easy way to put a border around a JCheckBox object including the label? setBorder doesn't seem to have any effect. I know I could put each checkbox inside of a JPanel and border that, but is there no way to border the entire JCheckBox by itself?
Thanks
 
Bryan Fagan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris:
Use the BorderFactory class. This class can create any kind of border. Please see the documentation for BorderFactory @ www.java.sun.com.
EXAMPLE:
/**
* Putting a border around a check box.
*
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Test extends JFrame {
public Test() {
JCheckBox jchk = new JCheckBox();
jchk.setBorder(BorderFactory.createEtchedBorder());
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(1);
}
});
getContentPane().add(jchk);
setSize(50,50);
setVisible(true);
}
public static void main(String[] args) {
Test tst = new Test();
}
}
 
Chris Shepherd
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried that, but it doesn't seem to work. Here is the code i used
button.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED,Color.green,Color.black));
I did also change the background color, but why would that effect the border? even with the code above, I still only get the standard checkbox look. Any other suggestions?
Thanks, Chris
 
Bryan Fagan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris:
I agree. It seems that the JCheckBox can't have Borders. I did find something that might interest you. Use the JCheckBoxMenuItem and the borders and titles work. See the code below.
private JCheckBoxMenuItem button;
private Border raisedB;
private TitledBorder titledB;
public Test() {
button = new JCheckBoxMenuItem();
raisedB = BorderFactory.createRaisedBevelBorder();
titledB = BorderFactory.createTitledBorder(raisedB,"Title");
button.setBorder(titledB);
getContentPane().add(button);
setSize(100,100);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(1);
}
});

}
Maybe this will help.
Bryan
 
Chris Shepherd
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bryan. I got your code to work. I even set up a test of minimal parts of my code and it worked. However, when I try to drop it into my real code it doesn't work. I'm going nuts trying to figure out why. I can change the background color of the button, but it will NOT recognise a border change. Is there any reason why a container would ignore a component's border setting? I am really at a loss. No where else in my code are the button colors adjusted. I am putting instances of this button into a gridlayout inside a JPanel.
Man this sux.
Any input would be appreciated, admired, and cheered (and possibly even oggled a bit if it worked).
Thanks,
Chris
[This message has been edited by Chris Shepherd (edited February 28, 2001).]
 
Bryan Fagan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris:
I created a JPanel with a gridlayout and added the button. It seemed to work ok. Do you have a snippet of your code that I could look at?
Below is some code based on the description from your last mail.
private JCheckBoxMenuItem button;
private JCheckBoxMenuItem button2;
private Border raisedB;
private TitledBorder titledB;
private JPanel pan;
public Test() {
pan = new JPanel();
pan.setLayout(new GridLayout(2,1));
button = new JCheckBoxMenuItem();
button2 = new JCheckBoxMenuItem();
raisedB = BorderFactory.createRaisedBevelBorder();
titledB = BorderFactory.createTitledBorder(raisedB,"Title");
button.setBorder(titledB);
button2.setBorder(titledB);
pan.add(button);
pan.add(button2);
getContentPane().add(pan);
setSize(100,100);
setVisible(true);
Bryan
 
Chris Shepherd
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, I went through all sorts of permutations and even used some UBB Code to present some of my code in a readable format in this message, and as I am working up my message and commenting stuff out I decided to comment out the look-and-feel section of my actual code, just in the off chance that made a difference. It did! When I just use the default L&F, I got the nicest border around my checkboxmenuitem. When I try to set L&F to my OS type(WindowsLookAndFeel), my newfound border is nonexistent. I went back to the test code and inserted the Windows L&F code into it and no border! So problem solved, I guess. Looks like I will have to go with the java L&F if I want those borders.
Thanks for all your help, Bryan.
Chris
 
Bryan Fagan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good info... I was wondering if that L&F would cause problems. Looks like it does every once in a while. I wonder if it's a bug....hmm...
Glad you got it working and thanks for the info.
Bryan
 
Nagurva Reddy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai All,
By default for chaeckbox and radiobutton ,the borderpainted property will false.If you want to set the border to chackbox
setBorderPainted(true).

example:
/**
* Class : CheckBoxBorder.java
* Author : Nagurva Reddy.P
* Date : 10/06/2001
*
*
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
public class CheckBoxBorder extends JFrame {
public CheckBoxBorder() {
JCheckBox jchk = new JCheckBox("CheckBoxBorder");
jchk.setBorderPainted(true);
jchk.setBorder(new LineBorder(Color.blue,2));
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(1);
}
});
getContentPane().add(jchk);
setSize(50,50);
setVisible(true);
}
public static void main(String[] args) {
CheckBoxBorder tst = new CheckBoxBorder();
}
}
 
Kevin Ng
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know how to use the JCheckBox? My idea is when i click the checkbox then the box being tick at the same time a popup dialog box appear.
Thank you!
 
Chris Shepherd
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WHOA you sure dredged up an old one, Kevin. What you want to do sounds fairly straight forward. Read up on the chekbox a bit and then setup an action listener for it that will pop up your dialog box. Keep searching around javaranch to find some code snippets that will help. I'm sure they are there.
Chris
 
c wade
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can call setBorderPainted(true) on the jcheckbox this works for me.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic