0

I want to create a radio button that has more components than the default JRadioButton. I'm wondering what the best method is, should I subclass JRadioButton adding my extra components or is it better to subclass JPanel and add the behavior of a radio button as shown below.

---------JPanel---------------
- Titel (Label)              -
- RadioButton (JRadioButton) -
- Description (label)        -
------------------------------

The whole panel should be focusable and should provide feedback when clicked or has focus. Thanks for your help.

DjSol
  • 208
  • 1
  • 11
  • *"should provide feedback"* How exactly? If the `JRadioButton` is the only focusable element, it will get focus if the panel is focused, but what exactly would change if the user clicks on a `JLabel`? *"things which are darn easy in WPF seem to be difficult in swing"* And things which are possible in Swing are impossible in WPF (e.g. creating a cross-platform GUI). More generally though, you do not encourage people to help by complaining about 'how things are hard in (the language of discussion) as opposed to (some other language)'.. – Andrew Thompson Mar 21 '14 at 09:25
  • @AndrewThompson "How exactly?" By setting the border of the JPanel for example: when pressed set lowered border, when released set 'normal' border. This provides the user with visual feedback. Thanks for your feedback. – DjSol Mar 21 '14 at 09:36
  • A JPanel is just a container, it's not a component, therefore you can't attach an ActionListener to a JPanel. – Anto Mar 21 '14 at 09:42
  • @Anto but you can add a FocusListener a KeyListener and a MouseListener...should be enough, right? – DjSol Mar 21 '14 at 10:14

1 Answers1

1

None of the above.

  • Don't extend components unless you genuinely need to extend functionality, especially not merely for decoration.

  • Don't confuse the containment hierarchy with the class hierarchy.

  • Don't defeat the existing focus subsystem unless you plan to provide your own.

  • If you want to alter the appearance of the panel when the radio button's focus changes, do so in a focus listener attached to the button. The background color of an opaque panel is a bound property, so setBackground() should be sufficient.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045