Replace `requestFocus()` by `requestFocusInWindow()` where applicable

The former gives focus to the window in which a component is present,
while the latter only changes the focus within the current window (not
focusing the window itself if it is not focused yet). Java documentation
recommends changing `requestFocusInWindow()` where possible, due to some
platform-dependent behaviour in `requestFocus()`.

When focusing the serial monitor and plotter, `requestFocus()` is still
used, since then the focused window *should* change.
This commit is contained in:
Matthijs Kooijman 2015-12-11 17:14:45 +01:00 committed by Martino Facchin
parent f57b90c1c8
commit 1d21378a5f
3 changed files with 4 additions and 4 deletions

View File

@ -1608,7 +1608,7 @@ public class Editor extends JFrame implements RunnerListener {
SwingUtilities.invokeLater(() -> { SwingUtilities.invokeLater(() -> {
codePanel.removeAll(); codePanel.removeAll();
codePanel.add(tabs.get(index), BorderLayout.CENTER); codePanel.add(tabs.get(index), BorderLayout.CENTER);
tabs.get(index).requestFocus(); // get the caret blinking tabs.get(index).requestFocusInWindow(); // get the caret blinking
// For some reason, these are needed. Revalidate says it should be // For some reason, these are needed. Revalidate says it should be
// automatically called when components are added or removed, but without // automatically called when components are added or removed, but without
// it, the component switched to is not displayed. repaint() is needed to // it, the component switched to is not displayed. repaint() is needed to

View File

@ -147,7 +147,7 @@ public class EditorStatus extends JPanel {
editField.setVisible(true); editField.setVisible(true);
editField.setText(dflt); editField.setText(dflt);
editField.selectAll(); editField.selectAll();
editField.requestFocus(); editField.requestFocusInWindow();
repaint(); repaint();
} }

View File

@ -600,9 +600,9 @@ public class EditorTab extends JPanel implements SketchCode.TextStorage {
} }
@Override @Override
public void requestFocus() { public boolean requestFocusInWindow() {
/** If focus is requested, focus the textarea instead. */ /** If focus is requested, focus the textarea instead. */
textarea.requestFocus(); return textarea.requestFocusInWindow();
} }
} }