I am a high school student working on a project that will convert the video from a YouTube link into an MP3 and download it. This program opens a GUI and has a button that converts the video from the link into an MP3 and downloads it. However, after the link gets converted I want it to open up another GUI, but the program just ends. Is there any way to have the program to continue to run after the downloading process?
package com.company;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
public class GUI extends JFrame {
private static JTextField userLink;
private static JFrame frameOne;
private static JPanel panelOne;
private static JButton convertB;
private static JLabel titleOne;
private static JLabel name;
public String youtubeLink;
public static JLabel titleTwo;
public static JLabel info;
public GUI() {
panelOne = new JPanel();
frameOne = new JFrame();
frameOne.setSize(500, 300);
frameOne.setBackground(Color.CYAN);
frameOne.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frameOne.setVisible(true);
frameOne.add(panelOne);
panelOne.setLayout(null);
titleOne = new JLabel("Welcome to the Youtube"); //Title for first panel
titleOne.setBounds(55, 10, 500, 30);
titleOne.setFont(new Font("Courier", Font.BOLD, 30));
panelOne.add(titleOne);
titleTwo = new JLabel("to MP3 Converter!"); //Title for first panel
titleTwo.setBounds(100, 40, 500, 30);
titleTwo.setFont(new Font("Courier", Font.BOLD, 30));
panelOne.add(titleTwo);
name = new JLabel("By Zeid Akel");
name.setBounds(220, 80, 80, 25);
panelOne.add(name);
convertB = new JButton("Convert and Download"); //Convert and
convertB.setBounds(170, 220, 175, 50);
convertB.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
youtubeLink = userLink.getText();
System.out.println(youtubeLink);
String s;
String l = "youtube-dl -x --audio-format mp3 ";
String rl = l + youtubeLink;
File f = new File("/Users/zeidakel/YoutubeVideos"); //Location where
if (!f.exists()) {
System.out.println("Download Location does not exist"); //Check if
} else {
try {
Process p = Runtime.getRuntime().exec(rl); //Code put int terminal
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
System.out.println("Output:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
System.out.println("Errors:\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
frameOne.setVisible(false);
new GUI2();
System.exit(0);
} catch (IOException e1) {
}
}
}
});
panelOne.add(convertB);
userLink = new JTextField(); //Input area for youtube link
userLink.setBounds(135, 190, 250, 25);
panelOne.add(userLink);
info = new JLabel("Paste your link here:");
info.setBounds(140, 170, 250, 25);
panelOne.add(info);
frameOne.setVisible(true);
}
}
The GUI should always be running you should never close it or try to create a new JFrame.
Instead, the task to convert the video should be run in a separate Thread, so you don't prevent the GUI from responding to events.
An easy way to do this is to use a
SwingWorker
. Read the section from the Swing tutorail on Concurrency for more information and examples.Other comments:
Swing components should NOT be defined as static variables. That is not what the static keyword is used for.
Don't use a null layout and setBounds(). Swing was designed to be used with layout managers.