





















































This is the third part of the Twitter Java client tutorial article series! In Build your own Application to access Twitter using Java and NetBeans: Part 2 we:
//code for the Friends timeline
try {
java.util.List<Status> statusList = twitter.getFriendsTimeline();
jPanel1.setLayout(new GridLayout(statusList.size(),1));
for (int i=0; i<statusList.size(); i++) {
statusText = new JLabel(String.valueOf(statusList.get(i).getText()));
statusUser = new JLabel(statusList.get(i).getUser().getName());
JPanel individualStatus = new JPanel(new GridLayout(2,1));
individualStatus.add(statusUser);
individualStatus.add(statusText);
jPanel1.add(individualStatus);
}
} catch (TwitterException e) {
JOptionPane.showMessageDialog (null, "A Twitter error ocurred!");}
jPanel1.updateUI();
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
JLabel statusText;
JLabel statusUser;
Let’s examine what we did in the previous exercise. On steps 2-5 you added a JTabbedPane container and created a Home tab where the JScrollPane1 and JTextArea1 controls show your latest tweets, and then on steps 6-8 you added the JPanel1 container inside the JScrollPane2 container.
On step 9 you changed the name of the new tab to Friends and then added some code to show your friends’ latest tweets. As in previous exercises, we need to add the code inside a try-catch block because we are going to call the Twitter4J API to get the last 20 tweets on your friends timeline.
The first line inside the try block is:
java.util.List<Status> statusList = twitter.getFriendsTimeline();
This line gets the 20 most recent tweets from your friends’ timeline, and assigns them to the statusList variable. The next line,
jPanel1.setLayout(new GridLayout(statusList.size(),1));
sets your jPanel1 container to use a layout manager called GridLayout, so the components inside jPanel1 can be arranged into rows and columns. The GridLayout constructor requires two parameters; the first one defines the number of rows, so we use the statusList.size() function to retrieve the number of tweets obtained with the getFriendsTimeline() function in the previous line of code. The second parameter defines the number of columns, and in this case we only need 1 column.
The next line,
for (int i=0; i<statusList.size(); i++) {
starts a for loop that iterates through all the tweets obtained from your friends’ timeline. The next 6 lines are executed inside the for loop. The next line in the execution path is
statusText = new JLabel(String.valueOf(statusList.get(i).getText()));
This line assigns the text of an individual tweet to a JLabel control called statusText. You can omit the String.valueOf function in this line because the getText() already returns a string value –I used it because at first I was having trouble getting NetBeans to compile this line, I still haven’t found out why, but as soon as I have an answer, I’ll let you know. As you can see, the statusText JLabel control was created programmatically; this means we didn’t use the NetBeans GUI interface.
The next line,
statusUser = new JLabel(statusList.get(i).getUser().getName());
creates a JLabel component called statusUser, gets the name of the user that wrote the tweet through the statusList.get(i).getUser().getName() method and assigns this value to the statusUser component. The next line,
JPanel individualStatus = new JPanel(new GridLayout(2,1));
creates a JPanel container named individualStatus to contain the two JLabels we created in the last two lines of code. This panel has a GridLayout with 2 rows and one column. The first row will contain the name of the user that wrote the tweet, and the second row will contain the text of that particular tweet. The next two lines,
individualStatus.add(statusUser);
individualStatus.add(statusText);
add the name of the user (statusUser) and the text of the individual tweet (statusText) to the individualStatus container, and the next line,
jPanel1.add(individualStatus);
adds the individualStatus JPanel component – which contains the username and text of one individual tweet –to the jPanel1 container. This is the last line of code inside the for loop. The catch block shows an error message in case an error occurs when executing the getFriendsTimeline() function, and the jPanel1.updateUI(); line updates the jPanel1 container so it shows the most recent information added to it.
Now you can see your friends’ latest tweets along with your own tweets, but we need to improve the way tweets are displayed, don’t you think so?
For starters, let’s change some font attributes to show the user name in bold style and the text of the tweet in plain style. Then we’ll add a black border to separate each individual tweet.
import java.awt.Font;
Font newLabelFont = new Font(statusUser.getFont().getName(),Font.PLAIN,statusUser.getFont().getSize());
statusText.setFont(newLabelFont);
import javax.swing.BorderFactory;
import java.awt.Color;
individualStatus.setBorder(BorderFactory.createLineBorder(Color.black));