cómo separar el diseño de la forma lógica del programa en swing y swt

I have decent knowledge in J2EE frameworks such as Spring,Hibernate and got inspired by the separation of concerns . I am a starter in desktop development in java. Things are going somewhat fuzzy in swing. In VB, Form design will be kept as a separate one, and our form will only contains event listeners. But when I am started in swing many stuffs are loading in a single class making things difficult. I am unable to find where I am wrong. Similar thing continues when I started for alternatives such as SWT.

Sample SWT code I got from internet

import java.awt.Color;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import edu.umd.cs.piccolox.swt.PSWTCanvas;
import edu.umd.cs.piccolox.swt.PSWTPath;
import edu.umd.cs.piccolox.swt.PSWTText;
public final class SWTBasicExample {

/**
 * Create and open a new shell on the specified display.
 *
 * @param display display
 * @return a new shell on the specified display
 */
public static Shell open(final Display display) {
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    // create a new SWT canvas
    final PSWTCanvas canvas = new PSWTCanvas(shell, 0);

    // create some SWT nodes
    //    and add them as child nodes to the canvas' camera's first layer
    PSWTPath rect = PSWTPath.createRectangle(25, 25, 50, 50);
    rect.setPaint(Color.RED);
    canvas.getLayer().addChild(rect);

    rect = PSWTPath.createRectangle(300, 25, 100, 50);
    rect.setPaint(Color.BLUE);
    canvas.getLayer().addChild(rect);

    PSWTPath circle = PSWTPath.createEllipse(100, 200, 50, 50);
    circle.setPaint(Color.GREEN);
    canvas.getLayer().addChild(circle);

    circle = PSWTPath.createEllipse(400, 400, 75, 150);
    circle.setPaint(Color.YELLOW);
    canvas.getLayer().addChild(circle);

    PSWTText text = new PSWTText("Hello World\nMultiline");        
    text.translate(350, 150);
    text.setPenColor(Color.GRAY);
    text.setBackgroundColor(Color.BLACK);
    canvas.getLayer().addChild(text);

    text = new PSWTText("Goodbye World");
    text.translate(50, 400);
    text.setPenColor(Color.MAGENTA);
    canvas.getLayer().addChild(text);

    shell.open();
    return shell;
}

public static void main(final String[] args) {
    final Display display = new Display();
    final Shell shell = open(display);
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
}
}

Here everything is messing up so our business logic and the design are on single class. I studied that a method can have up to 10 lines to make it less complex and reusable. Is it applicable only to logic not desktop development.

Meanwhile, I heard Swing is an MVC framework. So, I want neat separation of logic, design and control. I want good engineering practices being followed in swing and swt to ease desktop development in java. Since I am using eclipse SWT will be preferable for me.

Gracias,

preguntado el 31 de enero de 12 a las 08:01

How long are you working with J2EE frameworks that you have decent knowledge? (no offence, just asking) -

3 Respuestas

Spring (and other mentioned technologies/frameworks) have absolutely another purpose than GUI frameworks. You may not require same thing from two so different technologies. Desktop is not web, there aren't same rules as we speak.

If you want use MVC like patterns with SWT (especially for data binding) learn JFace.

Respondido el 31 de enero de 12 a las 21:01

Yes, I will accept your point. One thing we can add Apache Foundation and springsource organization and Jboss community helped java developers by giving handful of framework for Web development. But, it is not the case in Desktop development in Java. But, I don't know the reason behind that. Thanks, for spending your valuable time :D - Hariharan

I meant that web is made on totally different basis than desktop. Client server pattern is not common in desktop development. That's why most frameworks don't use any similar approach. When you'll try to use something like that in GUI, you'll have to write huge amount of boilerplate code.. - Hechicero

As for Swing vs. SWT, I have used both (but mainly Swing). At least back when I was looking into SWT (~5 years ago) the documentation for SWT was very poor compared to Swing. Even though I liked many design aspects of SWT, and it is simpler than Swing, IMO, Swing is the one to learn because, IMO, it has far far better documentation, examples, etc. And it is used in many more places.

As for general design ideas, one technique, if your major JPanels (or whatever your Component is) are modal, is to have a Model / DataObject / BusinessObject associated with each JPanel. The JPanel will have methods named something like panelToModel() and modelToPanel(). The former creates (or updates) a model corresponding to what the user has changed, and the latter takes a model and updates the GUI to match. Now, personally, I hate modal anythings, but this is a fairly simple way to get started.

Respondido el 31 de enero de 12 a las 23:01

5 years are long way ;].. You're right that SWT documentation wasn't so good, but for now I cannot imagine to write code without something so great as Fragmentos de SWT definitely are. I miss this type of documentation for Swing so much! - Hechicero

That would have helped me a lot. Anybody got a time machine? :-) - user949300

In Swing the components itself follow the MVC pattern - most of the time. They are based on models, the visual representation can be replaced easily. For a larger scope you have to implement this yourself. Some approaches for separation of concerns exist, like the usage of Actions. But it is in no way a ready to use application platform, it is a GUI toolkit. There exist projects for application frameworks, like JSR 296 - "Swing Application Framework". But this one doesn't get finalized for years. Another notable platform based on Swing is the Plataforma Netbeans, but this is a level higher.

SWT components are not designed with MVC concepts. JFace brings this concept to SWT. Ontop of this you can use Eclipse RCP which is an application framework similar to the Netbeans platform in functionality.

So if you are programming "raw" Swing you get some support but still have to do a lot of work by yourself. SWT asks for more basic effort (and discipline) from your side. But for both exist platforms that implement the MVC ideas.

Respondido 01 Feb 12, 00:02

I am wondering, how software is built without separation of concerns. Can we maintain the software without proper separation. Eager to know what will be the industry standard:) - Hariharan

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.