I'm a novice at Java and just beginning with Swing. I’m trying to figure out how to separate event logic from the presentation logic. Look at the following classes:
GUITest.java
package 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 constructor
public 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 class
This 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. I’d 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? I’d 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.java
package 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 constructor
public void actionPerformed(ActionEvent event){
i++;
ta.setText(ot.lyrics(i));
if(i == 4){i = 0;}
}
} //end class
This meets my needs: The code for all those complex methods is
- separated from the presentation class
- accessible by the presentation’s event handlers
But 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?