0

I have three different jtable on three diffenernt jscrollpane - one next to the other.
I successfuly written some code that makes them all scroll together when I scroll the mouse wheel.
I inherrited JScrollpane and overriden its processMouseWheelEvent methos as follows:

  public void processMouseWheelEvent(MouseWheelEvent e) {
    ...
    if (e.getSource() == this) {
        for (BTSJScrollPane other : effected) {
            other.processMouseWheelEvent(e);
        }
    }
}

I have canelled the pageup pagedown using

 final InputMap inputMap =   
             comp.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    inputMap.put(KeyStroke.getKeyStroke("PAGE_DOWN"), EMPTY);
    inputMap.put(KeyStroke.getKeyStroke("PAGE_UP"), EMPTY);

    comp.getActionMap().put(EMPTY, emptyAction);

But,
my only problem now is that when the user clicks up and down, they dont go together but rather scroll indipently.
Any suggestions ?
So far I wrote in my scrollpane something like

    KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
        @Override public boolean dispatchKeyEvent(KeyEvent e) {
            if (thisIsThesourceTable) {
                if (e.getKeyCode() == 38 || e.getKeyCode() == 40) {

                }
            }
            return false;
        }
    });

But :
1. is there a better way?
2. maybe I should do it somehow in the actionMap?
3. how do I pass the key up/down event to the other scrollpane?
4. can I know if I am near the end of the viewing part in the scroll pane? is it needed

mKorbel
  • 108,320
  • 17
  • 126
  • 296
Bick
  • 15,895
  • 44
  • 133
  • 231

1 Answers1

7

1, 2, & 3: You should use listeners on the scrollbars so it works with mouse, keyboard, and scrollbar interaction. That way you won't need key or mouse listeners. Here is one way:

scroll1.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {

    @Override
    public void adjustmentValueChanged(AdjustmentEvent e) {
       scroll2.getVerticalScrollBar().setValue(e.getValue());
    }
});

4: Look at JScrollPane's getVerticalScrollBar().getMaximumSize() and getModel() to find out if you're near the end.

Jim
  • 3,087
  • 4
  • 20
  • 31
  • Wow. a. If I could have given you more point I would. b. I have deleted now a lot of code with this one line. c. I dont need now answer to no. 4.. d. Thanks a lot. – Bick Apr 23 '12 at 15:35
  • Just glad to give back to the community that has helped me so much over the years. – Jim Apr 23 '12 at 15:49