Results 1 to 10 of 10
  1. #1
    Dinesh_rockz is offline Member
    Join Date
    Jan 2011
    Location
    India
    Posts
    22
    Rep Power
    0

    Thumbs up Autoscrolling in JTextArea

    hi to all..! i want to design the about screen for my java application

    how to make autoscrolling in swing Component:):)

    and idea.. for my problem:cool:
    Last edited by Dinesh_rockz; 02-21-2011 at 03:48 AM. Reason: [Solved]

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,993
    Rep Power
    9

    Default

    What does autoscrolling mean? What does google tell you? Are you just looking for a JScrollPane?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Dinesh_rockz is offline Member
    Join Date
    Jan 2011
    Location
    India
    Posts
    22
    Rep Power
    0

    Default

    sorry for my late reply

    i am just looking for automatically scrolling some components ..

    just like firefox credits..

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  5. #5
    Dinesh_rockz is offline Member
    Join Date
    Jan 2011
    Location
    India
    Posts
    22
    Rep Power
    0

    Default

    pls give some examples for BoundedRangeModel interface...!
    Last edited by Dinesh_rockz; 02-19-2011 at 04:09 AM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If you get the model from the JScrollBar, it will be a BoundedRangeModel object, and the methods available to this object are listed in its API including the all-important setValue(int value) method. Please have a look and try to experiment with some code yourself. If you get stuck, post what you've got.

  7. #7
    Dinesh_rockz is offline Member
    Join Date
    Jan 2011
    Location
    India
    Posts
    22
    Rep Power
    0

    Default

    i got the BoundedRangeModel .i am also set value for the model by using setValue() method .. but it's not scroll... here my code


    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    public class autoscr extends JDialog implements ActionListener
     {
    	JTextArea txt;
    	JScrollPane jscr;
    	Timer my_timer;
    	StringBuffer msg=new StringBuffer("This Program for Auto Scroll demo\n\nThis Program for Auto Scroll demos\n\n using    JTextArea,Jscrollpane\n\n\n\n\n\n\n\n");
    	BoundedRangeModel brm;
    
        public autoscr() 
    
        {
        	super();
        	setTitle("Auto ScrollDemo...");
        	setLayout(new BorderLayout());
    
        	msg.append("\n\n\n\n\n\n\n\n\n\n\nHi This is for a test purpose pls check it..");
        	txt=new JTextArea(msg.toString());
       
        	jscr=new JScrollPane(txt,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    
        	add(jscr,BorderLayout.CENTER);
    
        	my_timer=new Timer(1000,this);
            my_timer.start();
    
            brm=jscr.createHorizontalScrollBar().getModel(); 
            brm.setMaximum(250);
    
        	//System.out.println(brm.getValueIsAdjusting());
    
        	setSize(500,500);
        	setVisible(true);
        	addWindowListener(new java.awt.event.WindowAdapter() {
                        public void windowClosing(java.awt.event.WindowEvent e) {
                        	my_timer.stop();
                            System.exit(0);
                        }
                    });
        }
    
        
    
        
    
        public void actionPerformed(ActionEvent e)
    
        {
    
        	System.out.println ("Timer Status: "+my_timer.isRunning());
             System.out.println ("Model Value: "+brm.getValue());
            brm.setValue(brm.getValue()+10);
    
        }
    
        public static void main (String[] args) {
        	autoscr obj=new autoscr();
        	obj.setVisible(true);
    
    }
    }
    anything wrong in my code..?

  8. #8
    riddhishah28 is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default

    Whatever u want to make scroll, put it into scroll pane...
    and set the visible range.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Since you've provided an example, I'll do this same. The key is to get the BoundedRangeModel from the JScrollBar that is actually showing on the screen. Then you can get the value from the model, get the maximum from the model and set the value. For example:

    Java Code:
    import java.awt.Dimension;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.Scanner;
    
    import javax.swing.BoundedRangeModel;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.Timer;
    
    @SuppressWarnings("serial")
    public class GettysburgAutoScroll2 extends JPanel {
       private static final String GETTYSBURG_TEXT = "http://history.eserver.org/" +
       		"gettysburg-address.txt";
       private static final String LAST_WORD = "earth.";
       private static final int GETTY_REPEAT = 3;
       private static final Dimension VIEWPORT_SIZE = new Dimension(500, 600);
       private static final int TIMER_DELAY = 30;
       private static final int SCROLL_AMOUNT = 2;
       
       private JTextArea textArea;
       private JScrollPane scrollPane;
       private BoundedRangeModel barModel;  // Variable to hold the bounded range model
       
       public GettysburgAutoScroll2() {
          StringBuilder gettysburgTextStrBlder = new StringBuilder(); 
          Scanner gettyScanner = null;
          // Get Gettysburg address text from internet and put in StringBuilder
          try {
             URL gettyUrl = new URL(GETTYSBURG_TEXT);
             URLConnection gettyUrlConn = gettyUrl.openConnection();
             gettyScanner = new Scanner(gettyUrlConn.getInputStream());
             String line = "";
             while (gettyScanner.hasNextLine() && !line.contains(LAST_WORD)) {
                line = gettyScanner.nextLine();
                gettysburgTextStrBlder.append(line + "\n");
             }
          } catch (MalformedURLException e) {
             e.printStackTrace();
             System.exit(-1);
          } catch (IOException e) {
             e.printStackTrace();
             System.exit(-1);
          } finally {
             if (gettyScanner != null) {
                gettyScanner.close();
             }
          }
          gettysburgTextStrBlder.append("\n\n\n"); 
          
          // add a couple copies of the address to the StringBuilder
          String text = gettysburgTextStrBlder.toString();
          for (int i = 0; i < GETTY_REPEAT; i++) {
             gettysburgTextStrBlder.append(text);
          }
          
          // put Gettysburg address into JTextArea
          textArea = new JTextArea(gettysburgTextStrBlder.toString());
          textArea.setMargin(new Insets(10, 10, 10, 10));
          
          scrollPane = new JScrollPane(textArea);
          scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
          scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
          
          // constrain the size of the scroll pane's view port so it should be smaller
          // than the JTextArea.
          scrollPane.getViewport().setPreferredSize(VIEWPORT_SIZE);
          
          add(scrollPane);
          
          // *** extract the BoundedRangeModel from the vertical scroll bar
          barModel = scrollPane.getVerticalScrollBar().getModel();
          
          new Timer(TIMER_DELAY, new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                timerActionPerformed(e);
             }
          }).start(); 
       }
    
       private void timerActionPerformed(ActionEvent e) {
          int newModelValue = barModel.getValue();  // Get the model's curent value
          if (newModelValue < barModel.getMaximum() - barModel.getExtent()) {  // if not at max
             newModelValue += SCROLL_AMOUNT; // add something to it
             barModel.setValue(newModelValue);  // and change the model's value
          } else {
             ((Timer)e.getSource()).stop(); // else if we've reached the max, stop timer
          }
       }
    
       private static void createAndShowUI() {
          JFrame frame = new JFrame("AutoScroll2");
          frame.getContentPane().add(new GettysburgAutoScroll2());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }

    Gettysburg Address text from: gettysburg-address.txt
    Copyright info:
    Prepared by Gerald Murphy (The Cleveland Free-Net - aa300)
    Distributed by the Cybercasting Services Division of the
    National Public Telecomputing Network (NPTN).

    Permission is hereby granted to download, reprint, and/or otherwise
    redistribute this file, provided appropriate point of origin
    credit is given to the preparer(s) and the National Public
    Telecomputing Network.
    Last edited by Fubarable; 02-19-2011 at 02:32 PM.

  10. #10
    Dinesh_rockz is offline Member
    Join Date
    Jan 2011
    Location
    India
    Posts
    22
    Rep Power
    0

Similar Threads

  1. JTextArea
    By larry_d1990 in forum Advanced Java
    Replies: 4
    Last Post: 01-11-2011, 05:44 PM
  2. JTextArea
    By weikang in forum AWT / Swing
    Replies: 1
    Last Post: 12-01-2010, 04:51 AM
  3. JTextArea on PopUp -JTextArea isn't editable
    By Richy76 in forum AWT / Swing
    Replies: 3
    Last Post: 02-01-2010, 07:51 PM
  4. About JTEXTAREA
    By makpandian in forum AWT / Swing
    Replies: 4
    Last Post: 03-19-2009, 06:53 AM
  5. JTextArea
    By saytri in forum New To Java
    Replies: 0
    Last Post: 01-13-2008, 01:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •