Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7075351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:10:01+00:00 2026-05-28T06:10:01+00:00

So i got the grip on the basic MVC patterns in java using Observer

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:10:02+00:00Added an answer on May 28, 2026 at 6:10 am

    I take this opportunity to present my ideal MVC ideas.

    The wiring between the components can benefit from the brevity of the class EventHandler, http://docs.oracle.com/javase/6/docs/api/java/beans/EventHandler.html.

    Let’s constrain the GUI to java swing.

    • GUI Builders, especially if they generate form files, preferably in XML or Java FX, might be feasible, as they provide a pure hierarchical organisation. A canonical ordering.
    • One may create components (JPanel, MyJTextPane), to compose views and predefine styling properties.

    To integrate MVC one has a model, possibly composed of submodels. And one could have an abstract view class, possibly composed of subviews. This abstract view class is the factory of all swing components / subviews, with the injection of the model and the binding, by the controller.

    In the GUI builder one can add a JTextField a with as custom creation code view.a.create(). After initComponents one can call view.checkRequiredCreated() which might throw an informative exception on missing creations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a grip of PDFs that I need to combine into one using
Got a bluescreen in windows while cloning a mercurial repository. After reboot, I now
Got a list saved by using .data(). How is it possible to find exact
I've been coding php for a while now and have a pretty firm grip
Got this error when parsing my html page using XPATH.. i am also using
I've been at this for about an hour now, and cant seem to grip
I just got the grip on GWTP and the MVP, GIN and Dispatch. With
Got a werid problem with a view, if i define it with Inherits=System.Web.Mvc.ViewPage<List<Tuple<string, DateTime?,
got a new blog at wordpress few days ago ( http://ghads.wordpress.com ) and I
Got a class that serializes into xml with XMLEncoder nicely with all the variables

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.