テキスト領域内で TAB を押すと、タブ記号が作成されます。したがって、コードsetTabSize(0)
は何も修正しません。集計幅をゼロに設定するだけです。
単にフォーカス変更ホットキーを探している場合 - Ctrl+TAB は、任意の Swing テキスト コンポーネント内の単純な TAB の代わりに機能し、次に利用可能でフォーカス可能なコンポーネントにフォーカスを切り替えます。Ctrl+Shift+TAB は、フォーカスを前の使用可能でフォーカス可能なコンポーネントに切り替えます。
TAB を他の Swing コンポーネントと同じように動作させたい場合は、カスタム TAB アクションを追加できます。
public static void main ( String[] args )
{
SwingUtilities.invokeLater ( new Runnable ()
{
public void run ()
{
JFrame frame = new JFrame ();
frame.setLayout ( new GridLayout ( 1, 2 ) );
AbstractAction transferFocus = new AbstractAction ()
{
public void actionPerformed ( ActionEvent e )
{
( ( Component ) e.getSource () ).transferFocus ();
}
};
JTextArea textArea1 = new JTextArea ();
textArea1.getInputMap ().put ( KeyStroke.getKeyStroke ( "TAB" ), "transferFocus" );
textArea1.getActionMap ().put ( "transferFocus", transferFocus );
frame.add ( new JScrollPane ( textArea1 ) );
JTextArea textArea2 = new JTextArea ();
textArea2.getInputMap ().put ( KeyStroke.getKeyStroke ( "TAB" ), "transferFocus" );
textArea2.getActionMap ().put ( "transferFocus", transferFocus );
frame.add ( new JScrollPane ( textArea2 ) );
frame.setSize ( 500, 200 );
frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
frame.setLocationRelativeTo ( null );
frame.setVisible ( true );
}
} );
}
次のことを行う必要があることに注意してください。
- デフォルトのタブ KeyStroke を、入力マップの最初のアクション名に置き換えます
- カスタム アクションをコンポーネント アクション マップに追加する