Results 1 to 2 of 2
  1. #1
    pkumar85 is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default Cannot Implement Stop And Pause...multiple Actionlisteners..please Help.

    hi
    I have a problem in the GUI where im not able to implement stop and pause buttons while "play" is pressed where "play" can perform: audio and video playback. While its playing the button stays pressed and its not registering actions with other buttons..(i only need pause and stop to work while there are other buttons in the GUI)...

    code snippet: the action performed function...sorry its big..
    play basically plays the showimage(i) which plays video frame by frame..where i is the ith frame...

    public void actionPerformed(ActionEvent event)
    {
    if (event.getSource()== button2)
    {if (com1==0&&com2==1)
    {//scalling the video
    JTextField input1 = new JTextField();
    JTextField input2 = new JTextField();
    int num = fps.getValue();
    Object[] msg = {"Input X:", input1, "Input Y:", input2};

    JOptionPane op = new JOptionPane(
    msg,
    JOptionPane.QUESTION_MESSAGE,
    JOptionPane.OK_CANCEL_OPTION,
    null,
    null);

    JDialog dialog = op.createDialog(this, "Enter Resize Values");
    dialog.setVisible(true);

    int result = JOptionPane.OK_OPTION;

    try
    {
    result = ((Integer)op.getValue()).intValue();
    }
    catch(Exception uninitializedValue)
    {}

    if(result == JOptionPane.OK_OPTION)
    {
    rv1 = Integer.parseInt( input1.getText());
    rv2 = Integer.parseInt( input2.getText());
    panel.setresizev(rv1,rv2);


    System.out.println("input video x : " + rv1);
    System.out.println("input video y : " + rv2);
    }
    else
    {
    System.out.println("Canceled");
    }
    panel.showImage(num);a1.updateUI();
    }
    }

    if (event.getSource()== button2)
    {if (com1==2&&com2==1)
    {//scalling the image
    JTextField input1 = new JTextField();
    JTextField input2 = new JTextField();

    Object[] msg = {"Input X:", input1, "Input Y:", input2};

    JOptionPane op = new JOptionPane(
    msg,
    JOptionPane.QUESTION_MESSAGE,
    JOptionPane.OK_CANCEL_OPTION,
    null,
    null);

    JDialog dialog = op.createDialog(this, "Enter Resize Values");
    dialog.setVisible(true);

    int result = JOptionPane.OK_OPTION;

    try
    {
    result = ((Integer)op.getValue()).intValue();
    }
    catch(Exception uninitializedValue)
    {}

    if(result == JOptionPane.OK_OPTION)
    {
    ri1 = Integer.parseInt( input1.getText());
    ri2 = Integer.parseInt( input2.getText());
    panel.setresizei(ri1,ri2);


    System.out.println("input image x : " + ri1);
    System.out.println("input image y : " + ri2);
    }
    else
    {
    System.out.println("Canceled");
    }

    n1.setSize(ri1,ri2);
    a1.updateUI();
    }
    }





    if (event.getSource()== button1)
    {if (com1==0)
    {
    fc = new JFileChooser();
    int returnVal = fc.showOpenDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
    filev = fc.getSelectedFile();
    panel.setfilev(filev);panel.showImage(0);a1.update UI();
    }
    }
    if (com1==1)
    {
    fc = new JFileChooser();
    int returnVal = fc.showOpenDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
    filea = fc.getSelectedFile();
    //panel.setfilea(file1);
    }
    }

    if (com1 ==2)
    {
    fc = new JFileChooser();
    int returnVal = fc.showOpenDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
    filei = fc.getSelectedFile();
    panel.setfilei(filei);
    //panel.simage();

    n1=panel.simage();
    n1.addMouseMotionListener(new MouseMotionHandler());
    a1.add(n1); n1.setSize(240,180);
    n1.setVisible(true);a1.updateUI();
    }
    }}


    if (event.getSource()== button2)
    {

    if (com2==1)
    {

    }
    }

    if (event.getSource()== button4)
    {


    }

    if (event.getSource()== publish)
    {
    publish.setText("Published");}

    if (event.getSource()== play)
    {
    play.setEnabled(false);
    int num = fps.getValue();
    if(filei!=null)
    {
    n1.setVisible(false);
    }

    if(filea!=null)
    {PCMFilePlayerLeveler pcm1;
    try {
    pcm1 = new PCMFilePlayerLeveler(filea);
    } catch (UnsupportedAudioFileException e) {

    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } catch (LineUnavailableException e) {
    e.printStackTrace();
    }}
    try{
    for(int i=num;i<192;i++)
    {

    if(filev!=null)
    {
    panel.showImage(i);}
    if(filei!=null)
    {
    if(i>10 && i<40 )
    {
    n1.setVisible(true);

    a1.updateUI();
    System.out.println(" value of i = "+i);
    }
    else{
    n1.setVisible(false);a1.updateUI();
    }}
    }
    }catch(Exception e){System.out.println(e);}

    }
    if (event.getSource()== pause)
    {
    play.setEnabled(false);
    System.out.println("clicked pause");

    }
    if (event.getSource()== stop)
    {
    try{
    play.setEnabled(false);
    }
    catch(Exception e){System.out.println("ERROR - SOUND FILE IS NOT SET - PLEASE SET SOUND FILE");}

    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Your problem is that you're tying up the EDT (Event Dispatch Thread), the Swing application's main thread that is involved with drawing the application and interacting with the user. This happens when you call a method that is very time or CPU intensive on the EDT. The solution is to call these methods on a different background thread.

    Have a look here for more details on this and for a solution, the SwingWorker: Concurrency in Swing

Similar Threads

  1. pause until JFrame is closed.
    By Tamu in forum Advanced Java
    Replies: 8
    Last Post: 11-30-2008, 09:46 PM
  2. how to stop a thread
    By willemjav in forum Advanced Java
    Replies: 19
    Last Post: 09-10-2008, 07:11 AM
  3. Replies: 3
    Last Post: 06-05-2008, 11:10 PM
  4. How to implement multiple peers downloads
    By breznev in forum Advanced Java
    Replies: 2
    Last Post: 03-12-2008, 09:03 PM
  5. Help with Pause
    By trill in forum Java Applets
    Replies: 2
    Last Post: 09-28-2007, 08:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •