23

As a contrast to this wiki, I am looking for the proper way to implement Swing GUI controls from a coding standpoint.

I have been on a quest to learn Java and its GUI tools but I find internet tutorial after internet tutorial that throws everything in main and I know this isn't right.

I've also tried RAD systems like Netbeans and other "visual" editors but by the time I get to coding I've got a heap of code that I don't know half of what it does, so I'm intent on learning to hand code swing, and I know the basic controls and layout, but want to do it the right way.

Is there a model or standard I'm missing?

example questions...

do I extend JFrame and create my own frame object? (I would assume yes)

do I encapsulate the main menu inside that frame object? or do I create its own? etc...

How to I separate "View" logic from "Application" logic?

Basically, I'm looking for what the industry standard is, on how to organize GUI code.

Community
  • 1
  • 1
jondavidjohn
  • 61,812
  • 21
  • 118
  • 158
  • 3
    This belongs on Programmers SE... where I [asked it already](http://programmers.stackexchange.com/questions/39677/what-are-the-best-practices-of-java-swing-development). – Pops Mar 29 '11 at 14:08
  • 2
    I have to say I disagree, as I am asking for coding specific examples. With the opportunity for quantitative non-subjective answers. Programmers is for more subjective, profession related questions. However I do appreciate your information and will look over the answers to your question. – jondavidjohn Mar 29 '11 at 14:10
  • 1
    Well, you should probably say that explicitly, then... do you mean that you want answers for the the three things you listed under "example questions"? – Pops Mar 29 '11 at 14:13
  • sure, as a start, I'll edit to try to make myself more clear. – jondavidjohn Mar 29 '11 at 14:15
  • You're asking for subjective help. There is no "industry standard on organizing code" because that's like asking for "the one true derivation of an integral" ... given "the integral x^2" I want you to derive it and I want you to _know_ that my constant is [omitted] but I won't tell you what my constant is. ~ (my point) programming is like that. There's lots of ways to get to the same end result. So what's "best practices" for my shop may not be "best practices" for your shop. And as for "internet tutorials stick everything in `main`" it's because they're going for quickest entry to the code. – jcolebrand Mar 29 '11 at 14:33
  • BTW, I doubt there is an "industry standard" for organizing GUI code. I've worked at a lot of different places and ad hoc seems to be the most standard way of handling GUI code. Which means, that normally not much thought is given at all. – hooknc Mar 29 '11 at 16:01

2 Answers2

39

Since there seems to be some argument about what constitutes "best practices", I'll give you what I have found works best for me, and my reasoning:

1. Each window should extend either JFrame or JDialog (depending on the type of window). This makes it easy to control the properties of the window without specifying a specific object every time. This is more of the general case, though, as I have been known to do it both ways.

2. The main() method should be in a separate class. This increases the likelihood of being able to use your window classes elsewhere, as they are not tied to specific implementations. Technically it doesn't make a difference, but application startup code just doesn't belong in a window.

3. Listeners should be in anonymous inner classes. Your top-level class should not implement any listeners. This prevents hacks like calling the listener methods from anywhere except the object to which they are attached.

Here is a simple application with a single frame to demonstrate these practices:

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);
        addWindowListener(new WindowAdapter() {
            @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();
    }
}
Jonathan
  • 13,354
  • 4
  • 36
  • 32
15

I agree with all of Jonathan's points.

  1. Each window should extend either JFrame or JDialog...

  2. The main() method should be in a separate class...

  3. Listeners should be in anonymous inner classes...

I would also like to add the following:

  1. Use GridBagLayout (GBL) judiciously. GBL is a powerful Layout Manager, difficult to learn, but quite powerful.

  2. Consider hand coding all your UI. I'm personally not a fan of the code that is produced by visual editors. But, with that said I have not used a visual editor in several years (2000ish). They might be better at this point.

  3. Use JPanels judiciously. Look at your ui and determine which components should behave the same as the screen size changes and then group those components together on a JPanel. Consider using JPanels inside of JPanels to get your correct resizing behavior.

I normally take a slightly different approach on having my components handle events then Jonathan does, but I believe his approach is a bit cleaner then mine.

Also, really study the use of MVC and Layered Architecture. It is truly best not to be mixing UI and Business Logic together.

hooknc
  • 4,854
  • 5
  • 31
  • 60
  • "5.) Consider hand coding all your UI. I'm personally not a fan of the code that is produced by visual editors. But, with that said I have not used a visual editor in several years. They might be better at this point." Give yourself a break! Take a look at Netbeans 6.9.1 and Matisse. You finish in no time and the code you get is very good. – Costis Aivalis Mar 29 '11 at 17:43