Hey again guys, I've been stressing over this problem for the past few hours now, and cannot figure out why it's doing what it's doing. I'm rather new to java...but here we go :)

I currently have a slideshow class -and here it is. Sorry if it's sloppily written code...but you know, new to java and what not :).

public class SlideShow {

	private static JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"));
	private JFrame slideshowFrame;
	private JFrame buttonFrame;
	private ImagePanel display;
	private ArrayList<OFImage> images;
	private JPanel SScontentPane;
	private int imageCounter;
	private JSlider imageSlider; 
	private Hashtable<Integer, JLabel> table;
	private JScrollPane scrollPane;
	private ArrayList<Integer> positions;
	private int imageSpacing;

	private boolean pause;
	private JButton startslideshow;
	private JButton reset;
	private JButton loadimages;

	public SlideShow()
	{

		imageSpacing = 0;
		imageCounter = -1;
		images = new ArrayList<OFImage>();
		positions = new ArrayList<Integer>();
		makeFrames();
		slideshowFrame.setVisible(true);
	}


	private void makeFrames()
	{
		slideshowFrame = new JFrame("Slideshow");
		slideshowFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		SScontentPane = (JPanel)slideshowFrame.getContentPane();
		SScontentPane.setLayout(new BorderLayout(6, 6));
		display = new ImagePanel();
		scrollPane = new JScrollPane(display);
		scrollPane.setViewportView(display);
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		SScontentPane.add(scrollPane, BorderLayout.CENTER);
		imageSlider = new JSlider();
		imageSlider.setPaintTicks(true);
		imageSlider.setSnapToTicks(true);
		imageSlider.setPaintLabels(true);
		table = new Hashtable<Integer, JLabel>();
		SScontentPane.add(imageSlider, BorderLayout.SOUTH);
		slideshowFrame.pack();

		JPanel grid = new JPanel(new GridLayout(1,3));
		loadimages = new JButton("Load Images");
		loadimages.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				loadImages();
				loadimages.setEnabled(false);
				reset.setEnabled(true);
				startslideshow.setEnabled(true);
			}
		});
		loadimages.setVisible(true);
		grid.add(loadimages);

		startslideshow = new JButton("Start Slideshow...");
		startslideshow.setEnabled(false);
		startslideshow.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				int interval = Integer.parseInt(JOptionPane.showInputDialog(null, "How many seconds interval? : ","", 1));
				slideShow(interval);
				startslideshow.setEnabled(false);
				reset.setEnabled(true);
			}
		});
		reset = new JButton("Reset");
		reset.setEnabled(false);
		reset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				loadimages.setEnabled(true);
				reset.setEnabled(false);
				startslideshow.setEnabled(false);
				images.clear();
				table.clear();
				positions.clear();
				imageSpacing = 0;
				imageCounter = -1;
				display.setImage(null);
			}
		});


		startslideshow.setVisible(true);
		grid.add(startslideshow);
		grid.add(reset);



		SScontentPane.add(grid, BorderLayout.NORTH);


		//Small frame with load/pause button etc. 




	}

	private void loadImages()
	{
		images.clear();
		fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		int returnVal = fileChooser.showOpenDialog(slideshowFrame);
		if(returnVal != JFileChooser.APPROVE_OPTION) {
			return;  // cancelled
		}
		File selectedDirectory = fileChooser.getSelectedFile();
		File[] imageFiles = selectedDirectory.listFiles();
		int counter = 0;
		int pos = (imageSlider.getMaximum()/(imageFiles.length-1));
		imageSlider.setMajorTickSpacing(100/(imageFiles.length-1));
		imageSpacing = (100/(imageFiles.length-1));
		System.out.println(imageFiles.length);
		for(File f : imageFiles)
		{

			images.add(ImageFileManager.loadImage(f));
			table.put((pos*counter), new JLabel(f.getName()));
			positions.add(pos*counter);
			counter++;

		}
		if(images.get(0) != null)
		{
			display.setImage(images.get(0));
			slideshowFrame.setPreferredSize(display.getPreferredSize());
			slideshowFrame.pack();
			imageSlider.setValue(0);

		}
		imageSlider.setLabelTable(table);
		imageSlider.addChangeListener(new ChangeListener()
		{
			public void stateChanged(ChangeEvent e) {
				
//im well aware this throws errors, but i couldn't think of anything else to do, any suggestions are welcome :)
for(int i = 0 ; i < imageSlider.getMaximum(); i++)
					if(i == imageSlider.getValue())
					{
						display.setImage(images.get(positions.indexOf(i)));
					}


			}
		});
	}

	private void slideShow(int interval)
	{
		imageSlider.setValue(0);
		for(int slideshowCounter= 0; slideshowCounter < images.size(); slideshowCounter++)
		{
			imageSlider.setValue(imageSpacing*slideshowCounter);	
			doPause(interval);
		}
	}


	public void doPause(int seconds){
		long t0, t1;
		t0=System.currentTimeMillis( );
		t1=System.currentTimeMillis( )+(seconds*1000);
		do {
			t0=System.currentTimeMillis( );

		} while (t0 < t1);
	}


}

I have a problem, it works fine if I create a new instance of it from my main method, but if I try to create an instance of it from within my main application ( from a menu option new slide show...which just creates a new slideshow ) - then the slideshow doesn't actually work. It will display the first image - until the cummulitive pauses are over, so seconds*number of images, then jump to the last image. During this time the java program seems to crash?

If I create a new one outside of this, in the main method. The slideshow works perfectly...no kinks atall.... hmmm.

Thanks, ross.

This article has been dead for over six months. Start a new discussion instead.