TimeLine.java Source code

Java tutorial

Introduction

Here is the source code for TimeLine.java

Source

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Kelompok twitTIF
 * Muhamad Mashudi Ardi Winata (141240000229)
 * Ika A ()
 * Faidl Fahruddin Nawa ()
 */
public class TimeLine extends javax.swing.JFrame {

    /**
     * Creates new form TimeLine
     * @throws twitter4j.TwitterException
     */

    public TimeLine() throws TwitterException {
        initComponents();

        // Image Icon
        setIconImage(new ImageIcon(getClass().getResource("/app.png")).getImage());

        // maximize window
        this.setExtendedState(TimeLine.MAXIMIZED_BOTH);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        txtTimeline = new javax.swing.JTextArea();
        btTentang = new javax.swing.JButton();
        txStatus = new javax.swing.JTextField();
        btTwit = new javax.swing.JButton();
        btTimeline = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        txtStatus = new javax.swing.JTextArea();
        btReloadTwit = new javax.swing.JButton();
        btSearch = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("TweetTIF");

        txtTimeline.setEditable(false);
        txtTimeline.setColumns(20);
        txtTimeline.setLineWrap(true);
        txtTimeline.setRows(5);
        jScrollPane1.setViewportView(txtTimeline);

        btTentang.setText("Tentang");
        btTentang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btTentangActionPerformed(evt);
            }
        });

        btTwit.setText("Tweet");
        btTwit.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btTwitMouseClicked(evt);
            }
        });
        btTwit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btTwitActionPerformed(evt);
            }
        });

        btTimeline.setText("Reload Timeline");
        btTimeline.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btTimelineActionPerformed(evt);
            }
        });

        txtStatus.setEditable(false);
        txtStatus.setColumns(20);
        txtStatus.setLineWrap(true);
        txtStatus.setRows(5);
        jScrollPane3.setViewportView(txtStatus);

        btReloadTwit.setText("Reload Tweet");
        btReloadTwit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btReloadTwitActionPerformed(evt);
            }
        });

        btSearch.setText("Search");
        btSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSearchActionPerformed(evt);
            }
        });

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/app.png"))); // NOI18N

        jLabel2.setFont(new java.awt.Font("Dialog", 1, 36)); // NOI18N
        jLabel2.setText("TweetTIF");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jScrollPane1)
                                .addContainerGap())
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                layout.createSequentialGroup().addGap(297, 297, 297)
                                        .addComponent(btTentang, javax.swing.GroupLayout.DEFAULT_SIZE, 587,
                                                Short.MAX_VALUE)
                                        .addGap(259, 259, 259))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                layout.createSequentialGroup().addContainerGap().addComponent(jLabel1)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 192,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jScrollPane3)
                                .addContainerGap())
                        .addGroup(layout.createSequentialGroup().addContainerGap()
                                .addComponent(txStatus, javax.swing.GroupLayout.DEFAULT_SIZE, 445, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btTwit).addGap(200, 200, 200).addComponent(btSearch)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btReloadTwit)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btTimeline).addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(17, 17, 17)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(txStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 30,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(btTwit).addComponent(btTimeline).addComponent(btReloadTwit)
                                .addComponent(btSearch))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 131,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE)
                        .addGap(18, 18, 18).addComponent(btTentang).addGap(17, 17, 17)));

        setSize(new java.awt.Dimension(1153, 732));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void btTentangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btTentangActionPerformed
        new TentangTweetTIF(this, rootPaneCheckingEnabled).setVisible(true);
    }//GEN-LAST:event_btTentangActionPerformed

    private void btTwitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btTwitActionPerformed
        // Tombol update status, show setelah update
        txtStatus.setText("");

        TwitterFactory tf = new TwitterFactory();
        Twitter twitter = tf.getInstance();

        try {
            Status status = twitter.updateStatus(txStatus.getText());
            JOptionPane.showMessageDialog(rootPane, "Twit \n [ " + status.getText() + " ]\nTerkirim!");
        } catch (TwitterException ex) {
            JOptionPane.showMessageDialog(rootPane, "Tidak bisa mengirim : " + ex.getMessage());
            Logger.getLogger(TimeLine.class.getName()).log(Level.SEVERE, null, ex);
        }

        List<Status> statuses = null;
        try {
            statuses = twitter.getUserTimeline();

            statuses.stream().forEach((status) -> {
                txtStatus.append(status.getUser().getName() + " : " + status.getText() + " - "
                        + status.getCreatedAt() + " \n Via : " + status.getSource() + "\n\n");
            });

            txStatus.setText(""); //reload field
        } catch (TwitterException te) {
            JOptionPane.showMessageDialog(rootPane, "Failed to Show Status!" + te.getMessage());
        }
    }//GEN-LAST:event_btTwitActionPerformed

    private void btTimelineActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btTimelineActionPerformed
        // TODO add your handling code here:
        txtTimeline.setText("");

        TwitterFactory tf = new TwitterFactory();
        Twitter twitter = tf.getInstance();

        List<Status> statuses = null;
        try {
            statuses = twitter.getHomeTimeline();

            statuses.stream().forEach((status) -> {
                txtTimeline.append(status.getUser().getName() + " : " + status.getText() + " - "
                        + status.getCreatedAt() + " \n Via : " + status.getSource() + "\n\n");
            });
        } catch (TwitterException ex) {
            Logger.getLogger(TimeLine.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(rootPane, "Failed to Show Status!" + ex.getMessage());
        }
    }//GEN-LAST:event_btTimelineActionPerformed

    private void btReloadTwitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btReloadTwitActionPerformed
        // Tombol reload twit saya
        txtStatus.setText("");

        TwitterFactory tf = new TwitterFactory();
        Twitter twitter = tf.getInstance();

        List<Status> statuses = null;
        try {
            statuses = twitter.getUserTimeline();

            statuses.stream().forEach((status) -> {
                txtStatus.append(status.getUser().getName() + " : " + status.getText() + " - "
                        + status.getCreatedAt() + " \n Via : " + status.getSource() + "\n\n");
            });

            txStatus.setText(""); //reload field
        } catch (TwitterException te) {
            JOptionPane.showMessageDialog(rootPane, "Failed to Show Status!" + te.getMessage());
        }
    }//GEN-LAST:event_btReloadTwitActionPerformed

    private void btTwitMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btTwitMouseClicked
        // TODO add your handling code here:

    }//GEN-LAST:event_btTwitMouseClicked

    private void btSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSearchActionPerformed
        new Pencarian(this, rootPaneCheckingEnabled).setVisible(true);
    }//GEN-LAST:event_btSearchActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TimeLine.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TimeLine.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TimeLine.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TimeLine.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new TimeLine().setVisible(true);
                } catch (TwitterException ex) {
                    Logger.getLogger(TimeLine.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });

    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btReloadTwit;
    private javax.swing.JButton btSearch;
    private javax.swing.JButton btTentang;
    private javax.swing.JButton btTimeline;
    private javax.swing.JButton btTwit;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextField txStatus;
    private javax.swing.JTextArea txtStatus;
    private javax.swing.JTextArea txtTimeline;
    // End of variables declaration//GEN-END:variables

}