Inside an ActionListener i am adding an image to a JPanel, which afterwards pops up. After a specific time that JFrame will be disposed.
This is what my mainFrame class looks like:
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
public class mainFrame {
JFileChooser chooser = new JFileChooser();
File[] files;
FileFilter filter = new FileNameExtensionFilter("Bilder", "gif", "png","jpg");
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
mainFrame window = new mainFrame();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public mainFrame() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 300, 119);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnStart = new JButton("Start !");
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try{
final imageFrame imageFrame = new imageFrame();
imageFrame.getContentPane().add(new JLabel(new ImageIcon(files[0].getAbsolutePath())));
imageFrame.setVisible(true);
new Timer().schedule(new TimerTask() {
public void run() {
imageFrame.dispose();
}
}, (Integer) 3000);
}catch(Exception e){
e.printStackTrace();
}
}
});
btnStart.setBounds(6, 62, 117, 29);
frame.getContentPane().add(btnStart);
JButton btnChooseFiles = new JButton("Choose Files !");
btnChooseFiles.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
/* JFileChooser deklarieren */
chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(true);
chooser.addChoosableFileFilter(filter);
chooser.showOpenDialog(null);
/* FileList erstellen */
files = chooser.getSelectedFiles();
/* Displaying String containing chosen files */
String fileList = "";
for (int i = 0; i < files.length; i++) {
fileList += files[i].getName() + "\n";
}
System.out.println("Chosen Files:\n" + fileList);
}
});
btnChooseFiles.setBounds(6, 6, 117, 29);
frame.getContentPane().add(btnChooseFiles);
}
}
And this is what my imageFrame looks like:
import java.awt.BorderLayout;
public class imageFrame extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
imageFrame frame = new imageFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public imageFrame() {
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
setBounds(100, 100, 623, 472);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
}
}
So far everything works great for one file, but what i got is a list of files, so i decided to make use of a for-loop wrapping the try-catch block in the mainFrame "Start"-ActionListener, which unfortunately does not work, as every image apart from the last one pops up immediately and only the last one stands to the delay of the timer.
All in all i just want to change the image inside the imageFrame Object, after a specific time. Let's say every 3 seconds the image inside the Pane should change, until we're through all chosen files.