Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

general question about Java Swing

Tags:

java

swing

I have made a Swing application which is rather simple in functionality. However the code which it consist of became rather large and very messy in my opinion. All the swing components and actions is in one file. So for instance if I was to make a even larger application with more functionality the code will be rather hard to go through.

So my question is how to make a good structure of the code. Or if there's a good webpage I can read about it, and if possible some code examples. I have checked Sun's tutorial about Swing, but those a rather simplistic examples they've shown.

UPDATE: I have pondering a while and check for some examples. I don't know if I got the MVC pattern correct. Anyway my idea is to seperate each JFrame to their own class file. Afterward I have one MainFrame which is the main window for the application. From that JFrame I create one instance of each JFrame I have. And call those frame from the MainFrame with Actions. I don't know if this is a good idea. However it makes the code significant easier to read anyway.

Here's an example of how I meant

class Main implements ActionListener {

    private JFrame frame = new JFrame();
    private JButton button1 = new JButton();
    private JPanel panel = new JPanel();

    private FirstFrame frame1 = new FirstFrame();
    private SecondFrame frame2 = new SecondFrame();
    private ThirdFrame frame3 = new ThirdFrame();

    public Main() {
        button1.addActionListener(this);
    }

    public createGUI() {
        frame.setTitle("Main");
        frame.setSize(400,300);
        panel.add(button);

        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
    }

    public static void main(String args[]) {
        new Main().createGUI();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == button1)
        {
            frame1.enable();
        }
    }
}
like image 376
starcorn Avatar asked Dec 26 '09 10:12

starcorn


People also ask

What is the purpose of Java Swing?

August 3, 2021. Java Swing is part of Java Foundation Classes. It is used to create window-based applications which makes it suitable for developing lightweight desktop applications. Java Swing is built on top of an abstract windowing toolkit API purely written in Java programming language.

What are the 3 types of Java Swing containers?

As we mentioned before, Swing provides three generally useful top-level container classes: JFrame , JDialog , and JApplet .

What are the importance of using Java Swing components?

Advantages of Swing Swing components are platform-independent. Swing components can use a different look and feel. Swing components use the Model-View-Controller paradigm (MVC) and thus can provide a much more flexible UI. Swing components are lightweight (are less resource-intensive than AWT).


2 Answers

Use Model-View-Controller design pattern. It deals with separating user interface code from Business logic.

EDIT :

As OP is looking for organized code rather than flexible design, I have removed the NetBeans UI Designer part from my answer.

like image 143
missingfaktor Avatar answered Sep 28 '22 18:09

missingfaktor


Organizing user interface code of any significant size is a problem with surprisingly little written about it, considering that every application has the same problem. There are some techniques that can help:

  1. Refactoring - to clean up after the code gets untidy.
  2. Model View Controller pattern - to separate concerns.
  3. OO Design - use many small, cooperating classes instead of big chunks of code.
  4. "On event Do action" - idiom for separating the actions from the views.
  5. UML Component models - visualize design concepts above the class level.
  6. Dependency Inversion Principle - organize code dependencies.

The basic design techniques have been around for quite some time, and are well understood, but applying the techniques on a larger scale is something that seems to be designed individually for each application.

One approach that I have seen applied effectively is to separate out the Events, Listeners, Actions, etc into their own classes and either organize them into packages by type, or use a consistent naming convention across packages so that a component and its associated classes are easily identifiable (XDialog, XDialogMouseListener, XDialogCancelAction, etc).

Another approach would be to look at some large open source applications (Eclipse, Firefox, ...), and see how they organize their GUI code.

like image 33
richj Avatar answered Sep 28 '22 19:09

richj