HiveBrain v1.2.0
Get Started
← Back to all entries
principlejavaMinor

Design Pattern for Swing application

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
applicationdesignswingforpattern

Problem

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.

Solution

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.

Context

StackExchange Code Review Q#23042, answer score: 2

Revisions (0)

No revisions yet.