private void getPreviousButtonActionPerformed(java.awt.event.ActionEvent evt)                                                  
    {                                                      
        
        List<Tweet> listOfTweets = remoteBean.getMoreTweets(5);
    
        StringBuilder displayStringBuilder = new StringBuilder();
        
        if(listOfTweets != null)
        {
           
            for (Tweet tweet : listOfTweet)
            {
                displayStringBuilder.append(tweet.toString() + "\n");
            }
            textArea.setText(displayStringBuilder.toString());
        }
    
    }

As it stands when i run the code that this method is in and enter a number of tweets they are displayed in the text area of the GUI, when i click the getPrevious button it displays the last 5 tweets stored in the list, I want to add code so that when i click the getPrevious button again it gives the remaining tweets made and if i click again it will say no tweets remaining in the text area.

I know i need to use some sort of loop but i cant get my head around what loop to use or where to write it I have thought about getting the size of the list and using a

if(listOfTweets.Size() > 0)
{
   remoteBean.getMoreTweets(5);
   
   for (Tweet tweet : listOfTweet)
   {
      displayStringBuilder.append(tweet.toString() + "\n");
   }
   textArea.setText(displayStringBuilder.toString());
}
else
{
     textArea.setText("no more tweets");
}

Am I going the right way or am i completely off target

Recommended Answers

All 6 Replies

I'm now thinking i need to have a way off removing the 1st five from the list as this will just keep displaying them unless i do

If you keep track of where the currently displayed items came from, say with an index, then you can go forward by incrementing the index or backward by decrementing the index.
The display routine will get and display the n items start at the current value of index.

Will that work if i ask it display every 5 items and there is only 3 items left

Your method can easily check versus size and return the minimum of the requested number or the remainder of the list. As Norm already mentioned, you just need to keep track of your current list index.

Your method can easily check versus size and return the minimum of the requested number or the remainder of the list. As Norm already mentioned, you just need to keep track of your current list index.

Exactly... for example:

// Something like this
remoteBean.getPreviousTweets(5);

// More code here
// .....................

// definition of getPreviousTweets() method
public Tweet[] getPreviousTweets(int numberOfTweets) {

   // decide which is smaller, our current index or the supplied number of Tweets to return
   int numberToTrace = Math.min(numberOfTweets, m_currentIndex);

   // Perform rest of operations here
}
SwingUtilities.invokeLater(new Runnable() {

    @Override
    public void run() {
        textArea.setText(someString);
    }
});
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.