Solved

JScrollBar Listener generates too many events

Posted on 1999-01-03
7
1,001 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

746 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

9 Experts available now in Live!

Get 1:1 Help Now