Focus rectangle around jCheckBox

Hi all,
I have a JCheckBox with no label text with it. Usually when the JCheckBox has focus, theres a dotted focus rectangle around the label text, so in this case there never is.
Since theres a few checkboxes in the same panel, it gets very confusing as you can't see what component is in focus. Is there any way about this? I was trying to create my own checkbox with a dotted border when the component gets focus, but theres no dotted border in swing.
So my question is this:
1) Is there any way to change the focus rectangle to go about the whole component and not just the text label?
or
2) Is there any dotted border in swing that I've missed?
thanks,
Justin

what about having blank spaces as the text (the # of spaces determines the rectangle width)
something like this
import java.awt.*;
import javax.swing.*;
class Testing extends JFrame
  JCheckBox cbx1 = new JCheckBox("      ");
  JCheckBox cbx2 = new JCheckBox("      ");
  public Testing()
    setLocation(400,300);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    JPanel p = new JPanel();
    p.add(cbx1);
    p.add(cbx2);
    getContentPane().add(p);
    pack();
  public static void main(String[] args){new Testing().setVisible(true);}
}

Similar Messages

  • JTable Focus Rectangle

    When navigating around my table the focus recatangle is not shown - but the correct cell is selected.
    A single click selects a cell, but I need to double click to see the focus triangle.
    I would like the focus rectangle to appear when the cell is selected.
    Any ideas?

    Actually I was't thinking clearly when you asked about a "focus rectangle (which is why I always recommend a simple demo program that shows your problem). I thought you where asking about the "dotted line focus rectangle" that appears on components like JButton.
    Anyway here is a simple table and not that the "focus rectangle" appears on all cells except the column with boolean values.
    import java.awt.*;
    import java.util.*;
    import javax.swing.*;
    public class TableBasic extends JFrame
         public TableBasic()
              String[] columnNames = {"Date", "String", "Integer", "Boolean"};
              Object[][] data =
                   {new Date(), "A", new Integer(1), Boolean.TRUE },
                   {new Date(), "B", new Integer(2), Boolean.FALSE},
                   {new Date(), "C", new Integer(9), Boolean.TRUE },
                   {new Date(), "D", new Integer(4), Boolean.FALSE}
              JTable table = new JTable(data, columnNames)
                   //  Returning the Class of each column will allow different
                   //  renderers to be used based on Class
                   public Class getColumnClass(int column)
                        return getValueAt(0, column).getClass();
              table.setPreferredScrollableViewportSize(table.getPreferredSize());
              JScrollPane scrollPane = new JScrollPane( table );
              getContentPane().add( scrollPane );
         public static void main(String[] args)
              TableBasic frame = new TableBasic();
              frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
              frame.pack();
              frame.setLocationRelativeTo( null );
              frame.setVisible(true);
    }The default renderer for boolean values uses a JCheckBox to render the value. the check box uses setBorderPainted(false), which is why "focus rectangle" is not painted. Personally, I think this is a bug.
    Anyway, if you want the "focus rectangle" then you would need to create your own Boolean renderer, setBorderPainted(true) and provide a default EmptyBorder for the check box:
    Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);

  • JButton & Win XP Look and Feel focus rectangle bug??

    I searched the bug database but was unable to find any matches...
    I have received a bug report from our users relating to mistakenly thinking a button has focus. In the XP LAF, when a button is a 'default' button, it has a blue rectancle drawn around it (which is correct). When you tab to that button however, there is no additional 'focus' rectangle to indicate that the button has focus. I tested this in native XP components and an additional rectancle IS drawn when the button gets focus (in addition to the blue rectangle). Shouldn't there be some sort of focus indicator in addition to the blue rectangle? I'm using JRE version 1.4.2_02.
    Thanks,
    Andy

    Hello Andy!
    If the behavior of Win XP LAF is different from the native this is definitely a bug. It would be greate if you file a bug here
    http://java.sun.com/webapps/bugreport.
    Thank you.
    Anton

  • Custom focus rectangle in JTextField

    I'm trying to change my theme/look & feel to show a focus rectangle for JTextField, somewhat like this: [http://www.macvswindows.com/index.php?title=GUI_Customization|http://www.macvswindows.com/index.php?title=GUI_Customization] (second image down, or direct [http://www.macvswindows.com/images/thumb/f/f2/osx_aqua_colors.png/500px-osx_aqua_colors.png|http://www.macvswindows.com/images/thumb/f/f2/osx_aqua_colors.png/500px-osx_aqua_colors.png] ) I don't want to make it exactly like MacOS, but a similar idea.
    For now I'm just trying to do it on the inner part of the field. I can get it to paint with the code below, but then the caret painting messes things up as I tab through the fields or move the caret around inside the fields.
    So it seems I'm headed down the wrong path, what should I be doing? Extending DefaultHighlighter or DefaultCaret, or something else?
    Thanks,
    Jim
    package testui;
    import javax.swing.*;
    import javax.swing.plaf.metal.DefaultMetalTheme;
    import javax.swing.plaf.metal.MetalLookAndFeel;
    public class ThemeTest extends JFrame {
        public class TestMetalTheme extends DefaultMetalTheme {
            public void addCustomEntriesToTable(UIDefaults table) {
                UIManager.put("TextFieldUI", "customui.CustomTextFieldUI");
            public String getName() {
                return "TestMetalTheme";
        public static void main(String[] args) {
            ThemeTest testFrame = new ThemeTest();
            testFrame.setVisible(true);
        public ThemeTest() {
            try {
                MetalLookAndFeel.setCurrentTheme(new TestMetalTheme());
                UIManager.setLookAndFeel(new MetalLookAndFeel());
            } catch (Exception e) {
                e.printStackTrace();
            setSize(500, 500);
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            JPanel panel = new JPanel();
            add(panel);
            panel.add(new JTextField("test"));
            panel.add(new JTextField("test"));
            panel.add(new JTextField("test"));
            panel.add(new JButton("button"));
    package testui;
    import javax.swing.*;
    import javax.swing.plaf.ComponentUI;
    import javax.swing.plaf.metal.MetalTextFieldUI;
    import java.awt.*;
    public class CustomTextFieldUI extends MetalTextFieldUI {
        private Color focusColor = Color.cyan;
        private JComponent component;
        public CustomTextFieldUI(JComponent c) {
            if (c == null) {
                throw new IllegalArgumentException("component must be specified");
            this.component = c;
        public static ComponentUI createUI(JComponent c) {
            return new CustomTextFieldUI(c);
        protected void paintSafely(Graphics g) {
            super.paintSafely(g);
            if (component.hasFocus()) {
                Dimension size = component.getSize();
                Graphics2D g2d = (Graphics2D) g;
                System.out.println(g2d.getClip());
                System.out.println(g2d.getClipBounds());
                g2d.setColor(focusColor);
                g2d.setStroke(new BasicStroke(2f));
                g2d.drawRect(1, 1, size.width - 3, size.height - 3);
    }

    Why not just combine the two approaches?
    import java.awt.*;
    import java.awt.event.FocusEvent;
    import java.awt.event.FocusListener;
    import javax.swing.*;
    import javax.swing.border.BevelBorder;
    import javax.swing.border.Border;
    import javax.swing.plaf.ComponentUI;
    import javax.swing.plaf.metal.MetalTextFieldUI;
    public class CustomTextFieldUI extends MetalTextFieldUI {
         private Color focusColor = Color.cyan;
         private JComponent component;
         private Border normalBorder;
         private Border focusedBorder;
         private FocusListener focusListener;
         public CustomTextFieldUI( JComponent c ) {
              if ( c == null ) {
                   throw new IllegalArgumentException( "component must be specified" );
              this.component = c;
              focusListener =  new FocusListener() {
                   public void focusGained( FocusEvent fe ) {
                        if ( normalBorder == null ) {
                             normalBorder = component.getBorder();
                             focusedBorder = new BevelBorder( BevelBorder.LOWERED ) {
                                  @Override
                                  public Color getHighlightInnerColor() { return focusColor; }
                                  @Override
                                  public Color getHighlightOuterColor() { return focusColor; }
                                  @Override
                                  public Color getShadowInnerColor() { return focusColor; }
                                  @Override
                                  public Color getShadowOuterColor() { return focusColor; }
                        component.setBorder( focusedBorder );
                   public void focusLost( FocusEvent fe ) {
                        component.setBorder( normalBorder );
         public static ComponentUI createUI( JComponent c ) {
              return new CustomTextFieldUI ( c );
         @Override
         protected void installListeners() {
              super.installListeners();
              component.addFocusListener( focusListener );
         @Override
         protected void uninstallListeners() {
              super.uninstallListeners();
              component.removeFocusListener( focusListener );
    }

  • Viewing PDF in preview, black rectangles around links

    I've created a PDF in Microsoft Word 7.0, the PDF looks great in all applications except Preview on a mac. It would seem that all links in the PDF have a black rectangle around them, this only seems to be an issue in Preview, is this a known issue? As near as I can tell, no other applications seem to be interpreting these links as having boxes around them, this would lead me to believe that the problem does not lay with how the PDF was created.

    Hi,
    Can you try any other PDF file in Preview (not the one you created)
    Dimaxum

  • How to make a rectangle around activeDocument.selection?

    Hi,everyone!
    Recently I met a awful problem. I want to make a rectangle around current selection, but Not success.
    Could you help me or give some clues? Thanks!

    Try the following code.
    Cheers Daniel
    boundingbox();
    function boundingbox()
        // zet nulpunt linksonder
        app.activeDocument.pageOrigin = Array(0,0);
        app.activeDocument.rulerOrigin = Array(0,0);
        // set variable
        var breedte = app.activeDocument.width;
        var hoogte = app.activeDocument.height;
        var doc = app.activeDocument;
        // omrekenfactor
        var omf = 2.834645;
        // maak lijnkleur aan
        var newRGBColor = new RGBColor();
        newRGBColor.red = 0;
        newRGBColor.green = 255;
        newRGBColor.blue = 0;
        var black = new GrayColor();
        black.gray = 100;
        var layercheck = 0;
        var targetDocument = app.activeDocument;
        var layerCount = targetDocument.layers.length;
        var a=1;
        for (var i=1 ; i < 2; i++) {
            if (a==1){var txt = ''; var layname = 'BB';};
            var layercheck = 0;
            for (u = layerCount -1; u >= 0; u--){
                targetLayer = targetDocument.layers[u];
                var layerName = new String (targetLayer.name);
                    if (layerName.indexOf(layname) == 0){
                    targetDocument.layers[u].visible= true;
                    targetDocument.layers[u].locked= false;
                    var layercheck = 1;
            if (layercheck == 0){
                newlayer = documents[0].layers.add();
                newlayer.name = (layname);
                newlayer.color = newRGBColor;   
        alignType = 'top';
        mySelection = activeDocument.selection;
            if (mySelection.length > 0) {
                anchorBounds = getPosition(mySelection[0]);
                var artLayer = doc.layers['BB'].groupItems.add();
                targetDocument.layers['BB'].preview = false;
                var breedte = bnds[2] - bnds[0];
                var hoogte = bnds[1] - bnds[3];
                var rect = artLayer.pathItems.rectangle ((bnds[1] + (20*omf)),(bnds[0] - (20*omf)), breedte + (40*omf), hoogte + (40*omf));
                rect.filled = false;
                rect.stroked = false;
            } else {
                anchorBounds = getDocPosition();
                var doc = app.activeDocument;
                var omtrekartwork = app.activeDocument.geometricBounds;
                var leftartwork = omtrekartwork[0];
                var topartwork = omtrekartwork[1];
                var rightartwork = omtrekartwork[2];
                var bottomartwork = omtrekartwork[3];
                var origin = doc.rulerOrigin;
                var leftpage    = -origin[0];
                var toppage    = -origin[1] + doc.height;
                var rightpage    = -origin[0] + doc.width;
                var bottompage    = -origin[1];
                var centerxpage    = -origin[0] + doc.width/2;
                var centerypage    = -origin[1] + doc.height/2;
                var bleedleft = leftpage - leftartwork;
                var bleedright = rightartwork - rightpage;
                var bleedtop = topartwork - toppage;
                var bleedbottom = bottompage - bottomartwork;
                var breedteartwork = rightpage - leftpage;
                var hoogteartwork = toppage - bottompage;
                if (bleedleft <= 0 && bleedright <= 0 && bleedtop <= 0 && bleedbottom <=0){
                        var artLayer = doc.layers['BB'].groupItems.add();
                        targetDocument.layers['BB'].preview = false;
                        var rect = artLayer.pathItems.rectangle ((toppage + (20*omf)),(leftpage - (20*omf)), (breedteartwork + (40*omf)), (hoogteartwork + (40*omf)));
                        rect.filled = false;
                        rect.stroked = false;
                } else {
                        if    (bleedleft >= bleedright){
                            var bleedx = bleedleft + (20*omf);
                        } else {
                            var bleedx = bleedright + (20*omf);
                        if    (bleedbottom >= bleedtop){
                            var bleedy = bleedbottom + (20*omf);
                        } else {
                            var bleedy = bleedtop + (20*omf);
                        var artLayer = doc.layers['BB'].groupItems.add();
                        targetDocument.layers['BB'].preview = false;
                        var rect = artLayer.pathItems.rectangle ((toppage + bleedy),(leftpage - bleedx), (breedteartwork + (2*bleedx)), (hoogteartwork + (2*bleedy)));
                        rect.filled = false;
                        rect.stroked = false;
    function getBounds(obj) {
        var vbounds = new Array();
        if( obj.typename == 'TextFrame' ) {
            objcopy=obj.duplicate().createOutline();
            vbounds = objcopy.visibleBounds;
            objcopy.remove();
        else if( (obj.typename == 'GroupItem') && !obj.clipped ) {
            vbounds = getBounds(obj.pageItems[0]);
            for( n=1; n<obj.pageItems.length; n++  ) {
                vgb = getBounds(obj.pageItems[n]);
                if( vbounds[0] > vgb[0] ) vbounds[0] = vgb[0];
                if( vbounds[1] < vgb[1] ) vbounds[1] = vgb[1];
                if( vbounds[2] < vgb[2] ) vbounds[2] = vgb[2];
                if( vbounds[3] > vgb[3] ) vbounds[3] = vgb[3];
        else if( (obj.typename == 'GroupItem') && obj.clipped && (obj.pageItems.length > 1) ) {
            vbounds = getBounds(obj.pageItems[0]);
            cvgb = getBounds(obj.pageItems[1]);
            objleft = cvgb[0];
            objtop = cvgb[1];
            objright = cvgb[2];
            objbottom = cvgb[3];
            obj.pageItems[0].stroked ? so = 0 : so = 1;
            for( n=so; n<obj.pageItems.length; n++ ) {
                cvgb = getBounds(obj.pageItems[n]);
                if( objleft > cvgb[0] ) objleft = cvgb[0];
                if( objtop < cvgb[1] ) objtop = cvgb[1];
                if( objright < (cvgb[2]) ) objright = cvgb[2];
                if( objbottom > (cvgb[3]) ) objbottom = cvgb[3];
            if( vbounds[0] < objleft ) vbounds[0] = objleft;
            if( vbounds[1] > objtop ) vbounds[1] = objtop;
            if( vbounds[2] > objright ) vbounds[2] = objright;
            if( vbounds[3] < objbottom ) vbounds[3] = objbottom;
        else {
            vbounds = obj.visibleBounds;
        return vbounds;
    function getPosition(obj) {
        var b = new Array();
        bnds = getBounds(obj);
        b['left']    = bnds[0];
        b['top']    = bnds[1];
        b['right']    = bnds[2];
        b['bottom']    = bnds[3];
        b['centerX']    = bnds[0] + (bnds[2]-bnds[0])/2;
        b['centerY']    = bnds[1] - (bnds[1]-bnds[3])/2;
        return b;
    function getDocPosition() {
        var b = new Array();
        doc = activeDocument;
        var origin = doc.rulerOrigin;
        b['left']    = -origin[0];
        b['top']    = -origin[1] + doc.height;
        b['right']    = -origin[0] + doc.width;
        b['bottom']    = -origin[1];
        b['centerX']    = -origin[0] + doc.width/2;
        b['centerY']    = -origin[1] + doc.height/2;
        return b;

  • How do I put a thin 1px rectangle around an image?

    Using Photoshop CS5, can someone tell me how I put a thin 1px rectangle around an existing image
    For example, imagine I have an image that 500px wide and 200 px high
    I want a 10px gap all around and then I want the thin rectangle
    What would the proper way of doing this?
    Importantly, the image will be a PNG, so I want the gap around the image to be transparent
    I'm planning to have on a black background, so want my rectangle to be a white line
    + is Photoshop the correct application to use to do this?
    Thanks in advance
    Omar

    lee... i'm really happy with the results
    only a small thing though: the outline sint 1px
    i only noticed that afterwards
    se the following 2 pics below
    the first was created using ur method
    the second was created by a longer manual process: say we have 100 x 100 pic. create a new pic with transparent background of 122 x 122. now create a white blank pic of 120 x 120 size. copy the white and put into 122x122. no from the layers menu, add a 1px stroke. now flatten. now erase the white - u now have the required outline!. create a new pic with transparent background of size 122x122. copy and paste both th eoriginal pic and new outline pic. hey presto, 50 steps later, u have what i wanted.
    problem is it's 50 steps
    i like ur solution. this can be automated. the second solution can't be automated: BUT it gives the 1px outline i wanted!
    let me know what u think
    thanks
    first pic:
    second pic:

  • Turning off the annoying green rectangle around Java windows

    How does one turn off the annoying flashing green rectangle around Java windows? And how do you stop it from jumping to the top of other windows.
    Both of these functions are quite annoying and seem rather pointless.
    (I am using Firefox)
    Thanks

    Are you referring to some Applet that you have programmed or something you found on the net? Even for the latter case, I don't have either of the problems you mention, so it's probably related to the site or your browser settings.
    Please note that these forums are for support to developers and students of Java, not for every java product ever released.
    db

  • ComboBox focus rectangle

    I m using Combobox and when I activated accesability by Tab,
    The focus rectangles apears in a grean color, how do I change the
    color of that rectangle.. (I want it to Yellow, like it comes for
    Buttons etc..)
    Regards
    Sidharth

    nobody replied

  • Focus rectangle

    Can someone tell me how to make a focus rectangle visible
    without tabbing. i.e. via code something like that below:
    I know its possible because I've done it before and can't
    remember how I did it.
    Thanks in advance.

    I read through my post and thought it wasn't very clear what
    I was asking. Basically I've created code before that would show a
    focus rectangle if a user clicked in that field with the mouse. Now
    I'm trying to do that again and can't remember how I did it.

  • Funny Black Rectangle Around Every Selection

    A friend has a black rectangle around anything she selects in list or icon. Trashed com.apple.finder.plist, com.apple.desktop. No change. Though it's a bit hard to describe, in list view when selecting a file or folder name the black rectangle starts as far left as the window allows and stops at the right end of the name tab. It does not include the date, kind, size etc only the name.
    On the desktop, selecting an icon puts a black rectangle around the icon.
    There is no Color Label selected.
    Any ideas?
    MacOS 10.4.10, G5 Dual.

    Wow, that worked. When I told her she said she had been in System Preferences and had clicked Voice Over On and forgot about it. Volume control was off.
    THANKS!

  • A rectangle around my mouse cursor

    Since the last two days a rectangle around my mouse cursor is appearing. When i move my mouse and as it touches the wall of the rectangle it also moves and so my mouse cursor never comes out of that rectangle. Incidently when i login using a different account, i don't see this rectangle. It must be a program, i hope it should not be virus.
    Any solution please ?

    System preferences --> Universal Access --> Zoom Options button --> set maximum zoom to far left and/or uncheck the "Show prieview rectangle when zoomed out" checkbox
    Update:
    Here's a X Lab article on this topic:
    Black rectangle appears after Mac OS X 10.4.8 Update

  • Create a rectangle around object?

    Can this be done in illustrator scripting??
    http://coreldraw.com/forums/t/35280.aspx
    creating a rectangle around selected object or objects and set margins...
    Regards
    Martin

    Hallo martin_Cahill,
    My English is not the best, because of that in German.
    Grundsätzlich sind sich VBA und Javascript sehr ähnlich. Versuche die folgende Funktion mit den VBA-Äquivalenten zu vergleichen. Learning by doing
    function makeRect() {
    if (selection.length == 0) {
        alert("Kein Objekt …");
        return;
    }else{
        if (selection.length > 1) {
            alert("Zu viele Objekte …");
            return;
        }else{
            var myDoc = app.activeDocument;
            var Sel = myDoc.selection
            var SelVB = Sel[0].visibleBounds;
            var dMarg = 28.3464567;
            var Links = SelVB[0];
            var Oben = SelVB[1];
            var SelBreite = (SelVB[2] - SelVB[0]);
            var SelHoehe = (SelVB[1] - SelVB[3]);
            var newCMYK = new CMYKColor();
            newCMYK.cyan = 100;
            newCMYK.magenta = 0;
            newCMYK.yellow = 0;
            newCMYK.black = 0;
            var myGroup =myDoc.groupItems.add();
            Sel[0].move(myGroup, ElementPlacement.PLACEATBEGINNING);
            var MargBox = myDoc.pathItems.rectangle(Oben+dMarg, Links-dMarg, SelBreite+2*dMarg, SelHoehe+2*dMarg);
            MargBox.stroked = true;
            MargBox.strokeWidth = 10;
            //MargBox.filled = false;
            MargBox.fillColor = newCMYK;
            MargBox.move(myGroup, ElementPlacement.PLACEATEND);
    Hilft dir das weiter?
    Viel Spass noch.

  • Drawing a rectangle around a group of components

    I want to draw a rectangle around a group of rectangular adjacent components that are in a Canvas.  I can't seem to find the right component to do this, or I am just not doing it properly.  I tried using a Canvas inside the Canvas with the inner borders set and adding the components to the inner Canvas and placing the inner Canvas at the location of the components it now contains, but it doesn't show up.  I also tried a transparent Canvas with backgroundAlpha set to 0.  Canvas is probably not the proper component to do this with.  Is there an easier way?

    I want to draw a rectangle around a group of rectangular adjacent components that are in a Canvas.  I can't seem to find the right component to do this, or I am just not doing it properly.  I tried using a Canvas inside the Canvas with the inner borders set and adding the components to the inner Canvas and placing the inner Canvas at the location of the components it now contains, but it doesn't show up.  I also tried a transparent Canvas with backgroundAlpha set to 0.  Canvas is probably not the proper component to do this with.  Is there an easier way?

  • HToggleButton increase width of focus rectangle

    Hi, can anyone tell me how to increase the width of focus rectangle when any component like HToggleButton is focused

    Hi, can anyone tell me how to increase the width of focus rectangle when any component like HToggleButton is focused

Maybe you are looking for

  • I have a 2nd Display that turns purple.

    I thought this was a problem with the Samsung display and bought a Acer replacement.  The Acer turns purple too on occasion.

  • CUP - Comments not saving on request.

    Hi All, We are having an issue on CUP.  We have setup the comments field as mandatory when the request is either approved or rejected. When the approver enters the comments and approves the requests, CUP saves the time and date of approval, however i

  • Firefox 5 does not allow me to use McAfee site advisor. How can I download previous version?

    McAfee site advisor is important to me and the new Firefox 5 does not allow it's use. Can you please let me know if I can use previous version of Firefox and how can I get download. Perhaps you could e-mail me when problem has been resolved and I can

  • Cannot create new mailing list

    I have a large addressbook in TB with many mailing lists created in the past. I am trying to create a new one. When I go "File, New", the mailing list option is blanked out.

  • Strange error in calculator

    Hi community, there is a strange error in calculator or in my system. If I use the -/+ in the calculator it changes the number. Try this. Put in 51,99 and hit the -/+ sign. In my system it changes the number to -51,98999999999999. I also tried it for