Results 1 to 4 of 4
  1. #1
    Basit is offline Member
    Join Date
    Aug 2009
    Posts
    8
    Rep Power
    0

    Unhappy Applet hangs while using JOptionPane.xxx()

    Hi to all,
    Hope you all will be fine. I have an applet of voicerecorder. The problem is when you start to record the sound it works fine.Now i want to put a condition in it, that sound can not be recorded more than 3 minutes for this, in the initial step i put the condition like

    if(minutes > 3){
    JOptionPane.showMessageDialog(null, "Yor are not allowed to record more than 3 minutes", "Warning",JOptionPane.WARNING_MESSAGE);
    }
    But after executing this it shows a dialog box with nothing in it and after showing a long arror messages in the NetBeans output pane and when i click the exit button then the applet exits.The code snippet is as follws that do this part

    Java Code:
     public void start() {
                thread = new Thread(this);
                thread.setName("SamplingGraph");
                thread.start();
                seconds = 0;
                minutes = 0; 
            }
    
            public void stop() {
                if (thread != null) {
                    thread.interrupt();
                }
                thread = null;
            }
    
            public void run() {
                seconds = 0;
                minutes = 0; 
                while (thread != null) {
                    if ((playback.line != null) && (playback.line.isOpen()) ) {
    
                        int milliseconds = (int)(capture.line.getMicrosecondPosition() / 1000);
                        seconds =(int)  (milliseconds / 1000.0);
                        minutes = (int)((milliseconds / 1000.0) / 60 );
    
    
                    } else if ( (capture.line != null) && (capture.line.isActive()) ) {
    
                        int milliseconds = (int)(capture.line.getMicrosecondPosition() / 1000);
                        seconds =(int)  (milliseconds / 1000.0);
                        minutes = (int)((milliseconds / 1000.0) / 60 );
                    }
    
                    try { thread.sleep(100); } catch (Exception e) { break; }
    
                    repaint();
    
                    while ((capture.line != null && !capture.line.isActive()) ||
                           (playback.line != null && !playback.line.isOpen()))
                    {
                        try { thread.sleep(10); } catch (Exception e) { break; }
                    }
                }
                seconds = 0;
                minutes = 0; 
                repaint();
            }
    Then the part where i modify the things and things start going wrong
    Java Code:
    .......
    ......
      LineBreakMeasurer lbm = new LineBreakMeasurer(aci, frc);
                    float x = 5, y = 25;
                    lbm.setPosition(0);
                    while (lbm.getPosition() < errStr.length()) {
                        TextLayout tl = lbm.nextLayout(w-x-5);
                        if (!tl.isLeftToRight()) {
                            x = w - tl.getAdvance();
                        }
                        tl.draw(g2, x, y += tl.getAscent());
                        y += tl.getDescent() + tl.getLeading();
                    }
                } else if (capture.thread != null) {
                    g2.setColor(Color.black);
                    g2.setFont(font12);
                 [COLOR="Red"]   g2.drawString("Length: " + String.valueOf( minutes) + " min" + " : " + String.valueOf(seconds) + " sec", 3, h-4);
                    if(minutes > 3){
                         JOptionPane.showMessageDialog(null, "Yor are not allowed to record more than three minutes", "Warning",JOptionPane.WARNING_MESSAGE);
                         capture.stop();
                         fileName = " Press Done when complete";
                         samplingGraph.stop();
                         playButton.setEnabled(true);
                         pauseButton.setEnabled(false);
                         doneButton.setEnabled(true);
                         recordButton.setText("Record");                
                     }[/COLOR]
                    
    
                } else {
                    g2.setColor(Color.black);
                    g2.setFont(font12);
                    g2.drawString( fileName , 3, h-4);
                   // g2.drawString("File has been recorded." , 3, h-4);
    
                    if (audioInputStream != null) {
                        // .. render sampling graph ..
                        g2.setColor(jfcBlue);
                        for (int i = 1; i < lines.size(); i++) {
                            g2.draw((Line2D) lines.get(i));
                        }
    
                        // .. draw current position ..
                        if (seconds != 0) {
                            double loc = seconds/duration*w;
                            g2.setColor(pink);
                            g2.setStroke(new BasicStroke(3));
                            g2.draw(new Line2D.Double(loc, 0, loc, h-INFOPAD-2));
                        }
                    }
                }
    Please tell me where i m wrong.I also attach a file when i run the applet code
    Thanks in advance
    Attached Thumbnails Attached Thumbnails Applet hangs while using JOptionPane.xxx()-problem-joptionpanexxx-.jpg  
    Last edited by Fubarable; 08-08-2009 at 01:16 PM. Reason: code tags added for readability

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

    Default

    Your problem is that you are trying to call Thread.sleep on the EDT, the main thread the runs Swing apps, and that this is putting your entire GUI to sleep. Please read this tutorial as it will explain your problem and the solution: Concurrency in Swing

  3. #3
    Basit is offline Member
    Join Date
    Aug 2009
    Posts
    8
    Rep Power
    0

    Default

    Hi:)
    Thanks for your help.:)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Discussion: JVM hangs
    By Arica in forum Advanced Java
    Replies: 5
    Last Post: 06-29-2009, 05:29 AM
  2. java mail hangs
    By ashwinkumar18 in forum Advanced Java
    Replies: 6
    Last Post: 03-14-2009, 11:38 AM
  3. [SOLVED] Multiple client single server problem(it hangs halfway)
    By kellaw in forum Threads and Synchronization
    Replies: 4
    Last Post: 10-03-2008, 08:41 PM
  4. Replies: 1
    Last Post: 10-02-2008, 08:02 PM
  5. Replies: 1
    Last Post: 10-02-2008, 08:01 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
  •