I don't get the result I want in this code, which is when I click on the checkbox then the JTextField's text should be bold, but It doesn't happen. Help me please.
public class GUI extends JFrame{
//CheckBoxs
private JCheckBox box1;
private JCheckBox box2;
//Buttons
private JButton button1;
private JButton button2;
//Text
private JTextField fnam;
private JTextField snam;
public GUI(){
//basic
super("Program");
setLayout(new FlowLayout());
//add
box1 = new JCheckBox("Button 1 Activate");
box2 = new JCheckBox("Button 2 Activate");
add(box1);
add(box2);
button1 = new JButton("Select First Name");
button2 = new JButton("Select Second Name");
add(button1);
add(button2);
fnam = new JTextField("Mena Farid",10);
snam = new JTextField("Malak Mamdoh",12);
fnam.setEditable(false);
snam.setEditable(false);
fnam.setFont(new Font("Serif",Font.PLAIN,10));
snam.setFont(new Font("Serif",Font.PLAIN,12));
add(fnam);
add(snam);
//ActionListener and Events
Handler Handle = new Handler();
Handler2 Handle2 = new Handler2();
box1.addItemListener(
new ItemListener(){
public void itemStateChanged(ItemEvent e){
if(box1.isSelected())
button1.addItemListener(Handle);
}
}
);
box2.addItemListener(
new ItemListener(){
public void itemStateChanged(ItemEvent e){
if(box2.isSelected())
button2.addItemListener(Handle2);
}
}
);
}
private class Handler implements ItemListener{
public void itemStateChanged(ItemEvent f){
Font font = null;
font = new Font("Serif",Font.BOLD,10);
fnam.setFont(font);
}
}
private class Handler2 implements ItemListener{
public void itemStateChanged(ItemEvent f){
Font font2 = null;
font2 = new Font("Serif",Font.BOLD,12);
snam.setFont(font2);
}
}
}
The problem is The JTextField format is supposed to be turned into bold after I tick the checkbox and click the button but It doesn't happen.