テキスト コントロールを含む子ダイアログを生成する Java Swing アプリケーションがあります。問題は、子ダイアログでキーボード レイアウトを変更すると、ダイアログが閉じられた直後に元に戻ることです。
私が必要としているのは、メイン フレームで切り替えられたか、子フレームで切り替えられたかにかかわらず、切り替えられた後もキーボード レイアウトが維持されることです。
問題を示す SSCCE を次に示します。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class InheritInputContext {
public static void main(String[] arg) {
final MainFrame mainFrame = new MainFrame();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
mainFrame.setPreferredSize(new Dimension(300, 400));
mainFrame.pack();
mainFrame.setLocationRelativeTo(null);
mainFrame.setVisible(true);
}
});
}
}
class MainFrame extends JFrame {
MainFrame() {
setLayout(new BorderLayout());
JTextArea textArea = new JTextArea();
add(textArea, BorderLayout.CENTER);
JButton dialogBtn = new JButton("Dialog");
add(dialogBtn, BorderLayout.SOUTH);
dialogBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ChildDialog cd = new ChildDialog(MainFrame.this);
cd.setPreferredSize(new Dimension(200, 200));
cd.setLocationRelativeTo(MainFrame.this);
cd.pack();
cd.setVisible(true);
}
});
}
}
class ChildDialog extends JDialog {
ChildDialog(Window w) {
super(w);
JTextArea textArea = new JTextArea();
getContentPane().add(textArea);
}
}