?
Solved

JScrollBar Listener generates too many events

Posted on 1999-01-03
7
Medium Priority
?
1,010 Views
Last Modified: 2013-11-23
I'm developing an application that displays on a JEditoPane the contents of a file. Since the file is too large, I display it in blocks of 5000 chars each.
I use ScrollPane to scroll the text. The final goal is to add a listener to the JScrollBar and when the user scrolls down to the end of the block - the next block of text will be displayed.
The problem - When I add 'AdjustmentListener' to the JScrollBar and the user gets to the end of the scrolled text, the event is cought, the code is executed but when the JScrollBar returns to the start of the next block it is cought as another event and the previous block is displayed. As a result I can display only the first block of text.
The code is too long to be posted, so here is a pseudo :

initialize JScrollPane ;
initialize JEditorPane ;
get JViewPort ;
add JEditorPane to JViewPort ;
add JViewPort to JFrame ;

JScrollBar.addAdjustmentListener ...

public void adjustmentValueChanged(AdjustmentEvent e) {
  if (JScrollBar.getValue() == JScrollBar.getMaximum() )
     showNextBlock() ;
  if (JScrollBar.getValue() == JScrollBar.getMinimum() )
     showPreviousBlock() ;
}

When I tried to remove the listener, execute the code and then add the listener again - it still didn't work.

Can anyone help ?
0
Comment
Question by:yoni99
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 1

Author Comment

by:yoni99
ID: 1230333
Maybe a thread will help ? ? ?
0
 
LVL 1

Author Comment

by:yoni99
ID: 1230334
Edited text of question
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1230335
maybe just a boolean

boolean alreadyAdjusting = false;

public void adjustmentValueChanged(AdjustmentEvent e) {
      if (alreadyAdjusting) return;
      alreadyAdjusting = true;
      if (JScrollBar.getValue() == JScrollBar.getMaximum() )
         showNextBlock() ;
      if (JScrollBar.getValue() == JScrollBar.getMinimum() )
         showPreviousBlock() ;
      alreadyAdjusting = false;
    }
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:yoni99
ID: 1230336
Sorry,
It's not that simple - the method showNext..() uses setText() to update the JEditorPane.
I think JAVA is using a thread and JScrollBar updates before the text is actually displayed, when the update is completed the JScrollBar is catching another event and so on...  
This is a part of the code - I know it's a mess, I'm still working on it :

class Viewer extends JFrame {
      
      private JFrame frame ;
      private JPanel p ;
      private JEditorPane editorPane ;
      private JScrollPane scroll ;
      private JViewport vp ;
      private String parsed ;
      private ScrollBarListener listener ;
      private boolean alreadyAdjusting = false ;
      private int currentBlock = 1 ;
      private final int BLOCK_SIZE = 2500 ;

      
      // Viewer constructor.
      //******************** /**/
      public Viewer () {
            
            setSize(500,300) ;
            setVisible(true) ;
            addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent event) { System.exit(0); }
      });
      
            
      }
      
      // Display the parsed text on screen.
      //*********************************** /**/
      
      public void setText (String text) {
      
            
            parsed = text ;
            
            p = new JPanel () ;
            p.setPreferredSize (new Dimension (500,300)) ;
            p.setLayout (new BorderLayout()) ;
            getAccessibleContext().setAccessibleName ("HTML Viewer") ;
            getAccessibleContext().setAccessibleDescription ("Viewer for HTML files") ;
            
            String tmp ;
            
            if (text == null)
                  text = "Null Text" ;
            
            if (text.length() < BLOCK_SIZE) {
                  tmp = setLines(text) ;
            }
            
            editorPane = new JEditorPane ("text/html",tmp) ;
            editorPane.setPreferredSize(new Dimension (500,300)) ;
            scroll = new JScrollPane (JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED) ;
            vp = scroll.getViewport() ;
            vp.add(editorPane) ;
            editorPane.setEditable (false) ;
            vp.setBackingStoreEnabled (true) ;
            
            p.add(scroll, BorderLayout.CENTER) ;
            

/* Here I add the JScrollBar listener */
listener = new ScrollBarListener() ;
scroll.getVerticalScrollBar().addAdjustmentListener(listener) ;
            
            getContentPane().add(p) ;
            show() ;
            
      }
      
      public void blockUp() {
            
            // setting String change
            editorPane.setText(change) ;
      }
      
      
      
      public void blockDown() {
      
                           // setting String change
                   editorPane.setText(change) ;
            
      }
            
      class ScrollBarListener implements AdjustmentListener {
            
            /* This is an inner class in the main class */
    public void adjustmentValueChanged(AdjustmentEvent e) {
        
            
                  if (scroll.getVerticalScrollBar().getValue() == scroll.getVerticalScrollBar().getMaximum()) {
                        //True if the scrollbar is at the bottom.
        blockDown() ; // This method is defined in the main class
      }
      else if (scroll.getVerticalScrollBar().getValue() == scroll.getVerticalScrollBar().getMinimum()) {
        //True if scrollbar is at the top.
        blockUp() ; //Defined in the main class.
      }
          
                
    }
  }


      
} // End of class
      
      

0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1230337
/*
this is very quick and dirty example, but it works for me.
I've tested it with JDK1.1.7A, Swing 1.0.2 on WinNT
but you shouldn't have any problems even with JDK1.2 (you have to change the swing package names of course)

just compile it and start it.
if you have any problems, please post the question here
*/


import com.sun.java.swing.*;
import com.sun.java.swing.text.*;

import java.awt.event.*;
import java.awt.*;

class Viewer extends JFrame
 {

        private JFrame frame ;
        private JPanel p ;
        private JEditorPane editorPane ;
        private JScrollPane scroll ;
        private JViewport vp ;
        private String parsed ;
        private ScrollBarListener listener ;
        private boolean alreadyAdjusting = false ;
        private int currentBlock = 1 ;
        private final int BLOCK_SIZE = 2500 ;


boolean isAdjusting = false;
int block = 0;

        // Viewer constructor.
        //******************** /**/
        public Viewer () {

        setSize(100,300) ;
        setVisible(true) ;
        addWindowListener(new WindowAdapter() {
             public void windowClosing(WindowEvent event) { System.exit(0); }
              });


        }

        // Display the parsed text on screen.
        //*********************************** /**/

        public void setText (String text) {


        parsed = text ;

        p = new JPanel () ;
        p.setPreferredSize (new Dimension (500,300)) ;
        p.setLayout (new BorderLayout()) ;
        getAccessibleContext().setAccessibleName ("HTML Viewer") ;
        getAccessibleContext().setAccessibleDescription ("Viewer for HTML files") ;

        String tmp ;

        if (text == null)
        text = "Null Text" ;
/*
        if (text.length() < BLOCK_SIZE) {
        tmp = setLines(text) ;
        }
*/
isAdjusting = true;
block = 1;
            String bl = "block " + block + " ";
            tmp = "";
            for (int i = 0; i <100; i ++) tmp = tmp + bl + "line " + i + "\n";


        editorPane = new JEditorPane () ;
//        editorPane.setPreferredSize(new Dimension (100,100)) ;
            editorPane.setText(tmp);
        scroll = new JScrollPane
        (JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED) ;
        vp = scroll.getViewport() ;
        vp.add(editorPane) ;
        editorPane.setEditable (false) ;
        vp.setBackingStoreEnabled (true) ;

        p.add(scroll, BorderLayout.CENTER) ;


        /* Here I add the JScrollBar listener */
        listener = new ScrollBarListener() ;
        scroll.getVerticalScrollBar().addAdjustmentListener(listener) ;

        getContentPane().add(p) ;
        show() ;
            isAdjusting = false;

        }

        public void blockUp() {
                  block--;
                  String bl = "block " + block + " ";
                  String change = "";
                  for (int i = 0; i <100; i ++) change = change + bl + "line " + i + "\n";
                // setting String change
              editorPane.setText(change) ;
        }


        public void blockDown() {
                        block++;
                        String bl = "block " + block + " ";
                        String change = "";
                        for (int i = 0; i <100; i ++) change = change + bl + "line " + i + "\n";

                                   // setting String change
                editorPane.setText(change) ;
                        editorPane.setCaretPosition(1);

        }

        class ScrollBarListener implements AdjustmentListener {

        /* This is an inner class in the main class */
            public void adjustmentValueChanged(AdjustmentEvent e) {

            if (isAdjusting) return;
            isAdjusting = true;

            BoundedRangeModel model = scroll.getVerticalScrollBar().getModel();
/*
            System.out.println("minimum " + model.getMinimum());
            System.out.println("maximum " + model.getMaximum());
            System.out.println("value " + model.getValue());
            System.out.println("extent " + model.getExtent());
*/                               
        if (model.getValue() + model.getExtent() == model.getMaximum()) {
        //True if the scrollbar is at the bottom.
                        System.out.println("block down");
                blockDown() ; // This method is defined in the main class

              }
              else if (model.getValue() == model.getMinimum()) {
                //True if scrollbar is at the top.
//                        Thread.currentThread().dumpStack();
                        System.out.println("block up");
                blockUp() ; //Defined in the main class.
              }
             
            isAdjusting = false;
                   
            }

          }


            public static void main(String args[])
            {
                Viewer v = new Viewer();
                 v.setText("");
            };
        } // End of class
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1230338
do you have more problems ?
0
 
LVL 16

Accepted Solution

by:
heyhey_ earned 400 total points
ID: 1230339
I suppose that I can post an empty anser ...
see comments below for sample working code.

0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
Suggested Courses
Course of the Month11 days, 22 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question