Results 1 to 7 of 7
  1. #1
    Javified is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default Customized Radio Button

    I need to show a small, right-pointing, custom-color filled triangle, beside a radio-button's LHS (which would belong to a button-group) which has just gained focus in the group (by key-board arrow-key navigation). [i.e. the triangle would point towards the circle of the radio-button component]. Several alternatives are rushing through my mind w.r.t. a possible implementation. But, before I design it fresh from the grass-root level, I wanted to see if some one has a pointer to some concept-implementation/solution, in the interest of saving some time.

    Some alternatives that I'm considering:

    1. Custom UI delegate creation and then setUI on the Radio-Button.

    2. Packing a parallel, thin, vertical JPanel on the LHS of the circles of the radio-buttons (of the associated radio-group) and then computing the Y-co-ordinate of the center of the circle, of the just-focus-gained radio-button, in its focusGained() event-handler, to compute the offset of the Box (of the vertical BoxLayout) in the LHS-JPanel and then rendering a Path2D-based triangle, which will then be custom-color-filled and then making that box visible (in the LHS JPanel vertical-stack - it will have at most one box visible at a time - the one associated with the focusGained() radio-button) and then, vertical-aligning the LHS-JPanel and the button-group with the Spring Layout (so that they move-together over the main-panel-resizing operation, if undertaken by the user).

    3. Customizing a radio-button, by putting it in a JPanel with horizontal BoxLayout, (whose first box will hold the Java2D-rendered triangle) and making all such JPanel-embedded radio-buttons members of the covering group.

    Please comment on the above and/or please provide some example pointers of similar attempts. Thanks a lot.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    Could be useful to you: Toggle Button Icons Java Tips Weblog

    Also see the API for the various set...Icon methods inherited from AbstractButton.

    db

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    Also see the API for the various set...Icon methods inherited from AbstractButton.
    Actually, as per your requirement and as there isn't a setFocusedIcon method, it may be better to implement the behavior in the icon's paintIcon.
    Java Code:
    import darrylbu.renderer.RadioButtonIcon;
    import java.awt.*;
    import javax.swing.*;
    
    public class RadioButtonFocusIcon {
    
      public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
    
          public void run() {
            new RadioButtonFocusIcon().makeUI();
          }
        });
      }
    
      public void makeUI() {
        JPanel panel = new JPanel(new GridLayout(0, 1));
        ButtonGroup group = new ButtonGroup();
        Icon icon = new ArrowIcon();
        for (int i = 0; i < 5; i++) {
          JRadioButton button = new JRadioButton("Choice" + i);
          button.setIcon(new RadioButtonIcon(button, icon,
                  SwingConstants.LEFT, SwingConstants.CENTER));
          group.add(button);
          panel.add(button);
        }
    
        JFrame frame = new JFrame();
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    }
    
    class ArrowIcon implements Icon {
    
      static final int SIDE = 12;
      static final int[] xPoints = {1, 11, 1};
      static final int[] yPoints = {1, 6, 11};
    
      public void paintIcon(Component c, Graphics g, int x, int y) {
        if (c.isFocusOwner()) {
          Graphics g2 = g.create(x, y, SIDE, SIDE);
          g2.setColor(Color.RED);
          g2.fillPolygon(xPoints, yPoints, 3);
          g2.dispose();
        }
      }
    
      public int getIconWidth() {
        return SIDE;
      }
    
      public int getIconHeight() {
        return SIDE;
      }
    }
    db

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    Now if I had known you weren't really interested, I wouldn't have bothered.

    db

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Typical "one post and out of here" poster.

  6. #6
    Javified is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default

    Hi Darryl: Thanks for posting the pointers and the sample code. Much appreciated. I've already added my nice comments for you with the help of the forum's reputation button. Just started getting a feel for this forum and its mechanisms. BTW: Are you permanently located in Goa or just site-seeing there presently (and avoiding the US's cold weather :-))?

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    Are you permanently located in Goa or just site-seeing there presently (and avoiding the US's cold weather :-))?
    I live and work here. No idea about the weather in the US.

    db

Similar Threads

  1. Disable Radio button
    By AJG in forum New To Java
    Replies: 3
    Last Post: 05-10-2011, 11:09 AM
  2. Clear Radio Button
    By Reborn in forum New To Java
    Replies: 6
    Last Post: 07-25-2010, 05:21 PM
  3. Radio Button help!
    By javanator in forum New To Java
    Replies: 3
    Last Post: 04-25-2010, 08:01 PM
  4. Radio button dot color
    By Psyclone in forum AWT / Swing
    Replies: 3
    Last Post: 02-21-2010, 04:29 AM
  5. How to use SWT Radio Button
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-25-2008, 02:25 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •