Welcome, Guest. Please login or register.
Did you miss your activation email?
Pages: [1]   Go Down
  Print  
Author Topic: problem in JRadioButton  (Read 805 times)
0 Members and 1 Guest are viewing this topic.
JavaLearner
Member
*

Reputation: 0
Offline Offline
Posts: 34
Referrals: 0

Awards
« on: July 11, 2009, 08:40:14 AM »

I'm trying to implement simple sample about using JRadioButton and i don't know Why some Buttons works and others not
That is my code...
Code
GeSHi (java):
  1. import java.awt.FlowLayout;
  2. import java.awt.Font;
  3. import java.awt.event.ItemEvent;
  4. import java.awt.event.ItemListener;
  5. import java.lang.reflect.InvocationTargetException;
  6. import javax.swing.ButtonGroup;
  7. import javax.swing.JFrame;
  8. import javax.swing.JRadioButton;
  9. import javax.swing.JTextField;
  10. import javax.swing.SwingUtilities;
  11.  
  12.  
  13. public class RadioButtonFrame extends JFrame {
  14.  
  15. private static final long serialVersionUID = -4257293733892482733L;
  16. private JTextField textField = null;
  17.  
  18. private Font plainFont = null;
  19. private Font italicFont = null;
  20. private Font boldFont = null;
  21. private Font italic_bold_Font = null;
  22.  
  23. private JRadioButton plainRadioButton ;
  24. private JRadioButton italicRadioButton;
  25. private JRadioButton boldRadioButton;
  26. private JRadioButton italicboldRadioButton;
  27.  
  28. private ButtonGroup radioGroup ;
  29.  
  30. public RadioButtonFrame()
  31. {
  32. super("Testing RadioButton");
  33.  
  34. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  35. setSize(300, 100);
  36. setLayout(new FlowLayout());
  37.  
  38. textField = new JTextField("Watch the font style change",25);
  39. add(textField);
  40.  
  41. plainRadioButton = new JRadioButton("Plain",true);
  42. italicRadioButton = new JRadioButton("Italic",false);
  43. boldRadioButton = new JRadioButton("Bold",false);
  44. italicboldRadioButton = new JRadioButton("Bold/Italic",false);
  45.  
  46. add(plainRadioButton);
  47. add(italicRadioButton);
  48. add(boldRadioButton);
  49. add(italicboldRadioButton);
  50.  
  51. //Create Logical relationship between JRadioButton.
  52. radioGroup = new ButtonGroup();
  53. radioGroup.add(plainRadioButton);
  54. radioGroup.add(italicRadioButton);
  55. radioGroup.add(boldRadioButton);
  56. radioGroup.add(italicboldRadioButton);
  57.  
  58. //Create font objects.
  59. plainFont = new Font("Serif", Font.PLAIN, 14);
  60. italicFont = new Font("Serif", Font.ITALIC, 14);
  61. boldFont = new Font("Serif", Font.BOLD, 14);
  62. italic_bold_Font = new Font("Serif", Font.BOLD + Font.ITALIC  , 14);
  63. textField.setFont(plainFont);
  64.  
  65. //Register events for JRadioButton.
  66. plainRadioButton.addItemListener(new RadioButtonHandler(plainFont));
  67. italicboldRadioButton.addItemListener(new RadioButtonHandler(italicFont));
  68. boldRadioButton.addItemListener(new RadioButtonHandler(boldFont));
  69. italicboldRadioButton.addItemListener(new RadioButtonHandler(italic_bold_Font));
  70.  
  71. setVisible(true);
  72. }
  73.  
  74. private class RadioButtonHandler implements ItemListener{
  75. private Font font = null ;
  76.  
  77. public RadioButtonHandler(Font f){
  78. font = f ;
  79. }
  80.  
  81. @Override
  82. public void itemStateChanged(ItemEvent e) {
  83. textField.setFont(font);
  84. }
  85.  
  86. }
  87.  
  88. public static void main(String[] args) throws InterruptedException, InvocationTargetException {
  89. SwingUtilities.invokeAndWait(new Runnable() {
  90.  
  91. @Override
  92. public void run() {
  93. new RadioButtonFrame();
  94. }
  95. });
  96.  
  97. }
  98.  
  99. }
Created by GeSHI 1.0.7.20

Another thing about Generated Code for Class what's the benefit about this
Is this like CLSID in COM Objects in C++ programming.

Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Your Ad Here