Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to separate GUI from logic in practice

807599Mar 25 2007 — edited Mar 27 2007
Hello

I have programmed in other languages in the past, now I need to make a serious application in Java, and feel the need to make it easy to maintain in the future.

So I must separate the GUI from the logic, so if I change the GUI, my program continues working. And too to see better the way it works, so it doesn't become too complex to maintain.

How do I do this in Netbeans?

For example: I did a frame with a button. Then made a class called CloseButton with a public static method that exits. From the Frame class I import the package that contains CloseButton class.
In the code of the button I place a call to the method that exits.

Is this well done? How do you do to separate gui and logic? Can this be done better?

I suppose the starting point to an application with GUI is the class that makes the GUI. Isn't it? So the class constructs the GUI, and then I place other classes to be called from there and make it function.
In this example I isolated the logic of a button.

Please tell me how is this done in Java, if I am doing well or not, before I continue coding.
I am not sure at all, as I am not used to this.

Thanks.

Jordi

PD: Here is an example code of what I mean
/*
 * HiperBrowserFrame.java
 *
 * Created on 25 de marzo de 2007, 18:54
 */

package frame3dbrowser;
import logic.*;
/**
 *
 * @author  Administrador
 */
public class HiperBrowserFrame extends javax.swing.JFrame {
    
    /** Creates new form HiperBrowserFrame */
    public HiperBrowserFrame() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">                          
    private void initComponents() {
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(68, 68, 68)
                .add(jButton1)
                .addContainerGap(587, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(45, 45, 45)
                .add(jButton1)
                .addContainerGap(309, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
 CloseButton.closeThis();
    }                                        
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new HiperBrowserFrame().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    // End of variables declaration                   
    
}
/*
 * CloseButton.java
 *
 * Created on 25 de marzo de 2007, 19:08
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package logic;

public class CloseButton {
    public static void closeThis() {
        System.exit(0);
    }
    
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 24 2007
Added on Mar 25 2007
10 comments
3,062 views