We help IT Professionals succeed at work.

Unable to track Unit_Increment for JScrollbar in AdjustmentValueChanged

harishalwala
harishalwala asked
on
We are writing a swing program that has a JScrollbar set for textPane. We would like to adjust the content of the textpane based on the user input on jscrollbar. We would like to capture the mouse click on the arrow down & arrow up so that we can scroll up or down relative content.
Problem:
We are using Adjustmentlistener and tracking the user input using AdjustmentValueChanged().
following is snippet.
We always get into AdjustmentEvent.TRACK, not to any other case.

Please provide the solution

switch (e.getAdjustmentType())
		{
		case AdjustmentEvent.TRACK:
			type = "Track";
			break;
		case AdjustmentEvent.BLOCK_DECREMENT:
			type = "Block Decrement";
			break;
		case AdjustmentEvent.BLOCK_INCREMENT:
			type = "Block Increment";
			break;
		case AdjustmentEvent.UNIT_DECREMENT:
			type = "Unit Decrement";
			break;
		case AdjustmentEvent.UNIT_INCREMENT:
			type = "Unit Increment";
			break;
		}

Open in new window

Comment
Watch Question

Awarded 2011
Awarded 2011

Commented:
Well, I also tried using this modified example form java2s
web site
  
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;

import javax.swing.*;

public class AdjustmentTest {
  public static void main(String args[]) {
    JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();
    Icon icon = new ImageIcon("java2s.gif");
    JTextArea b = new JTextArea(30,40);
    JScrollPane pane = new JScrollPane(b);
    AdjustmentListener hListener = new AdjustmentListener() {
      public void adjustmentValueChanged(AdjustmentEvent e) {
        System.out.println("Horizontal: ");
        dumpInfo(e);
      }
    };
    JScrollBar hBar = pane.getHorizontalScrollBar();
    hBar.addAdjustmentListener(hListener);
    AdjustmentListener vListener = new AdjustmentListener() {
      public void adjustmentValueChanged(AdjustmentEvent e) {
        System.out.println("Vertical: ");
        dumpInfo(e);
      }
    };
    JScrollBar vBar = pane.getVerticalScrollBar();
    vBar.addAdjustmentListener(vListener);
    contentPane.add(pane, BorderLayout.CENTER);
    frame.setSize(300, 200);
    frame.show();
  }

  private static void dumpInfo(AdjustmentEvent e) {
    System.out.println("\tValue: " + e.getValue());
    String type = null;

      System.out.println(e.getAdjustmentType());
    switch (e.getAdjustmentType()) {
   // case AdjustmentEvent.TRACK:
    //  type = "Track";
    //  break;
    case AdjustmentEvent.BLOCK_DECREMENT:
      type = "Block Decrement";
      break;
    case AdjustmentEvent.BLOCK_INCREMENT:
      type = "Block Increment";
      break;
    case AdjustmentEvent.UNIT_DECREMENT:
      type = "Unit Decrement";
      break;
    case AdjustmentEvent.UNIT_INCREMENT:
      type = "Unit Increment";
      break;
    }
    System.out.println("\tType: " + type);
  }
}

Open in new window



I printed the value of AdjustementEvent type

It looks like for all events - dragging or clicking
in any place it generates adjustmentevent  type equal 5
which should correspond to TRACK according to API
(In my environamnet it does not recofnize AdjustementEvet.TRACK filed - this is additional issue, but this is less important,
then generating the saem type for all events)

Don't know how to explain it.
But it does increment scrollbar when you click on arrow.

I guess TRACK should be when you drag it
and UNIT INCREMNET when you click the arow  and block increment when between the scrolls,
- but it always  shows type 5 (corresponding to TRACK)

Strange...




Awarded 2011
Awarded 2011
Commented:
Awarded 2011
Awarded 2011

Commented:
It looks like it is still a bug in Java 1.6
Which java are you using ?
maybe they fixed it in 7 ?
Awarded 2011
Awarded 2011

Commented:
Probably the way to go is to create your own buttons which will
scroll to the amount you want.

Alternatively, maybe you can track the location where
user clicked on the scrollbar somehow with MouseListener
But that would be rather tough
Again, I fixed the issue my self. When scroll pane is clicked, it always comes to TRACK.
case AdjustmentEvent.TRACK:
			type = "Track";
			break;

Open in new window

It never comes to another "case" in code provided in first comment.

We have written, our own logic to fix the issue

Author

Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for harishalwala's comment http:/Q_27427009.html#37175101

for the following reason:

Fixed by my seflt
Awarded 2011
Awarded 2011

Commented:

I don't know, how the issue was really finally resolved (I made a couple of suggestions of how to do it above), but I believe realization
that this is a documented Java bug in this situation was an important step in the process towards resolution
of this issue. Perhaps the author may choose to consider it once again.  
South ModModerator
SILVER EXPERT

Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for harishalwala's comment http:/Q_27427009.html#37175101

for the following reason:

All,<br />&nbsp; <br />Following an 'Objection' by for_yan (at <a href="http://www.experts-exchange.com/Q_27460960.html">http://www.experts-exchang<wbr />e.com/Q_27<wbr />460960.htm<wbr />l</a>) to the intended closure of this question, it has been reviewed by at least one Moderator and is being closed as recommended by the Expert.<br />&nbsp;<br />At this point I am going to re-start the auto-close procedure.<br />&nbsp;<br />Thank you,<br />&nbsp;<br />SouthMod<br />Community Support Moderator
Awarded 2011
Awarded 2011

Commented:
http:#37175101 offers no useful inforamtion - thsi is just a restatement of the problem and allusion to solution which is not given.

I first wrote a small program, beacuse I thought that maybe the author is doing something not quite correct with the small sinippet
which was posted. With that I confirmed for myself that I see the same issue.
I then found confirmation that this issue is actually the registered bug in the standards java library - see  posting
http:#37068015.
This was interesting for me, as I know not many of the confirmed bugs in
standard library (most suspisions end up in finding bugs in your own code)
 and this is a useful information for any developer which will deal with this issue,
because you don't spend time wondering why it is not working as it should.
I even suggested some ways for workaround, don't know though if the author actually used them, though.

Still I think this trail was useful for me, and maybe will be useful for someone else.
I think http:#37068015 should be accepted as a solution,
as this explained the issue, and therefore, was certainly and important step towards resolution.

If the author posts the way how it was resolved - that may be a part of the solution either.


Explore More ContentExplore courses, solutions, and other research materials related to this topic.