Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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.

share|improve this question
1  
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. –  E-Man Feb 25 '13 at 3:03
1  
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 –  tb- Feb 27 '13 at 17:32
add comment

1 Answer

up vote 2 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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