2
\$\begingroup\$

So i got the grip on the basic MVC patterns in java using Observer / Observable method. Now in interest of keeping it clean and readable i would like some pointers before i move on regarding how to well organize my View as this is where my Classes get most filled up. We been told in school to keep file size per class bellow 20kb to keep it readable and later easier maintainable.

Here is my view:

package view;

import model.*;
import helper.*;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import java.util.Observable;
import java.util.Observer;
import net.miginfocom.swing.MigLayout;


public class View extends JFrame implements Observer
{
    private Model model;

    private JPanel left = new JPanel(new MigLayout());
    private JPanel center = new JPanel(new MigLayout());
    private JPanel right = new JPanel(new MigLayout());

    private void setConstraints()
    {
        this.left.setMinimumSize(new Dimension(252, 540));
        this.left.setMaximumSize(new Dimension(252, 37500));

        this.center.setMinimumSize(new Dimension(298, 540));

        this.right.setMinimumSize(new Dimension(250, 540));
        this.right.setMaximumSize(new Dimension(250, 37500));
    }

    //Left panel contents
    private Towers box = new Towers();
    private Modules tree = new Modules();

    private JPanel setupLeft()
    {
        this.left.add(this.box, "growx, pushx, wrap");
        this.left.add(new JScrollPane(this.tree), "grow, push");
        return this.left;
    }

    //Center panel contents
    private Browser browser = new Browser();

    private JPanel setupCenter()
    {
        this.center.add(new JScrollPane(this.browser), "grow, push");
        return this.center;
    }

    //Right panel contents
    private JLabel tower = new JLabel("No tower selected.");
    private JLabel cap   = new JLabel("Capacitor");
    private JLabel cpu   = new JLabel("CPU");
    private JLabel shield = new JLabel("0");
    private JLabel armor  = new JLabel("0");
    private JLabel em     = new JLabel("0.0");
    private JLabel th     = new JLabel("0.0");
    private JLabel kn     = new JLabel("0.0");
    private JLabel ex     = new JLabel("0.0");

    private JPanel setupRight()
    {
        this.right.add(this.tower, "span, wrap");
        this.right.add(this.cap, "span, wrap");
        this.right.add(this.cpu, "span, wrap");
        this.right.add(this.shield, "span, wrap");
        this.right.add(this.armor, "span, wrap");
        this.right.add(this.em, "span, wrap");
        this.right.add(this.th, "span, wrap");
        this.right.add(this.kn, "span, wrap");
        this.right.add(this.ex, "span, wrap");
        return this.right;
    }

    public View(Model ui_model)
    {
        model = ui_model;

        this.setTitle("MVC Experiment 6");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        this.setMinimumSize(new Dimension(800, 600));
        this.setLayout(new MigLayout());

        this.setConstraints();

        this.add(this.setupLeft(), "dock west");
        this.add(this.setupCenter(), "dock center");
        this.add(this.setupRight(), "dock east");
    }

//Left panel contents - Listeners and methods for addressing JComponents
    public void xTowersBrowser(ActionListener event)
    {
        this.box.addActionListener(event);
    }

    public void xModulesBrowser(MouseListener event)
    {
        this.tree.addMouseListener(event);
    }

    public Towers getTowersBrowser()
    {
        return this.box;
    }

    public Modules getModulesBrowser()
    {
        return this.tree;
    }
    //Left panel - END

    //Center panel - components :: listeners and methods
    public void xBrowser(MouseListener event)
    {
        this.browser.addMouseListener(event);
    }

    public Browser getBrowser()
    {
        return this.browser;
    }
    //Center panel - END

    public void update(Observable o, Object arg)
    {

    }
}

Any suggestions on what to separate in new classes or how to minimize the code are helpful. This is just a cut out of my main View class there are still a lot of JComponents missing so it will get more messy.

\$\endgroup\$
1
\$\begingroup\$

Just a quick note: according to the Code Conventions for the Java Programming Language, File Organization instance variables should be placed before constructors and methods.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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