アプリケーション全体でそれを変更する方法を探しています
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class CaretAction extends TextAction
{
private boolean caretForward;
public CaretAction(boolean caretForward)
{
super(caretForward ? "Caret Forward" : "Caret Backward");
this.caretForward = caretForward;
}
public void actionPerformed(ActionEvent e)
{
JTextComponent textComponent = getFocusedComponent();
int start = textComponent.getSelectionStart();
int end = textComponent.getSelectionEnd();
int offset = textComponent.getCaretPosition();
if (start != end)
{
if (caretForward)
offset = (offset == end) ? offset + 1 : end;
else
offset = (offset == start) ? offset -1 : start;
}
else
{
offset += (caretForward) ? 1 : -1;
}
offset = Math.max(offset, 0);
offset = Math.min(offset, textComponent.getDocument().getLength());
textComponent.setCaretPosition( offset );
}
private static void createAndShowUI()
{
JTextField textField1 = new JTextField(10);
JTextField textField2 = new JTextField(10);
JPanel panel = new JPanel();
panel.add( textField1 );
panel.add( textField2 );
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( panel );
frame.pack();
frame.setLocationByPlatform( true );
frame.setVisible( true );
ActionMap map = (ActionMap)UIManager.get("TextField.actionMap");
map.put(DefaultEditorKit.backwardAction, new CaretAction(false));
map.put(DefaultEditorKit.forwardAction, new CaretAction(true));
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
JTextArea、JFormattedTextField などの ActionMap も変更する必要があります。