Skip to content

Commit 710db6e

Browse files
committed
Fixed: FileSelectionField - editing text field is difficult - cursor jumps to the end after each keystroke #27
1 parent 168f506 commit 710db6e

File tree

2 files changed

+7
-21
lines changed

2 files changed

+7
-21
lines changed

scalafx-extras/src/main/scala/org/scalafx/extras/generic_pane/DirectorySelectionField.scala

+4-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011-2023, ScalaFX Project
2+
* Copyright (c) 2011-2024, ScalaFX Project
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -27,7 +27,7 @@
2727

2828
package org.scalafx.extras.generic_pane
2929

30-
import scalafx.application.Platform
30+
import org.scalafx.extras.onFX
3131
import scalafx.beans.property.StringProperty
3232
import scalafx.scene.Node
3333
import scalafx.scene.control.{Button, TextField}
@@ -44,7 +44,7 @@ object DirectorySelectionField {
4444
* existing parent
4545
*/
4646
@tailrec
47-
def existingOrParent(file: File): File =
47+
private def existingOrParent(file: File): File =
4848
if (file.exists()) file
4949
else existingOrParent(file.getCanonicalFile.getParentFile)
5050
}
@@ -90,14 +90,6 @@ class DirectorySelectionField(
9090
prefColumnCount = columns
9191
}
9292

93-
// Make sure that end of the file name is visible
94-
textField.text.onChange { (_, _, _) =>
95-
val location = textField.text.length.get()
96-
Platform.runLater {
97-
textField.positionCaret(location)
98-
}
99-
}
100-
10193
val button = new Button("Browse") {
10294
onAction = _ => {
10395
val initialPath = path.value
@@ -112,6 +104,7 @@ class DirectorySelectionField(
112104
Option(selection).foreach { s =>
113105
path.value = s.getCanonicalPath
114106
lastDirectoryHandler.lastDirectory = s
107+
onFX { textField.positionCaret(textField.text.length.get()) }
115108
}
116109
}
117110
}

scalafx-extras/src/main/scala/org/scalafx/extras/generic_pane/FileSelectionField.scala

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011-2023, ScalaFX Project
2+
* Copyright (c) 2011-2024, ScalaFX Project
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -27,7 +27,7 @@
2727

2828
package org.scalafx.extras.generic_pane
2929

30-
import scalafx.application.Platform
30+
import org.scalafx.extras.onFX
3131
import scalafx.beans.property.StringProperty
3232
import scalafx.scene.Node
3333
import scalafx.scene.control.{Button, TextField}
@@ -73,14 +73,6 @@ class FileSelectionField(
7373
prefColumnCount = columns
7474
}
7575

76-
// Make sure that end of the file name is visible
77-
textField.text.onChange { (_, _, _) =>
78-
val location = textField.text.length.get()
79-
Platform.runLater {
80-
textField.positionCaret(location)
81-
}
82-
}
83-
8476
val button = new Button("Browse") {
8577
onAction = _ => {
8678
val initialPath = path.value
@@ -101,6 +93,7 @@ class FileSelectionField(
10193
Option(selection).foreach { s =>
10294
path.value = s.getCanonicalPath
10395
lastDirectoryHandler.lastDirectory = s
96+
onFX { textField.positionCaret(textField.text.length.get()) }
10497
}
10598
}
10699
}

0 commit comments

Comments
 (0)