Solved

JScrollBar Listener generates too many events

Posted on 1999-01-03
7
1,002 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 100 total points
ID: 1230339
I suppose that I can post an empty anser ...
see comments below for sample working code.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now