-2

Getting error on line where HandlerClass implements ActionListener saying "Multiple markers at this line - Syntax error on tokens, delete these tokens - Syntax error, insert '}' to complete Block"

What I've got wrong there ?

import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class Gui extends JFrame {

private JTextField tf;
private JCheckBox boldbox;
private JCheckBox italicbox;

public Gui() {
    super("Hakuna Matata");
    setLayout(new FlowLayout());
    tf = new JTextField("This is a sentence");
    tf.setFont(new Font("Serif", Font.PLAIN, 14));
    add(tf);

    boldbox = new JCheckBox("bold");
    italicbox = new JCheckBox("italicbox");
    add(boldbox);
    add(italicbox);

    HandlerClass handler = new HandlerClass();
    boldbox.addActionListener(handler);
    italicbox.addActionListener(handler);
}
private HandlerClass implements ActionListener {
    public void ActionPerformed(ActionEvent event) {
        Font font = null;
        if(boldbox.isSelected() && italicbox.isSelected())
            font = new Font("Serif", Font.BOLD + Font.ITALIC, 14);
        else if(boldbox.isSelected())
            font = new Font("Serif", Font.BOLD, 14);
        else if(italicbox.isSelected())
            font = new Font("Serif", Font.ITALIC, 14);
        else
            font = new Font("Serif", Font.PLAIN, 14);

        tf.setFont(font);

    }
}
Mark Alexa
  • 125
  • 2
  • 9

2 Answers2

0

You were close but off in implementing ActionListener, the correct method signature that you need to override is:

public void actionPerformed(ActionEvent event);

Please note the lowercase a in the start of the method name, as per the Java convention for all method names to start from lower case character.

EDIT

Also, your HandlerClass should be an inner class, so it should be declared as:

private class HandlerClass implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent event) {
        //your logic here
    }
}

Please note that I added the class keyword before the HandlerClass name. Also, I suggest that you rename it to a more appropriate class name, such as Handler instead of HandlerClass.

JChrist
  • 1,734
  • 17
  • 23
0

There are two mistakes. The first is as JChrist describes. To prevent this, you should have

@Override
before the method.

But the real mistake is you're missing a closing curly bracket at the end. You never close the Gui class.

  • I had a bracket there. Before I posted a question here I made sure that I'm not missing a semicolon. Didn't want to waste your time on a rookie mistake but I removed semicolon 'coz I was getting error saying get rid off it so I did despite I didn't understand why 'coz I was sure it should be there. – Mark Alexa Feb 02 '17 at 20:35