0

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.

BombrickJL
  • 11
  • 4
  • Your code doesn't make sense. In your checkbox item listener, you assign an event handler to the button instead of setting the font weight of the button label. – Daniel B Dec 14 '15 at 13:38
  • @DanielB Did you get what I'm trying to do? I'm actually trying to make the button activated by ticking the checkbox and when I click the button It changes the font of the JTextField I already have – BombrickJL Dec 14 '15 at 13:52
  • @BombrickJL Sorry, completely missed the `button1` part. But you can't use an `ItemListener` for a `JButton`. Either use a `JToggleButton` or an `ActionListener`. – Lukas Rotter Dec 14 '15 at 13:59
  • @LuxxMiner ow.. Thanks you fixed my problem . I have been using ItemListener for button instead of actionlistener all the time and that was the problem :v – BombrickJL Dec 14 '15 at 14:03

1 Answers1

0

@LuxxMiner ow.. Thanks you fixed my problem . I have been using ItemListener for button instead of ActionListener all the time and that was the problem.

BombrickJL
  • 11
  • 4