I have a JScrollPane with a JTextArea set as its view port.
I update the (multi line) text shown on the JTextArea continously about once a second. Each time the text updates, JScrollPane goes all the way to the bottom of the text.
Instead, I'd like to figure out the line number that is currently shown as the first line in the original text, and have that line be the first line shown when the text has been updated (or if the new text doesn't have that many lines, then scroll all the way to the bottom).
My first attempt of doing this was to get the current caret position, figure the line based on that, and then set the text area to show that line:
int currentPos = textArea.getCaretPosition();
int currentLine = 0;
try {
for(int i = 0; i < textArea.getLineCount(); i++) {
if((currentPos >= textArea.getLineStartOffset(i)) && (currentPos < gameStateTextArea.getLineEndOffset(i))) {
currentLine = i;
break;
}
}
} catch(Exception e) { }
textArea.setText(text);
int newLine = Math.min(currentLine, textArea.getLineCount());
int newOffset = 0;
try {
newOffset = textArea.getLineStartOffset(newLine);
} catch(Exception e) { }
textArea.setCaretPosition(newOffset);
This was almost acceptable for my needs, but requires the user to click inside the text area to change the caret position, so that the scrolling will maintain state (which isn't nice).
How would I do this using the (vertical) scroll position instead ?
I encountered the same problem and found that this answer includes a nice solution that works in this case:
DefaultCaret caret = (DefaultCaret) jTextArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
This is pieced together, untested, from the API documentation:
use getViewport() on your JScrollPane to get a hold of the viewport.
use Viewport.getViewPosition() to get the top-left coordinates. These are absolute, not a percentage of scrolled text.
use Viewport.addChangeListener() to be notified when the top-left position changes (among other things). You may want to create a mechanism to distinguish user changes from changes your program makes, of course.
use Viewport.setViewPosition() to set the top-left position to where it was before the disturbance.
Update:
To stop JTextArea from scrolling, you may want to override its getScrollableTracksViewport{Height|Width}() methods to return false.
Update 2:
The following code does what you want. It's amazing how much trouble I had to go to to get it to work:
apparently the setViewPosition has to be postponed using invokeLater because if it's done too early the text update will come after it and nullify its effect.
also, for some weird reason perhaps having to do with concurrency, I had to pass the correct value to my Runnable class in its constructor. I had been using the "global" instance of orig and that kept setting my position to 0,0.
public class Sami extends JFrame implements ActionListener {
public static void main(String[] args) {
(new Sami()).setVisible(true);
}
private JTextArea textArea;
private JScrollPane scrollPane;
private JButton moreTextButton = new JButton("More text!");
private StringBuffer text = new StringBuffer("0 Silly random text.\n");
private Point orig = new Point(0, 0);
public Sami() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new BorderLayout());
this.textArea = new JTextArea() {
#Override
public boolean getScrollableTracksViewportHeight() {
return false;
}
#Override
public boolean getScrollableTracksViewportWidth() {
return false;
}
};
this.scrollPane = new JScrollPane(this.textArea);
getContentPane().add(this.scrollPane, BorderLayout.CENTER);
this.moreTextButton.addActionListener(this);
getContentPane().add(this.moreTextButton, BorderLayout.SOUTH);
setSize(400, 300);
}
#Override
public void actionPerformed(ActionEvent arg0) {
int lineCount = this.text.toString().split("[\\r\\n]").length;
this.text.append(lineCount + "The quick brown fox jumped over the lazy dog.\n");
Point orig = this.scrollPane.getViewport().getViewPosition();
// System.out.println("Orig: " + orig);
this.textArea.setText(text.toString());
SwingUtilities.invokeLater(new LaterUpdater(orig));
}
class LaterUpdater implements Runnable {
private Point o;
public LaterUpdater(Point o) {
this.o = o;
}
public void run() {
// System.out.println("Set to: " + o);
Sami.this.scrollPane.getViewport().setViewPosition(o);
}
}
}
Related
I want to print multiple label according to the number(no string allowed) you wrote in a text field first. I want it to be dynamical. I want it to change every time you type something in the text field.
So far it can read if it's a number or a string and throw exception if the text doesn't match the requirement.
I've try multiple thing to print multiple Jlabel on the screen, but it didn't work so far.
Here's the code: can you help me?
The main window class
public class MainWindow extends JFrame {
private MainPanel mp = new MainPanel();
public MainWindow()
{
this.setVisible(true);
this.setTitle("Calculateur sur 100");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(200, 400);
this.setLocationRelativeTo(null);
this.setContentPane(mp);
}}
The mainPanel class
public class MainPanel extends JPanel implements ActionListener, MouseListener, KeyListener {
private JTextField tI = new JTextField("Pourcentage");
private JOptionPane jop3 = new JOptionPane();
public MainPanel()
{
this.add(tI);
tI.addKeyListener(this);
tI.addMouseListener(this);
}
//Mathematic calculation
private double onHundred(int tot, int now)
{
return (tot / 100) * now;
}
public void keyReleased(KeyEvent e)
{
boolean ok = true;
try
{
int numbs = Integer.parseInt(tI.getText());
}
catch(Exception s)
{
tI.setText("");
jop3.showMessageDialog(null, "Veuillez entrer seulement des chiffres", "Erreur", JOptionPane.ERROR_MESSAGE);
ok = false;
}
if(ok)
{
System.out.print("Supposed to print");
JLabel[] label = new JLabel[Integer.parseInt(tI.getText())];
for(int i = Integer.parseInt(tI.getText()); i <= 0; i--)
{
label[i] = new JLabel(i + " = " + Math.ceil(onHundred(Integer.parseInt(tI.getText()), i)));
label[i].setVisible(true);
this.add(label[i]);
}
}
}
You MainWindow class should look something like this:
public class MainWindow extends JFrame {
private MainPanel mp = new MainPanel();
public static void main(String[] args) {
new MainWindow();
}
public MainWindow() {
setContentPane(mp);
setTitle("Calculateur sur 100");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
}
Note the order: setContentPane then pack then setVisible. pack replaces setSize as it determines the preferred size of the window based on its components.
I modified your MainPanel class:
public class MainPanel extends JPanel {
private JTextField tI = new JTextField("Pourcentage");
JPanel labelPanel = new JPanel();
public MainPanel() {
setLayout(new BorderLayout());
tI.getDocument().addDocumentListener(new MyDocumentListener());
add(tI, BorderLayout.PAGE_START);
add(labelPanel);
}
private int check() {
int numL;
try {
numL = Integer.parseInt(tI.getText());
} catch (NumberFormatException exc) {
return 0;
}
return numL > 100? 100 : numL;
}
private void update(int numL) {
labelPanel.removeAll();
for (int i = 0; i < numL; i++)
labelPanel.add(new JLabel(String.valueOf(i+1)));
JFrame mainWindow = ((JFrame) SwingUtilities.getWindowAncestor(this));
mainWindow.pack();
mainWindow.repaint();
}
class MyDocumentListener implements DocumentListener {
#Override
public void insertUpdate(DocumentEvent e) {
update(check());
}
#Override
public void removeUpdate(DocumentEvent e) {
update(check());
}
#Override
public void changedUpdate(DocumentEvent e) {
}
}
}
Explanation:
The main panel has the text field separately from another panel which updates dynamically to contain the labels.
The text field uses a DocumentListener instead of a KeyListener to listen to changes in its contents. This is the correct approach for many reasons I will not get into here unless really necessary.
Whenever the text changes, a check method verifies that the input is a number. If it's not it returns 0. If it's more than 100 it returns 100. You can change this behavior as you need.
The value from check is passed to update which clears all the previous labels and reconstructs them. (You can do a bit of optimization here if you want by keeping labels in memory but not displaying them. If the cap is 100 as in my example this won't be noticeable.). The main frame then recalculates the space it needs for all the labels and then repaints.
The labels appear next to each other because the default layout for JPanel is FlowLayout. You can change this as needed.
First - you have Integer.parseInt(tI.getText()) a number of times within the same keyReleased function. When you have done the first check to assign it to int numbs, then use numbs from then on, instead of referring back to tI.getText(). Theoretically the user input can change while you are processing your array, which will cause runtime exceptions or undesired results. Hint - declare numbs directly under ok.
Second - after you add controls programmatically, you need to invalidate the control on to which you are adding them, ie your MainPanel. The invalidate directive tells the control that it is not drawn correctly and needs to be repainted (do this at the completion of your loop). Look through the documentation for JPanel for invalidate and paint.
I wish to make a rough and ready load of reusable swing classes to save a bit of coding and to recap on what I once new however I cannot understand what it is in my problem that is not working; apart from some fundamental basic understanding of course.
Now I have looked at lots of questions about focuses and implementing keylistener, I actually tried a combination of all other threads on this problem and still don't have the answer that works.
In the Easyswing class, I hoped to take advantage of java knowing which constructor to use to determine the component I got, please also suggest a better way of doing it if you know it
here is my main snippet
public class mainbox
{
static Easyswing tix = new Easyswing(300,500);;
public static void main(String[] args) {
System.out.println(tix.getText());
}
And here is the Easyswing class:
public class Easyswing
{
String text;
int locationx, locationy;
JTextArea tarea;
JTextField tfield;
JScrollPane scrollpane;
boolean istarea;
//text on startup, size in rows, size in columns, is editable, might take advantage of different constructors to give different uses to this class outputting messages
Easyswing(String t, int x, int y,int lx, int ly){
//create and initialise text areas starting text, size in number of rows and columns
tarea = new JTextArea("",x, y);
//create slight border to avoid text right on edge
tarea.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
//adds the text area to the scroll pane
scrollpane = new JScrollPane(tarea);
JFrame frame1 = new JFrame(t);
frame1.setSize(x,y);
frame1.setDefaultCloseOperation(frame1.EXIT_ON_CLOSE);
frame1.add(scrollpane);
frame1.setLocation(locationx = lx, locationy = ly);
frame1.setVisible(true);
//if it is a text area then true
istarea = true;
}
//this will create a basic text input field
Easyswing(int lx, int ly){
tfield = new JTextField();
scrollpane = new JScrollPane(tfield);
JFrame frame1 = new JFrame("Write");
frame1.setSize(250,75);
frame1.setDefaultCloseOperation(frame1.EXIT_ON_CLOSE);
frame1.add(scrollpane);
frame1.setLocation(locationx = lx, locationy = ly);
frame1.requestFocus();
//HERE HERE THIS IS WHAT I DID HERE WHY DOESNT THIS WORK???
frame1.addKeyListener(new KeyListener(){
#Override
public void keyPressed(KeyEvent e) {
System.out.println("whut");
}
#Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
#Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
});
frame1.setVisible(true);
istarea = false;
}
I started doing the assignment number #3 by just adding a basic code to understand how it works. But I can’t get out of this problem. I just added an “if” so that if the input text is equal to “hr”, then the turtle would move 2 squares to the right every time. But when I run the code, it is as if it only checks the first characters. If the first two characters are “hr” then it marks a point, but if not, it never again checks the input. So for example if I write:
re
Fd
hr
It never marks the point even though “hr” is there. What can I do so that the TurtleRenderer reads the line every time a \n is inserted and not only once the code is run?
My code:
package turtle;
public class BoardMaker {
private static int MAX = 100;
private boolean[][] board = new boolean[MAX][MAX];
int previousX = 0;
int previousY = 0;
public boolean[][] makeBoardFrom(String description) {
if(description.contentEquals("hr")){
previousX+=2;
board[previousX][previousY]=true;
}
return board;
}
public boolean[][] initialBoard() {
for(int i=0;i<MAX;i++)
{
for(int j=0;j<MAX;j++)
board[i][j]=false;
}
return board;
}
}
The TurtleRenderer class:
package turtle;
public class TurtleRenderer extends Panel implements DocumentListener {
private static final long serialVersionUID = 1;
static final Dimension WINDOW_SIZE = new Dimension(1150, 1150);
boolean [][] board;
final BoardMaker boardMaker;
public TurtleRenderer() {
boardMaker = new BoardMaker();
board = boardMaker.initialBoard();
}
static public void main(String args[]) throws Exception {
JFrame frame = new JFrame("Display image");
JPanel panel = new JPanel();
TurtleRenderer image = new TurtleRenderer();
image.setPreferredSize(WINDOW_SIZE);
JScrollPane textArea = makeTextArea(image);
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
panel.add(image);
buildRightPanel(panel, textArea);
frame.setSize(WINDOW_SIZE);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}});
frame.getContentPane().add(panel);
frame.setVisible(true);
}
static void buildRightPanel(JPanel panel,JComponent textArea) {
JLabel label = new JLabel("Your program:");
label.setPreferredSize(new Dimension(150, 20));
JPanel right = new JPanel();
textArea.setPreferredSize(new Dimension(150,500));
right.setLayout(new BoxLayout(right, BoxLayout.Y_AXIS));
right.add(label);
right.add(textArea);
panel.add(right);
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.white);
g.fillRect(0, 0, WINDOW_SIZE.width, WINDOW_SIZE.width);
if(board == null)
return;
g2d.setColor(Color.red);
for(int i=0;i<board.length;i++) {
for(int j=0;j<board.length;j++) {
if(board[i][j])
g2d.fillRect(9*i+1, 9*j+1, 6, 6);
}
}
}
static JScrollPane makeTextArea(TurtleRenderer image) {
JTextArea textArea = new JTextArea();
textArea.getDocument().addDocumentListener(image);
textArea.setVisible(true);
JScrollPane areaScrollPane = new JScrollPane(textArea);
areaScrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
areaScrollPane.setBorder(BorderFactory.createLineBorder(Color.black));
return areaScrollPane;
}
#Override
public void insertUpdate(DocumentEvent e) {
changed(e);
}
#Override
public void removeUpdate(DocumentEvent e) {
changed(e);
}
#Override
public void changedUpdate(DocumentEvent e) {
changed(e);
}
void changed(DocumentEvent de) {
String description;
Document document = de.getDocument();
try {
description = document.getText(0, document.getLength());
} catch (BadLocationException e) {
throw new RuntimeException(e);
}
try {
board = boardMaker.makeBoardFrom(description);
} catch(ParserException pe) {
board = null;
}
this.repaint();
}
}
Your problem is that you're currently testing if the whole text held by the JTextArea holds "hr". This may be true if hr is the only text in the JTextArea, but once more text is added, this will always be false. What you need to check is if the last line is "hr".
Since this is homework, I won't post a solution, but a pseudo-code logic solution for your DocumentListener could be:
try
get the text String from the document
get the last char from this String
if this last Char == carriage return which is (char)10
split the text into lines using the carriage return as the split's delimiter
get the last line held by this array and check it
if it is hr do something
end if last char == carriage return
end try
catch for BadLocationException
From the Javadocs,
public boolean contentEquals(CharSequence cs)
Compares this string to the specified CharSequence.
The result is true if and only if this String represents the same sequence of char values as the specified sequence.
public boolean contains(CharSequence s)
Returns true if and only if this string contains the specified sequence of char values.
Thus String.contentEquals will function more of a type of String.equals method. There are some differences though.
As with this problem you would require String.contains method to check whether the text contains the String "hr"
One more advice with regards to code efficiency :
You don't have to perform any action in the changedUpdate(DocumentEvent e) method within the DocumentListener. This method is only called when an attribute or a set of attributes has changed, i.e. the style of the document has changed which is not possible in a JTextArea as it does not support styled text.
I hope I have understood your problem correctly.
First, as in a previous comment, the method makeBoardFrom will every time receive
the entire program. If is up to you to split that program into individual commands, and then execute each command. You should not attempt to change the TurtleRenderer class to behave differently.
Second, if you want to move the turtle to the left by two squares you have to mark both
squares as visited, not just the destination square. Right now in your solution, by only using previousX+=2; you only mark the destination square as visited.
Third, in the initialBoard method you also have to actually mark the initial square of the turtle with true. In your case that will be the square at position (0, 0).
I'm a student programming a frogger game, when the frog collides with an object or reaches the end zone either the score is incremented or lives decremented and the frog returned to the start position. this section works and the decrement and increment work when outputting them to the console, I try to pass the variable to the other jpanel and display it there but it doesnt update and display the variable in the textField.
Game Panel
public GamePanel() {
super();
setFocusable(true);
addKeyListener(new KeyList());
System.out.println("GAME PANE FOCUS:" + this.isFocusOwner());
scores.setVisible(true);
lives = p.STARTLIVES;
scores.setCurrentLives(lives);
txtTest.setText("Hello");
txtTest.setVisible(true);
add(scores,new AbsoluteConstraints(0,550,50,800));
Boolean displayable = scores.isDisplayable();
System.out.println("Displayable" + displayable);
scores.setEnabled(false);
scores.revalidate();
scores.repaint();
scores.setVisible(true);
System.out.println("Displayable" + displayable);
car1.start();
car2.start();
car3.start();
car4.start();
Log1.start();
Log2.start();
Log3.start();
Log4.start();
Log5.start();
Log6.start();
Log7.start();
Log8.start();
//check for collisions
}
final public void winZone(Rectangle object){
if(myFrog.frogArea().intersects(object)){
currentScore = currentScore + 100;
System.out.println("current Score " + currentScore);
p.setScore(currentScore);
scores.myScore().setText("hello");
myFrog.lostLife();
}
scores panel
public class jplScores extends JPanel {
Properties p = new Properties();
int currentLives;
int i;
/** Creates new form jplScores */
public jplScores() {
initComponents();
}
public void setCurrentLives(int Lives){
currentLives = Lives;
}
public String getCurrentLives(){
String L = Integer.toString(currentLives);
return L;
}
public JTextField myScore(){
return txtScore;
}
Currently it will display the jpanel from the frame that they are both in but i have tried to make it so its a panel within a panel but i cant get the panel to display from within the game panel.
Any help would be great thanks
public FroggerGame() {
initComponents();
setFocusable(true);
//repaint();
// p.setHeight(550);
// p.setWidth(800);
// p.setLives(3);
// p.setScore(0);
PANELHEIGHT = p.getHeight();
PANELWIDTH = p.getWidth();
welcomePanel();
/*
Toolkit tool = Toolkit.getDefaultToolkit();
imgBackground = tool.getImage(imageBackground);
background = new ImageIcon(imgBackground);
//setDefaultCloseOperation(EXIT_ON_CLOSE);
*/
jps.myScore().addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent evt) {
// txtScorePropertyChange(evt);
jps.myScore().setText(Integer.toString(gp.currentScore()));
System.out.println(Integer.toString(gp.currentScore()));
jps.getScore(gp.currentScore());
System.out.println(" main score " + gp.currentScore());
}
});
}
....
private void btnEnterActionPerformed(ActionEvent evt) {
welcomePanel.setVisible(false);
getContentPane().setLayout(new AbsoluteLayout());
getContentPane().add(gp,new AbsoluteConstraints(0,0,800,550));
getContentPane().add(jps,new AbsoluteConstraints(0,550,800,100));
//gp.setSize(800, 550);
// gp.setBounds(0, 0, 800, 550);
gp.setVisible(true);
gp.requestFocusInWindow();
jps.setVisible(true);
gp is the game panel and jps is the score panel.
This really has little to do with "panels" or Swing GUI coding and all to do with the basic OOPS issue of passing information from one class to another. One way to solve this is to give your Scores panel a public method, say
public void changeScore(int value) {
// in here add the new value to the currently
// displayed value and display the new value.
}
Then the main class, the one with a Scores panel reference, you can call this method, passing in 1 if score is to increase or -1 if it is to decrease.
I think of this as the "push" solution, where one class pushes information into the other class. Another way to solve this is via listeners where the Scores class listens to critical properties of the other class and then changes its own score when an appropriate event occurs, and this often involves using PropertyChangeListeners or other more Swing-specific listener classes. This is sometimes a nicer solution, but I consider it a slightly more advanced concept.
I've a JTextArea component inside JScrollPane and the text area is not editable. I would like to enable scrolling of the text area with up and down arrow keys (i.e. pressing the arrow keys will scroll the text area by one line). Any ideas how to achieve this?
Yes Key Bindings is the way to go, but you don't always need to create your own actions. Swing components come with default Actions that you can often reuse.
See Key Bindings for a complete list of these Actions.
Now that you know the Action name you can just bind it to a keyStroke:
JScrollBar vertical = scrollPane.getVerticalScrollBar();
InputMap im = vertical.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
im.put(KeyStroke.getKeyStroke("DOWN"), "positiveUnitIncrement");
im.put(KeyStroke.getKeyStroke("UP"), "negativeUnitIncrement");
If the JTextArea is non-editable and non-focuseable, it will not respond to the arrow keys. I'm not sure if there is a canonical way to get around this, but one way to make it respond is to set its key binding to respond to the up and down keys when the JTextArea is in the focusable window. An example of this is as follows:
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.text.JTextComponent;
#SuppressWarnings("serial")
public class TestScrollingArea extends JPanel {
private static final String UP = "Up";
private static final String DOWN = "Down";
private JTextArea area = new JTextArea(20, 40);
private JScrollPane scrollPane = new JScrollPane(area);
public TestScrollingArea() {
// make textarea non-editable and non-focusable
area.setEditable(false);
area.setFocusable(false);
area.setWrapStyleWord(true);
area.setLineWrap(true);
add(scrollPane);
// fill area with letters
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 100; j++) {
area.append("abcdefg ");
}
}
// have JTextArea tell us how tall a line of text is.
int scrollableIncrement = area.getScrollableUnitIncrement(scrollPane.getVisibleRect(),
SwingConstants.VERTICAL, 1);
// add key bindings to the JTextArea
int condition = JTextComponent.WHEN_IN_FOCUSED_WINDOW;
InputMap inMap = area.getInputMap(condition);
ActionMap actMap = area.getActionMap();
inMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), UP);
inMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), DOWN);
actMap.put(UP, new UpDownAction(UP, scrollPane.getVerticalScrollBar().getModel(),
scrollableIncrement));
actMap.put(DOWN, new UpDownAction(DOWN, scrollPane.getVerticalScrollBar().getModel(),
scrollableIncrement));
}
// Action for our key binding to perform when bound event occurs
private class UpDownAction extends AbstractAction {
private BoundedRangeModel vScrollBarModel;
private int scrollableIncrement;
public UpDownAction(String name, BoundedRangeModel model, int scrollableIncrement) {
super(name);
this.vScrollBarModel = model;
this.scrollableIncrement = scrollableIncrement;
}
#Override
public void actionPerformed(ActionEvent ae) {
String name = getValue(AbstractAction.NAME).toString();
int value = vScrollBarModel.getValue();
if (name.equals(UP)) {
value -= scrollableIncrement;
vScrollBarModel.setValue(value);
} else if (name.equals(DOWN)) {
value += scrollableIncrement;
vScrollBarModel.setValue(value);
}
}
}
private static void createAndShowUI() {
JFrame frame = new JFrame("TestScrollingArea");
frame.getContentPane().add(new TestScrollingArea());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
Just came across this problem and while the answers was useful in driving me to the right direction some bits of the solution may have changed since then. It worked for me with he following changes:
- it was the InputMap of JScrollPane instance that had to be changed
- actionMapKeys had to be: "unitScrollX" and/or "scrollX" (X= Down, Up, Left, Right). They reside in BasicScrollPaneUI.
You should add KeyListener to your JScrollPane.
All I had to do was to make the scroll pane request focus on mouse enter (as explained in this answer).
var scrollPane = new JScrollBar(jPanelCanvas);
scrollPane.addMouseListener(new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent e) {
// this seems to enable key navigation
if ((e.getComponent() instanceof JScrollPane)) {
e.getComponent().requestFocus();
}
}
});
However I'm not sure on how to tweak the actions of these keys. Maybe by tweaking the actions on the JScrollPane directly as mentioned by tinca's answer.
The call to scrollPane.getActionMap() are showing the following actions defined
"unitScrollRight" -> {BasicScrollPaneUI$Actions#4310}
"unitScrollDown" -> {BasicScrollPaneUI$Actions#4312}
"scrollDown" -> {BasicScrollPaneUI$Actions#4314}
"scrollHome" -> {BasicScrollPaneUI$Actions#4316}
"scrollRight" -> {BasicScrollPaneUI$Actions#4318}
"scrollUp" -> {BasicScrollPaneUI$Actions#4320}
"unitScrollLeft" -> {BasicScrollPaneUI$Actions#4322}
"unitScrollUp" -> {BasicScrollPaneUI$Actions#4324}
"scrollEnd" -> {BasicScrollPaneUI$Actions#4326}
"scrollLeft" -> {BasicScrollPaneUI$Actions#4328}