前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >javafx框架tornadofx入门实战20_dragAndDrop-chooseFile-chooseDirecory

javafx框架tornadofx入门实战20_dragAndDrop-chooseFile-chooseDirecory

原创
作者头像
用户6167008
修改2020-03-19 14:41:45
8821
修改2020-03-19 14:41:45
举报

本实战演示打开或拖拽文件或目录到文本框中,将文件或目录路径或内容显示在文本框中

代码语言:javascript
复制
import javafx.scene.input.TransferMode
import javafx.stage.FileChooser
import tornadofx.*
import java.io.File

class APP21 : App(MainView21::class)
class MainView21 : View("tornadofx入门20_dragAndDrop-chooseFile-chooseDirecory") {
    val content = stringProperty()
    override val root = borderpane {

        center = vbox(5) {
            textarea(content) {
                isWrapText = true
                setOnDragOver {
                  if (it.gestureSource != this && it.dragboard.hasFiles()) {
                      //这一句必须有,否则setOnDragDropped不会触发
                      it.acceptTransferModes(*TransferMode.COPY_OR_MOVE)
                  }
                  it.consume()
                }

                setOnDragDropped {
                  if (it.dragboard.hasFiles()) {
                      val file = it.dragboard.files.first()
                      content.value=file.readText()
                      it.isDropCompleted = true
                  }

                  it.consume()
                }
            }
        }
        top = hbox(5) {
            label("choose or drag file to the textarea below")
        }
        left = vbox(10) {
            button("save") {
                action {
                    _chooseFile(FileChooserMode.Save)
                    File(content.value).writeText("this is save file")
                }
            }

            button("choose Single file") {
                action {
                    _chooseFile(FileChooserMode.Single)
                }
            }
            button("choose Multi file") {
                action {
                    _chooseFile(FileChooserMode.Multi)
                }
            }
            button("choose directory") {
                action {
                    chooseDirectory("choose dir",File(".")).apply {
                        this?.let {
                            content.value=it.absolutePath
                        }
                    }
                }
            }
        }

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档