7 min read

In the 3rd part of this article series, we learnt about:

  • Added a Tabbed Pane component to your SwingAndTweet application, to show your own timeline on one tab and your friend’s timeline on another tab
  • Used a JScrollPane component to add vertical and horizontal scrollbars to your friends’ timeline list
  • Used the getFriendsTimeline() method from the Twitter4J API to get the 20 most recent tweets from your friend’s timeline
  • Applied font styles to your JLabel components via the Font class
  • Added a black border to separate each individual tweet by using the BorderFactory and Color classes
  • Added the date and time of creation of each individual tweet by using the getCreatedAt() method from the twitter4j.Status interface, along with the Date class.

All those things, we learnt in the third part of the article series were a big improvement for our Twitter client, but wouldn’t it be cool if you could click on the URL links from your friends’ time line and then a web browser window would open automatically to show you the related webpage? Well, after reading this part of the article series, you’ll be able to integrate this functionality into your own Twitter client among other things.

Here are the links to the earlier articles of this article series:

Read Build your own Application to access Twitter using Java and NetBeans: Part 1

Read Build your own Application to access Twitter using Java and NetBeans: Part 2

Read Build your own Application to access Twitter using Java and NetBeans: Part 3

Using a JEditorPane component

Till now, we’ve been working with JPanel objects to show your Twitter information inside the JTabbedPane component. But as you can see from your friends’ tweets, the URL links that show up aren’t clickable. And how can we make them clickable? Well, fortunately for us there’s a Swing component called JEditorPane that will let us use HTML markup, so the URL hyperlinks will show up as if you were on a web page. Cool, huh? Now let’s start with the dirty job…

  1. Open your NetBeans IDE along with your SwingAndTweet project, and make sure you’re in the Source View.
  2. Scroll up to the import declarations section and type import javax.swing.JEditorPane; right below the last import declaration, so your code looks as shown below:

  3. Now scroll down to the last line of code, JLabel statusUser;, and type JEditorPane statusPane; just below that line, as shown in the following screenshot:

  4. The next step is to add statusPane to the JTabbedPane1 component in your application. Scroll through the code until you locate the //code for the Friends timelineline and the try-block code below that line; then type the following code block just above the for statement:
    String paneContent = new String();
    statusPane = new JEditorPane();
    statusPane.setContentType("text/html");
    statusPane.setEditable(false);
  5. The following screenshot shows how your code must look like after inserting the above block of code (the red square indicates the lines you must add):
  6. Now scroll down through the code inside the for statement and type paneContent = paneContent + statusUser.getText() + “<br>” + statusText.getText() + “<hr>”; right after the jPanel1.add(individualStatus); line, as shown below:
  7. Then add the following two lines of code after the closing brace of the try block:
    statusPane.setText(paneContent);
    jTabbedPane1.add("Friends - Enhanced", statusPane);
  8. The following screenshot shows how your code must look like after the insertion:
  9. Run your application and log into your Twitter account. A new tab will appear in your Twitter client, and if you click on it you’ll see your friends’ latest tweets, as in the following screenshot:
  10. If you take a good look at the screen, you’ll notice the new tab you added with the JEditorPane component doesn’t show a vertical scroll bar so you can scroll up and down to see the complete list. That’s pretty easy to fix: First add the import javax.swing.JScrollPane; line to the import declarations section and then replace the jTabbedPane1.add(“Friends – Enhanced”, statusPane); line you added on step 9 with the following lines:
    JScrollPane editorScrollPane = new JScrollPane(statusPane,
    JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, // vertical bar policy
    JScrollPane.HORIZONTAL_SCROLLBAR_NEVER ); // horizontal bar policy
    jTabbedPane1.add("Friends - Enhanced", editorScrollPane);
  11. Your code should now look like this:
  12. Run your Twitter application again and this time you’ll see the vertical scrollbar:

Let’s stop for a while to review our progress so far. In the first step above the exercise, you added an import declaration to tell the Java compiler that we need to use an object from the JEditorPane class. In step 3, you added a JEditorPane object called statusPane to your application. This object acts as a container for your friends’ tweets.

And in case you’re wondering why we didn’t use a regular JPanel object, just remember that we want to make the URL links in your friends’ tweets clickable, so when you click on one of them, a web browser window will pop up to show you the web page associated to that hyperlink.

Now let’s get back to our exercise. In step 4, you added four lines to your application’s code. The first line:

String paneContent = new String();

creates a String variable called paneContent to store the username and text of each individual tweet from your friends’ timeline. The next three lines:

statusPane = new JEditorPane();
statusPane.setContentType("text/html");
statusPane.setEditable(false);

create a JEditorPane object called statusPane, set its content type to text/html so we can include HTML markup and make the statusPane non-editable, so nothing gets messed up when showing your friends’ timeline.

Now that we have the statusPane ready to roll, we need to fill it up with the information related to each individual tweet from your friends. That’s why we need the paneContent variable. In step 6, you inserted the following line:

paneContent = paneContent + statusUser.getText() + "<br>" + statusText.getText() + "<hr>";

inside the for block to add the username and the text of each individual tweet to the paneContent variable. The <br> HTML tag inserts a line break so the username appears in one line and the text of each tweet appears in another line. The <hr> HTML tag inserts a horizontal line to separate one tweet from the other.

Once the for loop ends, we need to add the information from the paneContent variable to the JEditorPane object called statusPane. That’s why in step 7, you added the following line:

statusPane.setText(paneContent);

and then the

jTabbedPane1.add("Friends - Enhanced", statusPane);

line creates a new tab in the jTabbedPane1 component and adds the statusPane component to it, so you can see the friends timeline with HTML markup.

In step 10, you learned how to create a JScrollPane object called editorScrollPane to add scrollbars to your statusPane component and integrate them into the jTabbedPane1 container. In this example, the JScrollPane constructor requires arguments: the statusPane component, the vertical scrollbar policy and the horizontal scrollbar policy. There are three options you can choose for your vertical and horizontal scrollbars: show them as needed, never show them or always show them.

In this specific case, we need the vertical scrollbar to show up as needed, in case the list of your friends’ tweets doesn’t fit the screen, so we use the JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED policy. And since we don’t need the horizontal bar to show up because the statusPane component can adjust its horizontal size to fit your application’s window, we use the JScrollPane.HORIZONTAL_SCROLLBAR_NEVER policy.

The last line of code from step 10 adds the editorScrollPane component to the jTabbedPane1 container instead of adding the statusPane component directly, because now the JEditorPane component is contained within the JScrollPane component.

Now let’s see how to convert the URL links to real hyperlinks.

LEAVE A REPLY

Please enter your comment!
Please enter your name here