3
\$\begingroup\$

I have a Swing application with no real design pattern. I want to start learning to design Swing or any types of application properly. Here is the main JFrame class.

    import java.awt.Component;
    import java.awt.Dimension;    
    import javax.swing.JFrame;
    import javax.swing.JTabbedPane;
    import org.text.gui.components.tabs.*;

    public class TextFrame extends JFrame {

        private static final long serialVersionUID = 1289484372051624149L;
        public static final String INBOX = "Inbox";
        public static final String CONTACT = "Contact";
        public static final String SEND = "Send";
        private JTabbedPane pane;

        public TextFrame() {
            initComponents();
            layoutComponents();
            pack();
            setLocationByPlatform(true);
            setVisible(true);
        }

        public Component getTab(String title) {
        return pane.getComponentAt(pane.indexOfTab(title));
    }

    public void setSelectedTab(String title) {
        pane.setSelectedComponent(getTab(title));
    }

    private void initComponents() {
        setTitle("Text");
        setDefaultCloseOperation(EXIT_ON_CLOSE);            
        pane = new JTabbedPane();
        pane.setPreferredSize(new Dimension(225, 300));
        pane.addTab(INBOX, new InboxTab(this));
        pane.addTab(CONTACT, new ContactTab(this));
        pane.addTab(SEND, new SendTab());
    }

    private void layoutComponents() {
        add(pane);
    }       
}

The tabs are JPanels with some logic in them. Any advice on a design pattern to implement or examples or anything would be greatly appreciated. I hope I included enough information.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ This appears to be a pretty clean implementation of the top frame. Seeing the code for the tabs (Inbox/Contact/etc) would shed more light on what's going on here and maybe more opportunity for review. \$\endgroup\$
    – Eric P.
    Commented Feb 25, 2013 at 3:03
  • 1
    \$\begingroup\$ One thing which could be discussed, if it is a better way to extend JFrame or to have a private member of type JFrame. I prefer the second. Other than that I would agree to @E-Man \$\endgroup\$
    – tb-
    Commented Feb 27, 2013 at 17:32

1 Answer 1

2
\$\begingroup\$

One thing would be to separate view from functionality (you may even make the business logic invocation asynchronous). You can also make your code cleaner and smaller by creating widgets that support templating and using them.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.