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


