twitter.Home.java Source code

Java tutorial

Introduction

Here is the source code for twitter.Home.java

Source

package twitter;

/*
 * 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.
 */

import java.awt.Font;
import java.awt.GridLayout;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import twitter4j.Twitter;
import twitter4j.Status;
import twitter4j.TwitterException;
import javax.swing.JEditorPane;
import javax.swing.JScrollPane;

/**
 *
 * @author baguspw2405
 */
public class Home extends javax.swing.JFrame {

    /**
     * Creates new form Home
     */
    Twitter twitter;
    JLabel statusText;
    JLabel namaUser;
    JLabel usernameUser;
    JLabel tempatUser;
    JLabel waktuUser;
    JLabel tlGue;
    JLabel wktuGue;
    JLabel iniGue;
    JLabel usernameGue;
    JEditorPane statusPane;
    JEditorPane statusPane1;
    login Login = new login();

    public Home(Twitter mtwitter) throws TwitterException {
        this.twitter = mtwitter;
        initComponents();
        //menampilkan username pemakai applikasi
        String statInfo = twitter.getScreenName();
        lblInfo.setText("@" + statInfo);
        //menampilkan Timeline
        java.util.List<Status> statusList = twitter.getHomeTimeline();
        txtFriends.setLayout(new GridLayout(statusList.size(), 1));
        String paneContent = new String();
        statusPane = new JEditorPane();
        statusPane.setContentType("text/html");
        statusPane.setEditable(false);
        for (int i = 0; i < statusList.size(); i++) {
            statusText = new JLabel(String.valueOf(statusList.get(i).getText()));
            namaUser = new JLabel(String.valueOf(statusList.get(i).getUser().getName()));
            tempatUser = new JLabel(String.valueOf(statusList.get(i).getUser().getLocation()));
            usernameUser = new JLabel(String.valueOf(statusList.get(i).getUser().getScreenName()));
            Font newLabelFont = new Font(namaUser.getFont().getName(), Font.PLAIN, namaUser.getFont().getSize());
            statusText.setFont(newLabelFont);
            paneContent = paneContent + namaUser.getText() + " @" + usernameUser.getText() + " , "
                    + tempatUser.getText() + "<br>" + statusText.getText() + "<hr>";
        }
        statusPane.setText(paneContent);
        jTabPane.add("Timeline", statusPane);
        JScrollPane editorScrollPane = new JScrollPane(statusPane, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, // vertical bar policy
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); // horizontal bar policy
        jTabPane.add("Timeline", editorScrollPane);
        txtFriends.updateUI();
        //Profile Panel

        java.util.List<Status> statusGue = twitter.getUserTimeline();
        txtFriends.setLayout(new GridLayout(statusGue.size(), 1));
        String paneContent1 = new String();
        statusPane1 = new JEditorPane();
        statusPane1.setEditable(false);
        for (int j = 0; j < statusGue.size(); j++) {
            tlGue = new JLabel(String.valueOf(statusGue.get(j).getText()));
            wktuGue = new JLabel(String.valueOf(statusGue.get(j).getCreatedAt()));
            iniGue = new JLabel(String.valueOf(statusGue.get(j).getUser().getName()));
            usernameGue = new JLabel(String.valueOf(statusGue.get(j).getUser().getScreenName()));
            Font newLabelFont1 = new Font(iniGue.getFont().getName(), Font.PLAIN, iniGue.getFont().getSize());
            tlGue.setFont(newLabelFont1);
            paneContent1 = paneContent1 + iniGue.getText() + " @" + usernameGue.getText() + " ," + wktuGue.getText()
                    + "\n" + tlGue.getText()
                    + "\n------------------------------------------------------------------------"
                    + "------------------------------------------------------------------------------------\n";
        }
        statusPane1.setText(paneContent1);
        jTabPane.add("Timeline Saya", statusPane1);
        JScrollPane editorScrollPane1 = new JScrollPane(statusPane1, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, // vertical bar policy
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        jTabPane.add("Timeline Saya", editorScrollPane1);
        txtFriends.updateUI();
        /*for (Status status : statusList) {
        txtFriends.("\n"+status.getUser().getLocation()+
                "\n@" + status.getUser().getScreenName() + "\n" + status.getText()+"\n");
        }*/

    }

    /**
     * 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() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtUserStatus = new javax.swing.JTextField();
        btnUpdate = new javax.swing.JButton();
        lblUserStatus = new javax.swing.JLabel();
        jTabPane = new javax.swing.JTabbedPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtFriends = new javax.swing.JPanel();
        btnInfo = new javax.swing.JButton();
        btnLogOut = new javax.swing.JButton();
        lblInfo = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jPanel2.setBackground(new java.awt.Color(0, 153, 204));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(jPanel2Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 0, Short.MAX_VALUE));
        jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 15, Short.MAX_VALUE));

        jLabel1.setBackground(new java.awt.Color(0, 153, 204));
        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 153, 204));
        jLabel1.setText("Wellcome to Twitter");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel2.setText("Buat Status mu Bro :");

        txtUserStatus.setForeground(new java.awt.Color(0, 153, 255));
        txtUserStatus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtUserStatusActionPerformed(evt);
            }
        });

        btnUpdate.setBackground(new java.awt.Color(0, 153, 255));
        btnUpdate.setForeground(new java.awt.Color(0, 102, 102));
        btnUpdate.setText("Kirim");
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        lblUserStatus.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N

        jTabPane.setBackground(new java.awt.Color(255, 255, 255));

        jScrollPane1.setBackground(new java.awt.Color(0, 153, 204));

        txtFriends.setBackground(new java.awt.Color(0, 153, 204));

        javax.swing.GroupLayout txtFriendsLayout = new javax.swing.GroupLayout(txtFriends);
        txtFriends.setLayout(txtFriendsLayout);
        txtFriendsLayout.setHorizontalGroup(txtFriendsLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 1136, Short.MAX_VALUE));
        txtFriendsLayout.setVerticalGroup(txtFriendsLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 480, Short.MAX_VALUE));

        jScrollPane1.setViewportView(txtFriends);

        jTabPane.addTab("Home", jScrollPane1);

        btnInfo.setBackground(new java.awt.Color(0, 153, 255));
        btnInfo.setForeground(new java.awt.Color(0, 51, 51));
        btnInfo.setText("Info Apps");
        btnInfo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnInfoActionPerformed(evt);
            }
        });

        btnLogOut.setBackground(new java.awt.Color(0, 153, 204));
        btnLogOut.setForeground(new java.awt.Color(0, 51, 51));
        btnLogOut.setText("LogOut");
        btnLogOut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLogOutActionPerformed(evt);
            }
        });

        lblInfo.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        lblInfo.setForeground(new java.awt.Color(0, 153, 204));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                        Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap()
                                        .addComponent(jLabel1).addGap(4, 4, 4)
                                        .addComponent(lblInfo, javax.swing.GroupLayout.PREFERRED_SIZE, 265,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(btnInfo)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(btnLogOut))
                                .addGroup(jPanel1Layout.createSequentialGroup().addGap(22, 22, 22)
                                        .addGroup(jPanel1Layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                                        false)
                                                .addComponent(jTabPane, javax.swing.GroupLayout.DEFAULT_SIZE, 655,
                                                        Short.MAX_VALUE)
                                                .addComponent(btnUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 97,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(txtUserStatus)))
                                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap()
                                        .addComponent(jLabel2)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(lblUserStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 532,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel1)
                                .addGroup(jPanel1Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(btnInfo, javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(lblInfo, javax.swing.GroupLayout.Alignment.TRAILING,
                                                javax.swing.GroupLayout.PREFERRED_SIZE, 23,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(btnLogOut)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel2).addComponent(lblUserStatus,
                                        javax.swing.GroupLayout.PREFERRED_SIZE, 17,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtUserStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 69,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 34,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTabPane, javax.swing.GroupLayout.DEFAULT_SIZE, 506, Short.MAX_VALUE)
                        .addContainerGap()));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jPanel1,
                        javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                javax.swing.GroupLayout.PREFERRED_SIZE));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
        try {
            //untuk mengupdate status
            Status status = twitter.updateStatus(txtUserStatus.getText());
            lblUserStatus.setText("" + status.getText() + ": " + status.getCreatedAt());
            JOptionPane.showMessageDialog(this, "Status Sudah diUpdate Bro", "Info Update",
                    JOptionPane.INFORMATION_MESSAGE);
            txtUserStatus.setText("");

        } catch (TwitterException ex) {
            Logger.getLogger(Home.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_btnUpdateActionPerformed

    private void btnInfoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnInfoActionPerformed
        //untuk memanggil frame InformasiApplikasi
        InfomarsiApplikasi inf = new InfomarsiApplikasi();
        inf.setAlwaysOnTop(true);
        inf.setVisible(true);
    }//GEN-LAST:event_btnInfoActionPerformed

    private void txtUserStatusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtUserStatusActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtUserStatusActionPerformed

    private void btnLogOutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLogOutActionPerformed
        //untuk memanggil frame login (Logout)
        login Log = new login();
        Log.setVisible(true);
        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_btnLogOutActionPerformed

    /**
     * @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(Home.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Home.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Home.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Home.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                //new Home().setVisible(true);

            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnInfo;
    private javax.swing.JButton btnLogOut;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabPane;
    private javax.swing.JLabel lblInfo;
    private javax.swing.JLabel lblUserStatus;
    private javax.swing.JPanel txtFriends;
    private javax.swing.JTextField txtUserStatus;
    // End of variables declaration//GEN-END:variables

}