Results 1 to 7 of 7
Thread: Customized Radio Button
- 12-06-2010, 11:34 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 2
- Rep Power
- 0
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.
- 12-07-2010, 10:05 AM #2
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
- 12-07-2010, 10:35 AM #3
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.Also see the API for the various set...Icon methods inherited from AbstractButton.dbJava 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; } }
- 12-11-2010, 04:55 AM #4
Now if I had known you weren't really interested, I wouldn't have bothered.
db
-
Typical "one post and out of here" poster.
- 12-12-2010, 01:25 AM #6
Member
- Join Date
- Dec 2010
- Posts
- 2
- Rep Power
- 0
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 :-))?
- 12-13-2010, 07:06 AM #7
Similar Threads
-
Disable Radio button
By AJG in forum New To JavaReplies: 3Last Post: 05-10-2011, 11:09 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 -
Radio button dot color
By Psyclone in forum AWT / SwingReplies: 3Last Post: 02-21-2010, 04:29 AM -
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