Question

I have a noobie question about GUI designing in Java: should we use more methods or more objects? Take a very simple example: dialogs.

For a dialog, I can use a method or use an object:

Using Method:

public static void MakeDialog() {
    final JFrame frame = new JFrame();
    String help = "";//Reader.readhelp();

    JTextArea contentArea = new JTextArea(help, 60, 60);
    JScrollPane scroller = new JScrollPane(contentArea);
    scroller.setPreferredSize(new Dimension (650,650));

    JButton close = new JButton("Close");
    close.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent a) {
            frame.dispose();
        }
    });

    JPanel panel = new JPanel();
    panel.add(scroller);
    panel.add(close);

    frame.add(panel);
    frame.setTitle("Help");
    frame.setSize(700, 700);
    frame.setVisible(true);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}

Then I will just need to call this method need this dialog to show up.

However, I can have another approach: make a class of dialog:

public class MyDialog {

     public MyDialog() {
           final JFrame frame = new JFrame();
    String help = "";//Reader.readhelp();

    JTextArea contentArea = new JTextArea(help, 60, 60);
    JScrollPane scroller = new JScrollPane(contentArea);
    scroller.setPreferredSize(new Dimension (650,650));

    JButton close = new JButton("Close");
    close.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent a) {
            frame.dispose();
        }
    });

    JPanel panel = new JPanel();
    panel.add(scroller);
    panel.add(close);

    frame.add(panel);
    frame.setTitle("Help");
    frame.setSize(700, 700);
    frame.setVisible(true);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
     }
}

The problem that really bothers me is: which one uses memory more efficiently? For one or two dialogs in a software, this is probably not a problem at all. If I need to deals with hundreds of dialog, I need to think about the memory resources that I might consume. According what I have learned so far, calling that method is better than creating MyDialog object, since memory will be released when once the method is processed. However, the object MyObject will be remained in memory if I don't take any action on it. The problem that I am facing is: after a few hours of developing, I found out that I only have a few MB of memory left and I have to quit Eclipse and restart my computer to release the memory.

Therefore:

1) Which one is better?

2) If creating object is better, how can I remove that object from memory if I am not going to use it anymore?

Was it helpful?

Solution

Memory usage is irrelevant here. The difference will be around 20 bytes, that will be garbage collected.

What matters is correct design. An object's constructor is not meant to have side effects like showing a dialog box. The goal of a constructor is to create an object with a specific state, by initializing its fields, in order to use the object later.

Your example doesn't do anything like that. You would be creating a MyDialog instance, and do nothing with it. All the constructor would do is to have a huge side-effect: displaying a dialog box.

Another bad point in this design is that you're using an instance field to store a value that is in fact a constant, not tied to any specifu MyDialog instance, and only relevant to the constructor. There is no reason at all to have such a field.

And finally, your method doesn't respect the Java naming conventions: methods start with a lower-case letter. And it should be named showDialog() instead of makeDialog, since it doesn't create a dialog box, but shows one.

OTHER TIPS

I'm afraid this question is a bit too general to answer, since you're asking about any and all use cases of GUI design.

Let me instead refer to the specific scenario you mention, which is a dialog.

It might come as a surprise, what's considered as a Swing best practice when it comes to dialogs, is somewhere in between a method and a class.

When working with dialogs in Swing, on the one hand, it's considered a best practice to create a new class for the panel that contains all the GUI, and which will become the content pane of the dialog. On the other hand, you should create a method that will create (and possibly also display) the dialog. In that method, you'll do the following:

  • Create an instance of JDialog (usually, there's no need to subclass)
  • Create an instance of your custom panel class (e.g. MyDialogContentPaneClass)
  • Set the panel as the content pane of the dialog
  • Display and/or return the dialog
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top