View Single Post
  #17 (permalink)  
Old 09-04-2008, 02:30 AM
Norm's Avatar
Norm Norm is offline
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Norm is on a distinguished road
Your design seems backwards. Normally the GUI class is instantied first and starts the worker class (to do any init it needs) and then exits to let the GUI engine wait for user input. When the user does something, a listener is called which then calls(via a new Thread) the worker class to do whatever needs to be done.
Here's a new version of your program configured to work that way:
Code:
// willemjav looping problem - Add GUI to call mainSoundblock at button press import javax.swing.*; import java.awt.event.*; import java.awt.*; public class WaitLoops3 extends JFrame implements ActionListener { JButton doItBtn = new JButton("Do it"); Cont cont; int cnt; //---------------------------------------------------- // Dummy class for testing class Cont { boolean esc; int tn = 1; int gettrackNumb() {return tn;} void settrackNumb(int trck) {tn = trck;} boolean getescFlag() {return esc;} void setescFlag(boolean f) {esc = f;} } // end class Cont public void actionPerformed(ActionEvent ae) { Object obj = ae.getSource(); if(obj == doItBtn) { // Start task on own thread Thread t = new Thread(new Runnable() { public void run() { mainSoundblock(cont.gettrackNumb()); } }); t.start(); }else { System.out.println("unknown ae" + ae); } } // end actionPerformed //------------------------------------------------------ private void mainSoundblock(int trck) { // cont.ed.resetFbo(); cont.setescFlag(false); // trig=false; // exit=false; if (trck==1 && !cont.getescFlag()) { Soundblock1(); cont.settrackNumb(trck); trck=2; } if (trck==2 && !cont.getescFlag()) { Soundblock2(); cont.settrackNumb(trck); trck=3; } if (trck==3 && !cont.getescFlag()) { Soundblock3(); cont.settrackNumb(trck); trck=4; } if (trck==4 && !cont.getescFlag()) { Soundblock4(); cont.settrackNumb(trck); trck=5; } // ....... etc until 12 System.out.println(" end of sound block @ " + System.currentTimeMillis() ); cont.setescFlag(false); // cont.ed.closeFbo1(); // cont.stopclk(); // cont.ed.resetFbo(); } // end mainSoundBlock // Dummy methods void Soundblock1(){System.out.println("Soundblock1");} void Soundblock2(){System.out.println("Soundblock2");} void Soundblock3(){System.out.println("Soundblock3");} void Soundblock4(){System.out.println("Soundblock4");} // ... // Constructor -------------------------------- // Build the GUI and wait for user's input public WaitLoops3() { super("Do it when button pressed"); cont = new Cont(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container pane = getContentPane(); JPanel panel = new JPanel(); doItBtn.addActionListener(this); panel.add(doItBtn); pane.add(panel); pack(); setLocation(300, 200); // move out of corner setVisible(true); } // end Constructor //-------------------------------------------------------------------- // Test the above public static void main(String[] args) { new WaitLoops3(); } } // end class
Reply With Quote