JavaFX TextField 获取被点击的项目
JavaFX TextField Get Clicked Item
我正在尝试在您单击 TextArea 中的 (WebEngine) 链接时加载它们。但是我不知道如何获取点击的项目。
这是我试过的:
area.setOnMouseClicked(event -> {
WebController.getEngine().load((String) event.getSource());
});
event.getSource()
将是您的 TextArea
。
所以你的代码要么是
area.setOnMouseClicked(event -> WebController.getEngine().load(((TextArea) event.getSource()).getText()));
或更简单:
area.setOnMouseClicked(event -> WebController.getEngine().load(area.getText()));
编辑 - 简单 TextFlow
示例:
@Override
public void start(Stage primaryStage) {
TextFlow textFlow = new TextFlow();
textFlow.setOnMouseClicked(ev -> {
if(ev.getTarget() instanceof Text) {
Text clicked = (Text) ev.getTarget();
System.out.println(clicked);
}
});
IntStream.range(0, 500).mapToObj(Integer::toString).map(Text::new).forEach(textFlow.getChildren()::add);
BorderPane borderpane = new BorderPane(textFlow);
borderpane.setPadding(new Insets(20));
Scene scene = new Scene(borderpane, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
如果有人想看看结果如何,这里是我使用的代码:
package com.ekko.history;
import java.net.URL;
import java.util.Objects;
import java.util.ResourceBundle;
import com.ekko.WebController;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
public class HistoryController implements Initializable {
@FXML
private TextFlow textFlow;
@Override
public void initialize(URL location, ResourceBundle resources) {
textFlow.getChildren().clear();
textFlow.setOnMouseClicked(ev -> {
if(ev.getTarget() instanceof Text) {
Text clicked = (Text) ev.getTarget();
WebController.getEngine().load(clicked.getText());
}
});
HistoryClient.getHistory().stream().filter(Objects::nonNull).forEach(s -> {
Text text = new Text(s + "\n");
textFlow.getChildren().add(text);
});
HistoryClient.getHistory().clear();
}
}
这是 FXML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<ScrollPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.ekko.history.HistoryController">
<content>
<TextFlow fx:id="textFlow" prefHeight="400.0" prefWidth="600.0" />
</content>
</ScrollPane>
感谢 eckig 对我的帮助!
我正在尝试在您单击 TextArea 中的 (WebEngine) 链接时加载它们。但是我不知道如何获取点击的项目。
这是我试过的:
area.setOnMouseClicked(event -> {
WebController.getEngine().load((String) event.getSource());
});
event.getSource()
将是您的 TextArea
。
所以你的代码要么是
area.setOnMouseClicked(event -> WebController.getEngine().load(((TextArea) event.getSource()).getText()));
或更简单:
area.setOnMouseClicked(event -> WebController.getEngine().load(area.getText()));
编辑 - 简单 TextFlow
示例:
@Override
public void start(Stage primaryStage) {
TextFlow textFlow = new TextFlow();
textFlow.setOnMouseClicked(ev -> {
if(ev.getTarget() instanceof Text) {
Text clicked = (Text) ev.getTarget();
System.out.println(clicked);
}
});
IntStream.range(0, 500).mapToObj(Integer::toString).map(Text::new).forEach(textFlow.getChildren()::add);
BorderPane borderpane = new BorderPane(textFlow);
borderpane.setPadding(new Insets(20));
Scene scene = new Scene(borderpane, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
如果有人想看看结果如何,这里是我使用的代码:
package com.ekko.history;
import java.net.URL;
import java.util.Objects;
import java.util.ResourceBundle;
import com.ekko.WebController;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
public class HistoryController implements Initializable {
@FXML
private TextFlow textFlow;
@Override
public void initialize(URL location, ResourceBundle resources) {
textFlow.getChildren().clear();
textFlow.setOnMouseClicked(ev -> {
if(ev.getTarget() instanceof Text) {
Text clicked = (Text) ev.getTarget();
WebController.getEngine().load(clicked.getText());
}
});
HistoryClient.getHistory().stream().filter(Objects::nonNull).forEach(s -> {
Text text = new Text(s + "\n");
textFlow.getChildren().add(text);
});
HistoryClient.getHistory().clear();
}
}
这是 FXML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<ScrollPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.ekko.history.HistoryController">
<content>
<TextFlow fx:id="textFlow" prefHeight="400.0" prefWidth="600.0" />
</content>
</ScrollPane>
感谢 eckig 对我的帮助!