Search code examples
javaswingjcheckboxitemlistener

Problems with ItemListener in Java


I am creating a simple Bold And Italic checkbox which will change the font style written in the text field t1. I did this but it dint help.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class S4 extends JFrame implements ItemListener{
    public S4(){
        setSize(300,150);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JCheckBox c1,c2;
        JTextField t1;
        Panel p1 = new Panel();
        c1 = new JCheckBox("Bold",false);
        c2 = new JCheckBox("Italic",false);
        t1 = new JTextField(40);
        p1.add(t1);
        p1.add(c1);
        p1.add(c2);        
        c1.addItemListener(this);
        c1.addItemListener(this);
        getContentPane().add(p1);
    }
    public void itemStateChanged(ItemEvent e){
        Font f;        
        if(c1.isSelected() && c2.isSelected()){
            f = new Font("Aerial",Font.BOLD+Font.ITALIC,13);
        }
        else if (c1.isSelected()){
            f = new Font("Aerial",Font.BOLD,13);
        }
        else if (c2.isSelected()){
            f = new Font("Aerial",Font.ITALIC,13);
       }
       else {
            f = new Font("Aerial",Font.PLAIN,13);
        }
        t1.setFont(f);
    }    
    public static void main(String [] args){
        new S4();
    }
}

Solution

  • Well, first of all you can't declare variables inside one method and then use them in another. You need to declare your JCheckBox c1, c2; and your JTextField t1; in the class itself.

    Also take a look at where you add the listeners, you add the listener two times to c1. Where you want to add it once to c1 and once to c2.

    Here is your source code but with the small changes so it works!

    import java.awt.Font;
    import java.awt.Panel;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    
    public class S4 extends JFrame implements ItemListener {
    
        JCheckBox c1, c2;
        JTextField t1;
    
        public S4() {
             setSize(300,150);
             setVisible(true);
             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
             Panel p1 = new Panel();
             c1 = new JCheckBox("Bold",false);
             c2 = new JCheckBox("Italic",false);
             t1 = new JTextField(40);
             p1.add(t1);
             p1.add(c1);
             p1.add(c2);        
             c1.addItemListener(this);
             c1.addItemListener(this);
             getContentPane().add(p1);
        }
    
        public void itemStateChanged(ItemEvent e){
             Font f;        
             if(c1.isSelected() && c2.isSelected()){
                 f = new Font("Arial",Font.BOLD+Font.ITALIC,13);
             }
             else if (c1.isSelected()){
                 f = new Font("Arial",Font.BOLD,13);
             }
             else if (c2.isSelected()){
                 f = new Font("Arial",Font.ITALIC,13);
             }
             else {
                 f = new Font("Arial",Font.PLAIN,13);
             }
    
             t1.setFont(f);
        } 
    
        public static void main(String [] args){
             new S4();
        }
    }
    

    Also the font you are using are spelled work, the font is spelled "Arial" unless, you are using a custom installed font. Like this once. http://www.dafont.com/aerial.font