import java.io.*; import java.awt.*; import java.awt.event.*; import java.awt.TextArea.*; import javax.swing.*; import javax.swing.text.*; /** * RunCmdsGUI is a Java application that takes any number of commands * defined in the String[] array cmds. * Please note I had to use the java.awt TextArea instead of the * Swing JTextArea, because the JTextArea had problems updating * the textarea real-time. It is pretty cool, because it displays * both the stdout and stderr where they would be on the command-line. * * Run1 button executes commands specified by the commands in cmds * under the Run1 section of actionPerformed * Run2 button executes commands specified by the commands in cmds * under the Run2 section of actionPerformed * * Also please note, this code is probably not very 'correct'. * but it does work. * * @author Nick * (see http://illx.org for more details) */ public class RunCmdsGUI extends JFrame implements ActionListener { //compiled output TextArea co; PrintStream ps; protected boolean done = false; public RunCmdsGUI() throws IOException { //new jpanel JPanel p; //new container cp Container cp = getContentPane(); cp.add(BorderLayout.NORTH, p = new JPanel()); JButton b; p.add(b = new JButton("Run1")); b.addActionListener(this); JButton de; p.add(de = new JButton("Run2")); de.addActionListener(this); JButton close; p.add(close = new JButton("Close")); close.addActionListener(this); co = new TextArea(30, 60); co.setEditable(false); JScrollPane jsp = new JScrollPane(co); cp.add(BorderLayout.CENTER, new JScrollPane(co)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); // end of GUI portion } public void actionPerformed(ActionEvent ev) { String b = ev.getActionCommand(); co.setText(""); //flush the jtextarea String[] cmds = new String[3]; if(b.equals("Run1")) { cmds[0] = "date"; cmds[1] = "sleep 3"; cmds[2] = "date"; } else if(b.endsWith("Run2")) { cmds[0] = "date +%r"; cmds[1] = "w"; } else if(b.equals("Close")) { System.exit(0); } else { co.append("this should not happen\n"); } String cmdo = null; for(int i=0;i