Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

JScrollBar Listener generates too many events

Posted on 1999-01-03
7
Medium Priority
?
1,011 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This video teaches viewers about errors in exception handling.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
Suggested Courses

597 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