Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8999253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:09:19+00:00 2026-06-16T00:09:19+00:00

In my program, I am displaying images with a song in background. After song

  • 0

In my program, I am displaying images with a song in background.
After song is finished, i need to close frame and JInternalFrame.
Here is the code:

package projectfinal;
import javax.swing.*;
import java.io.File;
import java.io.IOException;

/* MORE IMPORTS */
public class ImagePanel extends javax.swing.JFrame {

    public static class audio extends JApplet {

        private static final int EXTERNAL_BUFFER_SIZE = 128000;

        public void aaudio() {

            String strFilename = "E:/zehreelay.wav";
            File soundFile = new File(strFilename);


            AudioInputStream audioInputStream = null;
            try {
                audioInputStream = AudioSystem.getAudioInputStream(soundFile);
            } catch (Exception e) {

                e.printStackTrace();
                System.exit(1);
            }


            AudioFormat audioFormat = audioInputStream.getFormat();

            SourceDataLine line = null;
            DataLine.Info info = new DataLine.Info(SourceDataLine.class,
                audioFormat);
            try {
                line = (SourceDataLine) AudioSystem.getLine(info);

                line.open(audioFormat);
            } catch (LineUnavailableException e) {
                e.printStackTrace();
                System.exit(1);
            } catch (Exception e) {
                e.printStackTrace();
                System.exit(1);
            }
            line.start();
            int nBytesRead = 0;
            byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
            while (nBytesRead != -1) {
                try {
                    nBytesRead = audioInputStream.read(abData, 0, abData.length);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (nBytesRead >= 0) {
                    int nBytesWritten = line.write(abData, 0, nBytesRead);
                }

            }
            line.drain();

            line.close();

            jInternalFrame1.setVisible(false);
            /**
            * closing internal frame
            */
        }
    }

    /**
    * Creates new form NewJFrame
    */
    public ImagePanel() {

        initComponents();

    }

    @SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jInternalFrame1 = new javax.swing.JInternalFrame();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();

        javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());

        //////////more code////////////////////////////////////
        jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
        jInternalFrame1.addComponentListener(new java.awt.event.ComponentAdapter() {

            public void componentHidden(java.awt.event.ComponentEvent evt) {
                hiden(evt);
            }
        });
        /////adding a listner for component///
    }

    public static void main(String args[]) {


        start();

    }

    private void hiden(java.awt.event.ComponentEvent evt) {

        setVisible(false);
    }

    public static void start() {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ImagePanel().setVisible(true);
            }
        });
        audio obj = new audio();
        obj.aaudio();
    }
    private static javax.swing.JInternalFrame jInternalFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
/////more variables...//////
}

This code is fine, but when I tried out without adding a component listener,
that is, getRootPane().SetVisible(false) or jInternalFrame1.getContentPane().setVisible(false) or setVisible(false) none of them was working, but it worked out only after adding a component hidden listener. Why? Any reasons?Then how come JInternalFrame was working (JInternalFrame.setVisible(false))? Is this something to do with rootPane()?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:09:20+00:00Added an answer on June 16, 2026 at 12:09 am

    As discussed in Initial Threads, “An initial thread schedules the GUI creation task by invoking invokeLater or invokeAndWait.” You are trying to both play a sound and operate a GUI on the same initial thread. Instead, start the sound on the initial thread and construct the GUI on the EDT, as shown here, or start a separate thread to play the sound, as shown here.

    I don’t want to use AWT dispatch thread.

    Right, you don’t want to use the AWT event dispatch thread to play sound, but you must use it for the GUI.

    The question is somewhat unanswered. The playing of sound is light, so must be handled by single EDT.

    I can only speculate that using a different component changed the timing enough to affect the result on your platform. Until the program is correctly synchronized, there’s always some environment in which it will work and some in which it will fail. The essential rule is this: don’t block the EDT. See also this answer, this answer and the Java Sound tag.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to toggle displaying an image in my program. Here is my code
I am using frame animation for displaying some images. But it's working only in
I have written a small program displaying sounds and images on the screen when
I've some huge images (7000*5000) to load simultaneously in my program, which I'm displaying
hey there, Im having problems displaying my results in this program but the program
This program returns: package main import ( flag fmt ) func main() { num_agents
I have two problems in my program: 1.After taking a picture, this is what
I am having issues with my program displaying information from different if statements when
Here is the code I wrote: @echo off :top set chkScheduleTime= set chkScheduleName= set
I am giving notification by a background service. In notification i am displaying an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.