0

I am confused about how GUI's should be implemented in Java because there are many different ways. There is this thread that already describes these proper habits: Java/Swing GUI best practices (from a code standpoint) But I am confused because when using this method Netbeans says "Overridable method call in constructor" next to functions that were called from the superclass.

I do not ask how to use GUI's or how to implement them, but I ask for the proper way of implementing them. I'm a little skeptical about this format because of that error that comes up. I have no problem running programs with this format though. At least while the programs are quite small and simple.

I get this sample code from the thread mentioned above:

public class Main {
    public static void main(String[] args) {
        final String text = args[0];
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                final MyWindow wnd = new MyWindow(text);
                wnd.setVisible(true);
            }
        });
    }
}

public class MyWindow extends JFrame {
    public MyWindow(String text) {
        super("My Window");

        setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); //Overridable method call in constructor
        addWindowListener(new WindowAdapter() {   //Overridable method call in constructor
            @Override
            public void windowClosing(WindowEvent e) {
                MyWindow.this.setVisible(false);
                MyWindow.this.dispose();
            }
        });

        final JButton btn = new JButton(text);
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(MyWindow.this, "Button Pressed", "Hey", JOptionPane.INFORMATION_MESSAGE);
            }
        });

        setLayout(new FlowLayout());
        add(btn);
        pack();
    }
}

And this error occurs with any other method that is inherited and used in the constructor. I know this thread explains why not to do this: What's wrong with overridable method calls in constructors? So now I ask, is this still the proper style? Is this appropriate to do when implementing GUIs?

Note: Not a very experienced programmer so I hope I phrased everything correctly. I would like to learn the proper style to this all from the start.

Community
  • 1
  • 1
Patrick18200
  • 41
  • 1
  • 3
  • *""Overridable method call in constructor""* this is a newish error message (but an oldish concept) introduced into Netbeans over the last few years, so it might post-date the tutorial you're read. *"So now I ask, is this still the proper style? Is this appropriate to do when implementing GUIs?"* - Generally speaking, you shouldn't be calling overridable methods in the constructor as a general rule. Sometimes, it's not possible, but you should be trying to construct your classes in such away that it limits the need to when you need to extend the class – MadProgrammer Feb 13 '16 at 00:39
  • One of the things you need to is justify the risk, for instance, in your example, you're using `addWindowListener`, which `public` by design. You could argue that this is an acceptable risk, as you're adding a local listener, which couldn't easily be established by making the caller do it (don't get me started on `init` methods), also, because you're extending from `JFrame` (and haven't overloaded the method yourself), you can better guarantee the expected behaviour. – MadProgrammer Feb 13 '16 at 00:44
  • So because I don't change what the method does, it's considered appropriate or acceptable under these circumstances? Because the way I understand it is that the super class is initialized before the sub class and therefore the methods should be perfectly fine to use and access because I am not changing them. – Patrick18200 Feb 13 '16 at 00:59
  • It's more that if you create a subclass of your frame, that class might override a method so that it does something entirely different, and therefore the superclass constructor would not work as expected. – Bagelman Feb 13 '16 at 01:02
  • Part of the problem is, if a user later comes along and extends your class and changes the functionality of the `addWindowListener` method, you could get yourself into problems, but frankly, if the user is screwing with that particular method, then they probably deserve to get into trouble – MadProgrammer Feb 13 '16 at 01:02

1 Answers1

0

To avoid putting an overridable method in a JFrame constructor you'll have to do all the JFrame initializing outside of the constructor, i.e: in your main function. This is generally bad practice as it can result in discrepancies between different instances of your frame. So yes, you're doing everything right. Netbeans displays some dumb warnings sometimes.

Bagelman
  • 766
  • 8
  • 14