Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,10 @@ protected boolean computeValue() {
control.setInputMethodRequests(new ExtendedInputMethodRequests() {
@Override public Point2D getTextLocation(int offset) {
Scene scene = getSkinnable().getScene();
Window window = scene.getWindow();
Window window = scene != null ? scene.getWindow() : null;
if (window == null) {
return new Point2D(0, 0);
}
Comment on lines +337 to +340
Copy link

@delvh delvh Feb 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As long as each scene needs to be located in a window, the following will simplify the code:

Suggested change
Window window = scene != null ? scene.getWindow() : null;
if (window == null) {
return new Point2D(0, 0);
}
if (scene == null) {
return new Point2D(0, 0);
}
Window window = scene.getWindow();

Is it possible to have a scene without an assigned window?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the NullPointer occurs here because the synchronization between JavaFx and AWT threads is not really possible in this case, we cannot ensure that this method is only called when the scene is assigned to a window.
I would check here for null both the scene and the window.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What a pity. Then ignore this comment.

// Don't use imstart here because it isn't initialized yet.
Rectangle2D characterBounds = getCharacterBounds(control.getSelection().getStart() + offset);
Point2D p = getSkinnable().localToScene(characterBounds.getMinX(), characterBounds.getMaxY());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
package test.javafx.scene.control.skin;

import java.util.concurrent.atomic.AtomicBoolean;
import javafx.geometry.Point2D;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.PasswordField;
import javafx.scene.control.skin.TextAreaSkin;
import javafx.scene.control.skin.TextFieldSkin;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

Expand Down Expand Up @@ -92,6 +94,17 @@ public class TextInputControlSkinTest {
passwordField.setText(null);
}

@Test public void noNullPointerIfTextInputNotInScene() {
TextField textField = new TextField();
TextFieldSkin skin = new TextFieldSkin(textField);
textField.setSkin(skin);

// Check that no NullPointerException is thrown if the TextField is not in scene
// and that the default point is returned.
Point2D point = textField.getInputMethodRequests().getTextLocation(0);
assertEquals(new Point2D(0, 0), point);
}

public class FocusableTextField extends TextField {
public void setFocus(boolean value) {
super.setFocused(value);
Expand Down