title: Onclick event in Java
body: <pre><code>import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  
  public class Prg_38 extends JFrame{
  
      JTextArea area = new JTextArea(5,10);                   // 5 rows and 10 colums
      JButton btn1 = new JButton("TextArea");             // name of JBUtton is TextArea
      JButton btn2 = new JButton("TextField");            // name of JButton is TextField
      JRadioButton bold,italic,plain,bold_italic;         // radio buttons
      JTextField fld = new JTextField(15);                // jtextfield is 15 characters long
  
      Font bf = new Font("Serif",Font.BOLD, 14);
      Font itf = new Font("Serif",Font.ITALIC, 14);
      Font itbf = new Font("Serif",Font.BOLD + Font.ITALIC, 14);
  
      GridBagConstraints gc = new GridBagConstraints();       // used to place components on GUI
  
      public Prg_38() {
  
          super("Interface");                                 // title of Dialog box
          setLayout(new GridBagLayout());         
  
          gc.insets = new Insets(0,0,2,2);                        // margin between components of GUI (top,left,right,bottom)
  
          // textarea
          area.setText("textarea");          // default text in textarea
          addComponent(area,0,0);            // method to place components on GUI
  
          // jbutton
  
          addComponent(btn1,1,0);            // textare
          addComponent(btn2,2,0);            // textfield
  </code></pre>
  
  <p>After adding the components to the interface, I'm adding action listeners to every component so that when text area button is clicked, focus changes to text area and when text field button is clicked, focus changes to text field.</p>
  
  <pre><code>        //radio buttons
  
          bold = new JRadioButton("Bold",false);              // name of radio button is Bold
          addComponent(bold,1,1);
  
          italic = new JRadioButton("Italic",false);          // name of radio button is Italic
          addComponent(italic,2,1);
  
          plain = new JRadioButton("Plain",false);                // name of radio button is Plain
          addComponent(plain,1,2);
  
          bold_italic = new JRadioButton("Bold and Italic",true);     // // name of radio button is Bold and Italic
          addComponent(bold_italic, 2, 2);
  
          ButtonGroup group = new ButtonGroup();              // it makes sure that only 1 radiobutton is enabled at a time
          group.add(bold);
          group.add(bold_italic);
          group.add(italic);
          group.add(plain);
  
          // when radio buttons are added to a group, only one radio button can be selected at a time.
  
          // jtextfield
  
          fld.setText("enter your name");                 // default text
          addComponent(fld,0,3);
  
          // action listeners
  
          thehandler handler = new thehandler();          // action listener class
  
          btn1.addActionListener(handler);                // textarea action listener
          btn2.addActionListener(handler);                // textfield 
  
  
          bold.addItemListener(new handler1(bf));
          italic.addItemListener(new handler1(itf));
  
  
          setVisible(true);               // making GUI visible
          setSize(400,400);               // pixels (x * y)
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);             // Frame quits when X is clicked on top right corner
  
      }
  
      public class thehandler implements ActionListener{
  
          public void actionPerformed(ActionEvent e) {
  
              if(e.getSource() == btn1) {
  
                  // change focus to textarea named area
  
                  area.requestFocusInWindow();
  
              }
  
  
              if(e.getSource() == btn2) {
  
                  fld.requestFocusInWindow();
              }               
  
  
          }
  
      }
  </code></pre>
  
  <p>Now i want is that if bold radio button is clicked, whichever component is selected(textarea and textfield), the text of that component changes to bold text</p>
  
  <pre><code>    public class handler1 implements ItemListener{
  
          Font font; 
  
          public handler1(Font f) {
  
              font = f;
          }
  
          public void itemStateChanged(ItemEvent event) {
  
              if((area.isFocusOwner() == true) &amp;&amp; (event.getSource() == bold)) {
  
                  area.setFont(font);
              }
  
          }
      }
  
      public void addComponent(Component c, int px, int py) {
  
          gc.gridx = px;                      // column
          gc.gridy = py;                      // row
  
          add(c, gc);             // add components(textarea, radio button and all other stuff) to GUI
      }
  
      public static void main(String[] args) {
  
          new Prg_38();
  
      }
  
  }
  </code></pre>