When a Tab is selected, it often makes a lot of sense to focus an element in the tab content, e.g. an input field. However, this is hardly possible. The following example does not work:
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;
public class FocusTabContent extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
TabPane tabPane = new TabPane();
Tab tab1 = createTab("tab1");
Tab tab2 = createTab("tab2");
tabPane.getTabs().addAll(tab1, tab2);
stage.setScene(new Scene(tabPane));
stage.show();
}
private Tab createTab(String text) {
Tab tab = new Tab(text);
TextArea textArea = new TextArea();
tab.setContent(textArea);
tab.selectedProperty().addListener(obs -> {
if(tab.isSelected()) {
textArea.requestFocus(); // does not work
Platform.runLater(() -> textArea.requestFocus()); // neither does this
}
});
return tab;
}
}
There is a suggested solution that uses Timer/Timeline [1] and another one that listens to mouse events on TabPane (and hence does not work for Ctrl+Tab selection or selection through API) [2]. They are both just suboptimal hacks.
I think this functionality should be supported more directly. Maybe a boolean property `focusContent` on Tab would do, which would allow the user to control whether to transfer focus to Tab content when the Tab gets the focus.
[1] http://stackoverflow.com/a/19046535/
[2] http://stackoverflow.com/a/15648609/
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;
public class FocusTabContent extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
TabPane tabPane = new TabPane();
Tab tab1 = createTab("tab1");
Tab tab2 = createTab("tab2");
tabPane.getTabs().addAll(tab1, tab2);
stage.setScene(new Scene(tabPane));
stage.show();
}
private Tab createTab(String text) {
Tab tab = new Tab(text);
TextArea textArea = new TextArea();
tab.setContent(textArea);
tab.selectedProperty().addListener(obs -> {
if(tab.isSelected()) {
textArea.requestFocus(); // does not work
Platform.runLater(() -> textArea.requestFocus()); // neither does this
}
});
return tab;
}
}
There is a suggested solution that uses Timer/Timeline [1] and another one that listens to mouse events on TabPane (and hence does not work for Ctrl+Tab selection or selection through API) [2]. They are both just suboptimal hacks.
I think this functionality should be supported more directly. Maybe a boolean property `focusContent` on Tab would do, which would allow the user to control whether to transfer focus to Tab content when the Tab gets the focus.
[1] http://stackoverflow.com/a/19046535/
[2] http://stackoverflow.com/a/15648609/