Search code examples
javatwittertwitter4j

ScrollArea and ScrollBar not working


I've been trying to make a java swing based application to retrieve tweets from a user by using twitter4j..the code seems to be working when pretend on the console but not adding ..i've tried adding the scroll area and scroll bar to help with text wrapping and longer texts but it seems to be not working..

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import twitter4j.*;
import twitter4j.auth.AccessToken;
import twitter4j.conf.ConfigurationBuilder;



public class mainTwitter {

    public static void main(String args[])
    {
        List<Status> statuses=null;
        String user;

try{

            ConfigurationBuilder cb = new ConfigurationBuilder();

            cb.setDebugEnabled(true)
              .setOAuthConsumerKey("*************")
              .setOAuthConsumerSecret("*************")
              .setOAuthAccessToken("*************")
              .setOAuthAccessTokenSecret("*************");

            TwitterFactory tf = new TwitterFactory(cb.build());

            Twitter twitter = tf.getInstance();




            if(args.length==1)
            {
                user=args[0];
                statuses=twitter.getUserTimeline(user);
            }
            else
            {
                user = twitter.verifyCredentials().getScreenName();
                statuses = twitter.getUserTimeline();

            }

            System.out.println("Showing "+user+"'s user timeline.");
        /*
            for(Status status: statuses)
            {
                System.out.println("@"+status.getUser().getScreenName()+"-"+status.getText());

            }*/

        }
        catch(Exception e)  
        {
            e.printStackTrace();
        }



        JFrame mainFrame = new JFrame("Twitter Montior");

        mainFrame.setLocationByPlatform(true);
        mainFrame.setLocation(360, 80);


        JPanel tPanel = new JPanel();
        JPanel bPanel = new JPanel();

        mainFrame.setLayout(null);


///////////////////////////////////////////////////////////////////////////////
                    //Top Panel  

        JTextField t1 = new JTextField("Twitter Monitor!!");
            t1.setBackground(Color.yellow);
            t1.setHorizontalAlignment(JTextField.CENTER);
            t1.setEditable(false);

            Font new_f = new Font("Serif", Font.PLAIN, 30);
            t1.setFont(new_f);


        JTextArea userInfo=new JTextArea();
            Font font2 = new Font("Serif", Font.PLAIN, 20);
            userInfo.setFont(font2);
            userInfo.setLineWrap(true);
            userInfo.setWrapStyleWord(true);
            userInfo.setBackground(Color.LIGHT_GRAY);
            userInfo.setEditable(false);
            userInfo.setLineWrap(true);

            JScrollPane scrollArea = new JScrollPane(userInfo);
                scrollArea.setVerticalScrollBarPolicy(
                        JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
                JScrollBar bar = new JScrollBar();

                scrollArea.add(bar);

        tPanel.add(scrollArea);


        JButton retrieve = new JButton("Click");



        tPanel.setLayout(null);
        t1.setBounds(150, 2, 250, 30);
        userInfo.setBounds(0,34,700,260);
        retrieve.setBounds(260, 300, 50, 20);

        tPanel.add(retrieve);
        tPanel.add(t1);
        tPanel.add(userInfo);
///////////////////////////////////////////////////////////////////////////////     

        ///Bottom Panel..







///////////////////////////////////////////////////////////////////////////////     
        tPanel.setBounds(0, 0, 700, 350);
        bPanel.setBounds(0,352,700,248);



        handler h = new handler(statuses, userInfo);
        retrieve.addActionListener(h);

        mainFrame.add(tPanel);
        mainFrame.add(bPanel);




        mainFrame.setSize(700, 600);
        mainFrame.setResizable(false);
        mainFrame.setVisible(true);








    }





}

class handler implements ActionListener
{
    List<Status> statuses2;
    JTextArea t;

    handler(List<Status> statuses1, JTextArea f)
    {

        statuses2=statuses1;
        t=f;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String s = e.getActionCommand();

        if(s=="Click")
        {
            for(Status status: statuses2)
            {
                System.out.println("@"+status.getUser().getScreenName()+"-"+status.getText());
                t.setText("@"+status.getUser().getScreenName()+"-"+status.getText());
                t.setText("\n");

            }
        }



    }





}

Solution

  • I think your main problem is that you use null layout, and you need to choose size and place of every components inside mainFrame and your panels. In my opinion is very bad idea and it is generally not recommended. It would be much simpler to write it with use of BorderLayout for JFrame, and other for your JPanels. But it is up to you.

    It this particular case, in my opinion your scrollArea is not working because:

    1. You add userInfo twice, first to scrollArea, then to tPanel directly. So what you see when you run program, is userInfo in tPanel, because components could be added to only one container.
    2. You use a null layout so it is necessary to setBounds() for every component inside container with such layout settings, but you didn't setBounds() for scrollArea.

    So the quickest way to make it work:

    • delete line tPanel.add(userInfo);,

    • change scrollArea.setBounds(0,34,700,260); for userInfo.setBounds(0,34,700,260); - it is enough to change component it relete to,

    After those changes it should work.

    Aditional comments:

    • you dont't need to add JScrollBar to JScrollPane, it has it by itself,
    • the mainFrame.setLocationByPlatform(true); line is not working and it is not necessary, becouse you setLocation() of mainFrame manually and use null layout,
    • you use userInfo.setLineWrap(true); twice, you can delete one line,