November 27, 2014
Hot Topics:

Writing Your own Twitter Application Using Java, Swing, and Twitter4j

  • August 12, 2009
  • By Cesar Otero
  • Send Email »
  • More Articles »

Displaying your Followers

Our sample application will contain three main panels. One for displaying your followers, a second for displaying your tweets and updating them, and a third that will both search for tweets and send direct messages. The following code listing shows the code for follower display panel.

  import twitter4j.Twitter;
  import twitter4j.TwitterException;
  import twitter4j.User;
  
  import javax.swing.*;
  import java.net.URL;
  import java.awt.*;
  
  public class FollowerPanel extends JPanel {
      URL imageUrl;
      int panelWidth = 250;
      int panelHeight = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();
      java.util.List<User> followers;
      JLabel followerName;
      JLabel followerImg;
      JPanel followerPanel;
      JLabel followerTitleLabel;
      JScrollPane followerJScrollPane;
  
      public FollowerPanel(Twitter twitter) throws TwitterException {
          followers = twitter.getFollowers();
          this.setLayout(new GridLayout(1, followers.size()));
          this.setSize(panelWidth, panelHeight);
          this.setPreferredSize(new Dimension(panelWidth, panelHeight));
          this.setMinimumSize(new Dimension(panelWidth, panelHeight));
  
          followerPanel = new JPanel();
          followerPanel.setLayout(new GridLayout(followers.size(), 1, 0, 0));
          followerPanel.setSize(panelWidth, panelHeight);
          followerPanel.setPreferredSize(new Dimension(panelWidth, panelHeight));
          followerPanel.setMinimumSize(new Dimension(panelWidth, panelHeight));
  
  
          int subPanelHeight = 50;
          for (User u : followers) {
              imageUrl = u.getProfileImageURL();
  
              followerName = new JLabel(u.getName());
  
              followerImg = new JLabel(new ImageIcon(imageUrl));
              followerImg.setSize(50, 50);
              followerImg.setPreferredSize(new Dimension(50, subPanelHeight));
              followerImg.setMaximumSize(new Dimension(50, subPanelHeight));
              JPanel thisFollowerPanel = new JPanel(new GridLayout(1, 2));
              thisFollowerPanel.add(followerName);
              thisFollowerPanel.add(followerImg);
  
              followerPanel.add(thisFollowerPanel);
          }
          followerJScrollPane = new JScrollPane(followerPanel,
                  JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, // vertical bar
                  JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
          this.add(followerJScrollPane);
      }
  }

The constructor requires a single argument of type Twitter, and throws an exception of TwitterException. We make a call to method twitter.getFollowers() which returns a List of type twitter4j.User. We iterate through the list of users, and retrieve each user image location by a call to getProfileImageURL(), and the user name by a call to method getName(). Notice we're not actually getting the byte data for the image, but rather a URL which is all we need to create a new ImageIcon.

Class FollowerPanel inherits from JPanel. Because it's nice to both have the image and the display name of our followers, we'll create a JPanel that has a JLabel (containing the name of the follower), and an ImageIcon that will hold the followers display picture.

Each follower JPanel is positioned using the GridLayout nested inside of the previously mentioned JPanel which itself is placed inside of a JScrollBar. The JScrollBar is then added to the instance, which again is a JPanel. The following figure shows the resulting panel.


Figure 3 - The follower panel

The Time Line

The single most important thing you're going to want to do is send tweets! You're probably going to want to see the time line of your updates as well. Note, we're not going to duplicate the time line exactly how it works on Twitter, which mixes updates from everyone that you follow, but rather just display your own. Again, modify to your hearts content.

  import twitter4j.Twitter;
  import twitter4j.TwitterException;
  import twitter4j.Status;
  
  import javax.swing.*;
  import java.awt.event.ActionListener;
  import java.awt.event.ActionEvent;
  import java.awt.*;
  import java.util.*;
  import java.text.SimpleDateFormat;
  
  public class StatusTimeLine implements ActionListener {
      Twitter twitter;
      JPanel timeLinePanel;
      JPanel updatePanel;
      JTextField updateField;
      JButton updateButton;
  
      public StatusTimeLine(Twitter twitter) throws TwitterException {
          this.twitter = twitter;
          timeLinePanel = new JPanel();
          updateField = new JTextField();
  
          updatePanel = new JPanel(new FlowLayout());
          updatePanel.setSize(800, 30);
          updatePanel.setPreferredSize(new Dimension(800, 30));
          updatePanel.setMaximumSize(new Dimension(800, 30));
  
          updateButton = new JButton("Update");
          updateButton.setSize(90, 20);
          updateButton.setPreferredSize(new Dimension(90, 20));
          updateButton.setMaximumSize(new Dimension(90, 20));
          updateButton.addActionListener(this);
  
          updateField = new JTextField();
          updateField.setSize(600, 20);
          updateField.setPreferredSize(new Dimension(600, 20));
          updateField.setMaximumSize(new Dimension(600, 20));
  
          updatePanel.add(updateField);
          updatePanel.add(updateButton);
          timeLinePanel.add(updatePanel);
          updateTimePanel();
  
      }
  
      private void updateTimePanel() throws TwitterException {
          java.util.List<STATUS> statusList = twitter.getUserTimeline();
  
          String statusArr[] = new String[statusList.size()];
          timeLinePanel.setLayout(new BoxLayout(timeLinePanel, BoxLayout.Y_AXIS));
  
          for (int i = 0; i < statusList.size(); i++) {
              Date tweetDate = statusList.get(i).getCreatedAt();
              SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yy HH:mm");
              statusArr[i] = formatter.format(tweetDate) + "-" + statusList.get(i).getText();
          }
  
          JList statusJList = new JList(statusArr);
          statusJList.setFixedCellHeight(20);
          JScrollPane scrollPane = new JScrollPane(statusJList,
                  JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, // vertical bar
                  JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
          timeLinePanel.add(scrollPane);
      }
  
      public JPanel getTimeLinePanel() {
          return timeLinePanel;
      }
  
      public void actionPerformed(ActionEvent event) {
          try {
              twitter.updateStatus(updateField.getText());
              updateTimePanel();
              timeLinePanel.remove(1);
              timeLinePanel.updateUI();
              updateField.setText("");
          } catch (TwitterException exception) {
              JOptionPane.showMessageDialog(null, "An error has occurred while updating.");
              exception.printStackTrace();
          }
      }
  }

At the top of this panel, we'll place a JTextField for entering the tweet, followed by JButton for updating. In a sub panel underneath, is a JList which will display all of the users tweets. Upon pressing the update button either a pop up will appear, giving an error message, or a tweet is sent to the users account, and the JList is updated with the new tweet.





Page 2 of 4



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel