I'm a novice at Java and just beginning with Swing. Im trying to figure out how to separate event logic from the presentation logic. Look at the following classes:GUITest.javapackage guitest;import java.util.ArrayList;public class GuiTest {         public static void main(String args[]) {      new MainFrame2();}}MainFrame2.java    package guitest;    import javax.swing.*;    import java.awt.event.*;public class MainFrame2 extends JFrame implements ActionListener{    JPanel pane = new JPanel();    JButton butt = new JButton(Push Me);     JTextArea ta = new JTextArea(Push Me);    int i = 0;    MainFrame2(){        super(My GUITest);        setVisible(true);        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);             setBounds(1000,100,500,500); //From upper left corner (right, down, width, height)        this.add(pane);         pane.setLayout(null);        butt.setSize(300,20);        butt.setLocation(10,10);         butt.addActionListener(this);        pane.add(butt);           ta.setBounds(0,50,300,30);        pane.add(ta);    } //end constructorpublic void actionPerformed(ActionEvent event){        i++;        switch(i){            case 1: ta.setText(And Then Just Touch Me);                       break;            case 2: ta.setText(Till I Can Get My);                break;            case 3: ta.setText(Satisfaction);                break;            case 4: ta.setText(Push Me);                break;            }    }   } //end classThis simple example works. When the button is pushed, the text area changes. But what if the text is produced by 100 lines of code? Say, if the results of a complex SQL query were pasted there? I wouldn't want to put that 100 lines of code in the MainFrame2 class. Id want to put it into another class, so I've separated my presentation code from my data manipulation code. Going further, what if I had several methods that weren't tied to a particular object? Say, if clicking a button was supposed to write to a log file or send an e-mail? Id see a need to put that code somewhere outside of the MainFrame2 class, but I wouldn't see a need to make a LogFile class or an Email class. I just need a group of methods that I can call without having to make a huge number of new classes.I found a workaround. I made a class called Misc, for all the miscellaneous methods I need to call. Then I instantiate a new Misc object in the MainFrame2 class. Then I call the miscellaneous methods from that object. Here are the Misc class and the adjusted MainFrame2 class.Misc.javapackage guitest;public class Misc {        Misc(){} public static String lyrics(int i){            if(i == 1){return(And Then Just Touch Me);}                                    if(i == 2){return(Till I Can Get My);}            if(i == 3){return(Satisfaction);}            if(i == 4){return(Push Me);}            return(Something's wrong. I should always be 1-4.);    }public static void logging(String s){    //Write s to some file            }   public static void notification(String sender, String receipient, String subject, String body){    //Send an e-mail to someone.    }}MainFrame2.java    package guitest;    import javax.swing.*;    import java.awt.event.*;public class MainFrame2 extends JFrame implements ActionListener{    JPanel pane = new JPanel();    JButton butt = new JButton(Push Me);     JTextArea ta = new JTextArea(Push Me);    int i = 0;    MainFrame2(){        super(My GUITest);        setVisible(true);        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);             setBounds(1000,100,500,500); //From upper left corner (right, down, width, height)        this.add(pane);         pane.setLayout(null);        butt.setSize(300,20);        butt.setLocation(10,10);         butt.addActionListener(this);        pane.add(butt);           ta.setBounds(0,50,300,30);        pane.add(ta);    } //end constructorpublic void actionPerformed(ActionEvent event){        i++;        ta.setText(ot.lyrics(i));        if(i == 4){i = 0;}    }   } //end classThis meets my needs: The code for all those complex methods isseparated from the presentation classaccessible by the presentations event handlersBut it seems horribly inelegant. It tosses all the random, one-off methods into a class of leftovers. Is there a better way to do this?
Separating presentation from the GUI
java;beginner;swing
You're on the right way. In principle this is the way used in Java. There are only some differences in real code.Your class Misc is normally named a Service. A Service class does the business logic of your application. Also normally you don't use static methods but instance methods to do you business logic.The Service class is normally created outside the GUI code (e.g. in your main() method). You then set the Service class into your GUI class either via the constructor or via a setter.In more advanced setups you use frameworks like spring to wire up your application.Also you would have three different services in your case, one for the lyrics (with several methods e.g. to get a single line or the complete lyrics at once), one for the notification and a third for logging (if you don't use a framework for this).Also the business logic is often parted into two layers, one for the pure business logic and another for the data access (e.g. database access).Also some other notes:It is usual to create the GUI first and call setVisible() in the last.The GUI initialization code is normally in its own private method that is called from the constructor.