Link to home
Start Free TrialLog in
Avatar of harishalwala
harishalwalaFlag for Afghanistan

asked on

Unable to track Unit_Increment for JScrollbar in AdjustmentValueChanged

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

Avatar of for_yan
for_yan
Flag of United States of America image

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...




ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It looks like it is still a bug in Java 1.6
Which java are you using ?
maybe they fixed it in 7 ?
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of harishalwala

ASKER

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

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.  
Avatar of South Mod
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="https://www.experts-exchange.com/questions/27460960/27-Nov-11-23-Automated-Request-for-Review-Objection-to-Accept-Q-27427009.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
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.