/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
package bai01;
/**
*
* @author hotua
*/
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Bai01 {
/**
* @param args the command line arguments
*/
private static DownloadTask downloadTask;
public static void main(String[] args) {
JFrame window=new JFrame ("NOTPAD WITH TAG");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(800,600);
JTabbedPane tabbedPane=new JTabbedPane();
tabbedPane.setFocusable(false);
//Add button
JButton addButton= new JButton("+");
addButton.setBorder(null);
addButton.setFocusPainted(false);
addButton.setContentAreaFilled(false);
addButton.setPreferredSize(new Dimension(30,30));
//Counter
AtomicLong counter = new AtomicLong(0);
//JLabel valueLabel = new JLabel("Value: 0");
addButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
JPanel formPanel = createFormPanel();
long nextValue = counter.incrementAndGet();
JTextArea textArea=new JTextArea();
JLabel tabTitleLabel=new JLabel("TAB"+nextValue);
tabTitleLabel.setPreferredSize(new Dimension(20, 10));
tabbedPane.addTab("TAB"+nextValue,textArea);
tabbedPane.setComponentAt(tabbedPane.getTabCount()-1, formPanel);
}
});
tabbedPane.addTab("", null);
tabbedPane.setTabComponentAt(0, addButton);
window.add(tabbedPane);
window.setVisible(true);
}
private static JPanel createFormPanel() {
JPanel panel = new JPanel();
//panel.setLayout(new GridLayout(3, 2));
JLabel linkLabel = new JLabel("Link:");
JTextField linkField = new JTextField(70);
linkField.setColumns(75);
JLabel saveLabel = new JLabel("Save to:");
JTextField saveField = new JTextField(70);
saveField.setColumns(75);
JLabel progressLabel = new JLabel("Progress:");
JProgressBar progressBar = new JProgressBar();
progressBar.setMinimum(0);
progressBar.setMaximum(100);
progressBar.setPreferredSize(new Dimension(753, 20));
progressBar.setStringPainted(true);
JButton startButton = new JButton("Start");
JButton stopButton = new JButton("Stop");
stopButton.setEnabled(false);
//Event download
startButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
String link=linkField.getText();
String save=saveField.getText();
startButton.setEnabled(false);
stopButton.setEnabled(true);
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(new DownloadTask(link, save, progressBar));
executor.shutdown();
}
});
stopButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ExecutorService executor = Executors.newSingleThreadExecutor();
if(downloadTask != null) {
// Stop the download task and shut down the executor
downloadTask.cancel(true);
executor.shutdownNow();
}
// Enable start button and disable stop button
startButton.setEnabled(true);
stopButton.setEnabled(false);
}
});
panel.add(linkLabel);
panel.add(linkField);
panel.add(saveLabel);
panel.add(saveField);
panel.add(progressLabel);
panel.add(progressBar);
panel.add(startButton);
panel.add(stopButton);
return panel;
}
}
//Download Task class
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package bai01;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Paths;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
/**
*
* @author hotua
*/
public class DownloadTask implements Runnable {
private String link;
private String save;
private JProgressBar progressBar;
public DownloadTask(String link, String save,JProgressBar progressBar) {
this.link = link;
this.save = save;
this.progressBar=progressBar;
}
@Override
public void run() {
try {
URL url = new URL(link);
try (BufferedInputStream in = new BufferedInputStream(url.openStream());
FileOutputStream fileOutputStream = new FileOutputStream(Paths.get(save).getFileName().toString())) {
int bytesRead;
byte[] buffer = new byte[1024];
long totalBytesRead = 0;
long fileSize = url.openConnection().getContentLengthLong();
while ((bytesRead = in.read(buffer, 0, 1024)) != -1) {
fileOutputStream.write(buffer, 0, bytesRead);
totalBytesRead += bytesRead;
int progress = (int) ((totalBytesRead * 100) / fileSize);
SwingUtilities.invokeLater(() -> progressBar.setValue(progress));
}
SwingUtilities.invokeLater(() -> progressBar.setValue(100));
JOptionPane.showMessageDialog(null, "Download complete!");
} catch (IOException e) {
SwingUtilities.invokeLater(() -> {
JOptionPane.showMessageDialog(null, "Error downloading file: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
});
}
} catch (IOException e) {
e.printStackTrace();
}
}
}