This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JScrollPane/JTextArea - Disabling Keyboard Events  RSS feed

 
Mike DeStefano
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an application in which I put a JTextArea into a JScrollPane. I want to disable the scrolling when UP or DOWN is pressed (aka only scroll using the mouse). However I try to do it, I cannot seem to stop keyboard events from scrolling the text in the text area.

Below is some sample test code. Please help - already spent a week trying to figure it out as you'll see by all the things I've tried to do to stop keyboard events from scrolling the text area.

Thanks in advance for your help,
Mike DeStefano

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.plaf.basic.*;

public class TestFrame extends JFrame {

protected Container contentPane;

public TestFrame() {
super("TestFrame");

contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());

setupConsolePanel();

pack();
setVisible(true);
}

public void setupConsolePanel() {
JLabel label = new JLabel("Test Text Area With Scrolling -");

MyTextArea myTextArea = new MyTextArea(5,40);
myTextArea.setMargin(new Insets(5,5,5,5));
myTextArea.setEditable(false);
myTextArea.setUI(new MyTextAreaUI());
myTextArea.unregisterKeyboardAction(KeyStroke.getKeyStroke(KeyEvent.VK_UP,0));

JScrollPane myScrollPane = new JScrollPane(myTextArea);
myScrollPane.setUI(new MyScrollPaneUI());
myScrollPane.unregisterKeyboardAction(KeyStroke.getKeyStroke(KeyEvent.VK_UP,0));
myScrollPane.getVerticalScrollBar().setUI(new MyScrollBarUI());
myScrollPane.getVerticalScrollBar().unregisterKeyboardAction(KeyStroke.getKeyStroke(KeyEvent.VK_UP,0));
myScrollPane.getHorizontalScrollBar().setUI(new MyScrollBarUI());
myScrollPane.getHorizontalScrollBar().unregisterKeyboardAction(KeyStroke.getKeyStroke(KeyEvent.VK_UP,0));
myScrollPane.getViewport().setUI(new MyViewportUI());
myScrollPane.getViewport().unregisterKeyboardAction(KeyStroke.getKeyStroke(KeyEvent.VK_UP,0));

JPanel myPanel = new JPanel(new BorderLayout(5,5));
myPanel.add(label, BorderLayout.NORTH);
myPanel.add(myScrollPane, BorderLayout.CENTER);
myPanel.unregisterKeyboardAction(KeyStroke.getKeyStroke(KeyEvent.VK_UP,0));

contentPane.add(myPanel, BorderLayout.SOUTH);

//TESTING TESTING TESTING:

myTextArea.println("testing");
myTextArea.println("testing");
myTextArea.println("testing");
myTextArea.println("testing");
myTextArea.println("testing");
myTextArea.println("testing");
myTextArea.println("testing");
myTextArea.println("testing");
myTextArea.println("testing");
}

public static void main(String[] args) {
JFrame frame = new TestFrame();
}
}


//*****************************************************************
//*****************************************************************
//*****************************************************************

class MyTextArea extends JTextArea {

public MyTextArea() {}

public MyTextArea(int rows, int cols) {
super(rows, cols);
}

public void println(Object o) {
append(o.toString() + "\n");
setCaretPosition(getText().length());
}

public void print(Object o) {
append(o.toString());
setCaretPosition(getText().length());
}

public void println(Object o, boolean setPos) {
append(o.toString() + "\n");
if (setPos) {
setCaretPosition(getText().length());
}
}

public void print(Object o, boolean setPos) {
append(o.toString());
if (setPos) {
setCaretPosition(getText().length());
}
}
}


//*****************************************************************
//*****************************************************************
//*****************************************************************

class MyTextAreaUI extends BasicTextAreaUI {
public MyTextAreaUI() {}
protected void installKeyboardActions() {}
}


//*****************************************************************
//*****************************************************************
//*****************************************************************

class MyScrollPaneUI extends BasicScrollPaneUI {
public MyScrollPaneUI() {}
protected void installKeyboardActions() {}
}

//*****************************************************************
//*****************************************************************
//*****************************************************************

class MyScrollBarUI extends BasicScrollBarUI {
public MyScrollBarUI() {}
protected void installKeyboardActions() {}
}

//*****************************************************************
//*****************************************************************
//*****************************************************************

class MyViewportUI extends BasicViewportUI {
public MyViewportUI() {}
protected void installKeyboardActions() {}
}
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
key bindings is probably the better way to go, but this seems to work OK
(the combobox is just to see if the arrow keys work outside the textArea)

 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a big fan of working with InputMap/ActionMap:

There are two competing mappings here. JTextArea wants the arrow keys to map to actions that move the caret, while JScrollPane wants the arrow keys to map to actions that scroll its view port. I'm not sure which one you want to turn off, so my code can do either, as you like, without having to subclass, which I often find confusing or daunting in Swing.
[ February 14, 2006: Message edited by: Jeff Albertson ]
 
Mike DeStefano
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
EXCELLENT! Thank you both for sharing examples of this. I will take a look in detail at the code today...

FYI - I am looking to prevent both the JScrollPane and JTextArea from scrolling on keyboard events. I only want scrolling on mouse events.

Mike DeStefano
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!