So i have this input.txt file, and there are 5 entries in it, I want to take each entry( in the format of Image1.jpg, Image2,jpg, etc) and store it into an arraylist. Then use java GUI to display each image by clicking a button, from image1 to image5.

First I stored each image name into an arraylist:

The input file:

Test1.jpg

Test2.jpg

Test3.jpg

Test4.jpg

Test5.jpg

The code to store these into arrayList:

try{

			BufferedReader inputFile = new BufferedReader(new FileReader(filename)); 
			String temp="";
			List<String> names = new ArrayList<String>();
			        
			while ((temp=inputFile.readLine()) != null)
			{
				names.add(temp);
			}
			String[] stringArray = names.toArray(new String[0]);

			String testOne = stringArray[0];
			String testTwo = stringArray[2];
			String testThree = stringArray[4];
			String testFour = stringArray[6];
			String testFive = stringArray[8];
			//System.out.println(testOne);
			

		}
		catch (Exception e)
		{
			System.err.println("Error: " + e.getMessage());
			}

testOne - testFive are the 5 image's names

But i am stuck on how to make a button that will update by click, first time clicking will give me image1, then if i click again, it will show image 2, then if i click again, it will show image 3 and so on.. I already have the GUI layout done, just not sure what to put under the event for the click.

private void jButtonActionPerformed(java.awt.event.ActionEvent evt) {
// ?
}

any help?

Just increment a "currentImageIndex" value in your click handler and call a routine to load and paint that image. I assume you got the basic image display working from your other thread.

Note that you could just leave them in your 'names' list without converting that to an array. You also don't need to store the blank lines.

Just increment a "currentImageIndex" value in your click handler and call a routine to load and paint that image. I assume you got the basic image display working from your other thread.

Note that you could just leave them in your 'names' list without converting that to an array. You also don't need to store the blank lines.

Thanks!
How can I not store the blank lines? Right now, my array stores all the blank lines as an element as well.

I tried to make it so that if the reads an empty line, it will not store it:

while ((temp=inputFile.readLine()) != null)
            {
                if(temp.matches(""))
                {
                }
                else{names.add(temp);}
                
            }

what should I put in the match pattern? so if it's all white spaces or blank it will not record?

A safe way to check for a "blank" line is to trim() the String then test if its length is 0.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.