Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Doing large scale GUI projects

To get right directly to my question.

How do you do large scale GUI projects. I have not done any larger GUI projects in java so far but what i am working on now grew pretty fast and pretty big and now i am stuck whit a huge pile of code that is really annoying and messy.

Since i come from field of web development i am used to MVC frameworks so i have 3 packages in my projects Model where i keep classes that interact whit files or db, Views where i keep my classes for Forms or GUI and Controller package where i keep the majority of my logic.

I have been told to separate my logic as well keep actions in one class and listeners in another class but i have no idea how to link all that up.

So far i only have 1 Controller class where i execute all the methods regarding whats happening on the GUI once its invoked.

package pft.controller;


import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JTree;
import java.awt.event.*;
import javax.swing.JProgressBar;
import pft.view.Invoke_GUI;
import pft.model.Events;
import pft.model.Parse;

public class Tower_Controller {

    public Tower_Controller() {
    }
    //Global variables
    String isSelected = null;
    int hasModules = 0;
    int cap = 0;
    int cpu = 0;
    int shield = 0;
    int armor = 0;

    public void setName(String name){
        this.isSelected = name;
    }

    public String getName(){
        return this.isSelected;
    }

    public void setCap(int cap){
        this.cap = cap;
    }

    public int getCap(){
        return this.cap;
    }

    public void setCpu(int cpu){
        this.cpu = cpu;
    }

    public int getCpu(){
        return this.cpu;
    }

    public void setShield(int shield){
        this.shield = shield;
    }

    public int getShield(){
        return this.shield;
    }

    public void setArmor(int armor){
        this.armor = armor;
    }

    public int getArmor(){
        return this.armor;
    }


    public void invoke() throws IOException {
        Invoke_GUI runnable = new Invoke_GUI();
        final JLabel tower_name = runnable.tower_name;
        final JComboBox tower_select = runnable.tower_select;
        final JTree module_browser = runnable.module_browser;
        final JTree selected_modules = runnable.selected_modules;

        final JProgressBar cap_bar = runnable.cap_bar;
        final JProgressBar cpu_bar = runnable.cpu_bar;

        final JLabel em_res = runnable.em;
        final JLabel th_res = runnable.thermic;
        final JLabel ki_res = runnable.kinetic;
        final JLabel ex_res = runnable.explosive;

        setTowerName(tower_name, tower_select);
        removeTower(tower_name);
        runnable.setVisible(true);       

    }

    public void removeTower(final JLabel tower_name) {
        tower_name.addMouseListener(new MouseListener() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if (hasModules == 1 & isSelected != null) {
                    Events evt = new Events();
                    evt.towerHasModules();
                } else if (isSelected == null) {
                } else {
                    tower_name.setText("No Control Tower selected");
                    isSelected = null;
                }
            }

            @Override
            public void mousePressed(MouseEvent e) {
            }

            @Override
            public void mouseReleased(MouseEvent e) {
            }

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }
        });
    }

    public void updateVariables(String name) throws IOException{
        Parse tower = new Parse();
        String data[] = tower.towerData(name);
        Integer x = Integer.valueOf(data[1]).intValue();
        setCap(x);
    }

    public void setTowerName(final JLabel tower_name, final JComboBox tower_select) {
        tower_select.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (isSelected != null) {
                    Events evt = new Events();
                    evt.towerSelected(isSelected);
                } else {
                    tower_name.setText(tower_select.getSelectedItem().toString());
                    setName(tower_name.toString());
                    try {
                        updateVariables(tower_name.toString());
                    } catch (IOException ex) {
                        Logger.getLogger(Tower_Controller.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        });
    }


}

There are a lot of tutorials and examples how to do small usually single class Java GUI but no tutorials or examples on how to do projects that are bit larger than a single class.

Thanks in advance for all the help and advice.

like image 994
Sterling Duchess Avatar asked Dec 22 '11 03:12

Sterling Duchess


People also ask

Is Java GUI outdated?

Majority of existing GUI java codebases are Swing and likely will stay that way until the codebase rots and nobody maintains it anymore. Majority of new GUI java codebases are using JavaFX , which is the Swing replacement in Java8 and is part of the standard java library now.

Is swing good for GUI?

Swing provides a rich set of widgets and packages to make sophisticated GUI components for Java applications. Swing is a part of Java Foundation Classes(JFC), which is an API for Java GUI programing that provide GUI.

Is Java used for GUI?

GUI stands for Graphical User Interface, a term used not only in Java but in all programming languages that support the development of GUIs. A program's graphical user interface presents an easy-to-use visual display to the user.


1 Answers

Here is my advice for Swing development in general. It does discuss the importance of using Controllers to bridge the needs of the view and the interace of the model.

GUI guidelines for swing

Last Swing project I did I designed a MVC framework that used Spring for defining the model of the program and Controllers, then used annotations in the Controller to wire up events dispatched by the view onto methods in the controller. The view had access to the event dispatcher which was an event bus, and events sent over the bus called methods on the Controller through the annotations. This allowed any Controller to respond to events from the View. So as a Controller got too large it was super simple to refactor each set of methods into another Controller, and the view or model didn't have to change.

The beauty of the event bus was it could be shared with the model as well so the model could dispatch asynchronous events the Controller could register for as well. It looked something like:

public class SomeController {

   private AuthenticationModel authenticationModel;

   private LoginService loginService;

   private MyApp view;

   @Listener( event = "login" )
   public void login( LoginEvent event ) {
       view.showWaitDialog();
       loginService.login( event.getUserName(), event.getPassword() )
       .onResult( new Callback<User>() {
           public void onResult( User user ) {
               authenticationModel.setUser( user );
               view.hideWaitDialog();
               view.showStartScreen(user);
           }
       });
   }

}

Once this framework was in place it was amazing how fast we could get things done. And it held up pretty well as we added features. I've done my fair share of large Swing projects (3 to date), and this architecture made a huge difference.

like image 187
chubbsondubs Avatar answered Sep 24 '22 04:09

chubbsondubs