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() {}
}