0

I have this code:

public class LaunchScreen extends JFrame implements ActionListener {
    private final JMenu commandMenu = new JMenu("Commands");
    private final JMenuItem add = new JMenuItem("Add");
    private final JMenuItem search = new JMenuItem("Search");
    private final JMenuItem quit = new JMenuItem("Quit");

    public LaunchScreen() {
        super();
        setSize(300, 300);
        setTitle("Library Search");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        commandMenu.add(add);
        commandMenu.add(search);
        commandMenu.add(quit);

    }

    @Override
    public void actionPerformed(ActionEvent e) {

    }
}

The three lines after super(); have a warning, Overridable method call in constructor. What does this mean? The other answers to this question don't really help me.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
MortalMan
  • 2,582
  • 3
  • 23
  • 47
  • 2
    By "other answers", did you mean [those one](http://stackoverflow.com/q/3404301/1743880)? If so, what don't you understand with them? – Tunaki Nov 29 '15 at 19:56
  • 1
    Why don't the other answers help you? They explain pretty well exactly what the problem is. – Hovercraft Full Of Eels Nov 29 '15 at 19:57
  • 1
    I don't understand what I am supposed to do. How am I supposed to set the size, title, etc of my window? – MortalMan Nov 29 '15 at 19:58
  • It's a warning, you can ignore the warning, or use the `@SupressWarnings(...)` annotation, or change a settings in your IDE to ignore this. But the explanation behind this warning is the same as all the other similar questions on this subject. – Hovercraft Full Of Eels Nov 29 '15 at 20:01
  • @HovercraftFullOfEels thanks. Is there anything I should change in my code or will it be fine the way it is? – MortalMan Nov 29 '15 at 20:02
  • You could also factor this into a private method and invoke that method from the constructor. – Tunaki Nov 29 '15 at 20:02
  • Personally I would avoid setting sizes as you're doing, I would avoid extending JFrame if I'm not altering its intrinsic behaviors. – Hovercraft Full Of Eels Nov 29 '15 at 20:02
  • @Tunaki: I don't think that that would reduce the risk of unknown side effects from potential future extensions of the methods. – Hovercraft Full Of Eels Nov 29 '15 at 20:03
  • See this question, this is exactly what you are looking for: https://stackoverflow.com/questions/36410183/calling-overridable-methods-like-swings-add-in-constructor – Hani Shams Feb 24 '21 at 07:28

0 Answers0