SHOEISHA iD

※旧SEメンバーシップ会員の方は、同じ登録情報(メールアドレス&パスワード)でログインいただけます

DeveloperZine(デベロッパージン)- エンジニアの意思決定を支える技術情報メディア ProductZine

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

japan.internet.com翻訳記事

Java、Swing、Twitter4jを使ってあなただけのTwitterアプリを作成しよう

ステータスの更新、フォロワーのリストの取得などを簡単に実装

ダイレクトメッセージを送信する

 ご記憶のとおり、Twitterでダイレクトメッセージを送信するには、相手があなたをフォローし、あなたも相手をフォローしている必要があります。

 次に追加するコンポーネントは、右側のパネルに表示するボタンです。これをクリックすると、SendDirectMessageDialogが呼び出されます。今回も匿名の内部クラスを使ってこの処理を実装します。

import twitter4j.Twitter;
  
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;

public class DirectMessagePanel extends JPanel {

    int componentsWidth = 180;

    public DirectMessagePanel(final Twitter twitter) {
        JButton sendDirectMessageButton = new JButton("Send Direct Message");
        sendDirectMessageButton.setSize(new Dimension(componentsWidth, 20));
        sendDirectMessageButton.setPreferredSize(new Dimension(componentsWidth, 20));
        sendDirectMessageButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                SendDirectMessageDialog dialog = new SendDirectMessageDialog(twitter);
                dialog.setVisible(true);
            }
        });

        this.add(sendDirectMessageButton);
    }
}

 SendDirectMessageDialogの定義は少しばかり長くなります。

import twitter4j.Twitter;
import twitter4j.TwitterException;

import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;

public class SendDirectMessageDialog extends JFrame implements ActionListener {

    int componentsWidth = 180;
    Twitter twitter;
    JComboBox followersComboBox;
    JTextField messageTextField;
    JButton sendButton;

    public SendDirectMessageDialog(Twitter twitter) {
        super("Direct Message Dialog");

        this.twitter = twitter;

        setLayout(new FlowLayout());
        setSize(new Dimension(620, 100));
        setPreferredSize(new Dimension(620, 100));
        setLocationRelativeTo(null);

        messageTextField = new JTextField();
        messageTextField.setSize(new Dimension(600, 20));
        messageTextField.setPreferredSize(new Dimension(600, 20));
        sendButton = new JButton("Send");
        sendButton.addActionListener(this);

        followersComboBox = new JComboBox();
        try {
            String[] followerArr = new String[twitter.getFollowers().size()];
            for (int i = 0; i < twitter.getFollowers().size(); i++) {
                followerArr[i] = twitter.getFollowers().get(i).getScreenName();
            }
            followersComboBox = new JComboBox(followerArr);
        } catch (TwitterException e1) {
            e1.printStackTrace();
            JOptionPane.showMessageDialog(this, "Error loading followers list.");
        }

        add(messageTextField);
        add(followersComboBox);
        add(sendButton);
    }

    public void actionPerformed(ActionEvent e) {
        try {
            twitter.sendDirectMessage(followersComboBox.getSelectedItem().toString(), 
                 messageTextField.getText());
            JOptionPane.showMessageDialog(this, "Message Sent Successfully");
            messageTextField.setText("");
        } catch (TwitterException e1) {
            e1.printStackTrace();
            JOptionPane.showMessageDialog(this, "Error sending direct messsage.");
        }
    }
}

 このコードで最初に目に留まるのは、今回はJFrameを継承することです。SendDirectMessageDialogを構成する3つの要素は、メッセージの入力場所となるJTextField、ダイレクトメッセージの宛先として選択できるユーザーを表示するJComboBox、およびメッセージを送信するJButtonです。

図7 ダイレクトメッセージのダイアログ
図7 ダイレクトメッセージのダイアログ

 フォロワーのリストを取得するために、TwitterオブジェクトのgetFollowers()メソッドを呼び出します。このメソッドは、UserオブジェクトのListを返します。forループを使ってこのリストに反復処理を行い、各ユーザー名を文字列配列に挿入します(これは以前と同様です)。このフォロワーの文字列配列を、インスタンス化時にJComboBoxのコンストラクタに渡します。メッセージを実際に送信するために、TwitterオブジェクトのsendDirectMessage()メソッドを使用します。このメソッドには、ユーザー名と送信するメッセージの2つの引数が必要です。ユーザー名は、JComboBoxで選択された項目から取得し、メッセージ文字列はJTextFieldから取得します。処理が正常に進んだ場合に、メッセージ送信を通知するポップアップを表示します。例外が発生した場合は、エラーメッセージを示す別のポップアップを表示します。

図8 ダイレクトメッセージ送信成功のポップアップ
図8 ダイレクトメッセージ送信成功のポップアップ

次のページ
最新トレンドを表示する

この記事は参考になりましたか?

japan.internet.com翻訳記事連載記事一覧

もっと読む

この記事の著者

japan.internet.com(ジャパンインターネットコム)

japan.internet.com は、1999年9月にオープンした、日本初のネットビジネス専門ニュースサイト。月間2億以上のページビューを誇る米国 Jupitermedia Corporation (Nasdaq: JUPM) のニュースサイト internet.comEarthWeb.com からの最新記事を日本語に翻訳して掲載するとともに、日本独自のネットビジネス関連記事やレポートを配信。

※プロフィールは、執筆時点、または直近の記事の寄稿時点での内容です

Cesar Otero(Cesar Otero)

※プロフィールは、執筆時点、または直近の記事の寄稿時点での内容です

この記事は参考になりましたか?

この記事をシェア

CodeZine(コードジン)
https://codezine.jp/article/detail/4409 2009/10/07 14:00

イベント

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

新規会員登録無料のご案内

  • ・全ての過去記事が閲覧できます
  • ・会員限定メルマガを受信できます

メールバックナンバー