我想在我的新组件中添加自定义操作.这该怎么做?
示例代码:
零件
public class MyCustomComponent extends Region {
public MyCustomComponent(){
super();
this.setOnMouseClicked(new EventHandler<MouseEvent>(){
@Override
public void handle(MouseEvent event) {
/* throw my custom event here and handle it in my FXML controller - but how? :-( */
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
调节器
public class MyController {
@FXML protected void myCustomAction(ActionEvent event) {
// do something
}
}
Run Code Online (Sandbox Code Playgroud)
FXML:
<BorderPane fx:controller="fxmlexample.MyController"
xmlns:fx="http://javafx.com/fxml">
<top>
<MyCustomComponent onAction="#myCustomAction">
</MyCustomComponent>
</top>
</BorderPane>
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助
我有一个非常简单的问题.当我在Scene Builder中创建用户界面时,我想稍后在我的代码中引用它.例如,我会在我的FXML中创建一个窗格,将其加载到我的场景中,然后将该场景放在我的舞台上.之后我想做一些事情,比如通过ID或任何类型的引用获取此窗格并向其添加一些元素,例如在单击按钮后,我会将图片添加到此引用窗格.此外,我会从我的控制器(在我的fxml中创建的按钮的onclick)中执行此操作,因此,我是否需要对我的场景进行某种引用或者使用某种方法来操作该窗格的内容?.可能吗?
我正在尝试MenuButton使用JavaFX而不是通过代码修改箭头的颜色CSS。
我在里面找到它caspian.css:
.menu-button > .arrow-button > .arrow {
-fx-background-insets: 1 0 -1 0, 0;
-fx-background-color: -fx-mark-highlight-color, -fx-mark-color;
-fx-padding: 0.25em; /* 3 */
-fx-shape: "M 0 -3.5 v 7 l 4 -3.5 z";
}
Run Code Online (Sandbox Code Playgroud)
我试图使用类似的东西:
menubutton.lookup(".arrow");
Run Code Online (Sandbox Code Playgroud)
但它抛出 NullPointerException
当我这样做时:
System.out.println(this.getStyleClass().toString());
它仅表明:menu-button仅。
所以任何人都可以给我一种无需使用CSS即可使用Java对其进行修改的方法吗?
如何更改Label文本时如何调用侦听器
Label myLabel= new Label("text1");
Run Code Online (Sandbox Code Playgroud)
如果我改变文字
myLabel.settext("text2");
Run Code Online (Sandbox Code Playgroud)
有任何听众,所以我可以执行此操作
myLabel.LISTENER{
System.out.println("Label Text Changed");
}
Run Code Online (Sandbox Code Playgroud) 我想通过CSS设置MenuItem的图标.我搜索了很多并尝试了很多,现在没有任何成功.
这是一个小的FXML示例:
<MenuBar>
<menus>
<Menu text="File">
<items>
<MenuItem text="Open" styleClass="iconOpen" />
<MenuItem text="Save" />
<MenuItem text="Exit" />
</items>
</Menu>
</menus>
</MenuBar>
Run Code Online (Sandbox Code Playgroud)
我在CSS中使用:
.iconOpen {
-fx-background-color: red;
-fx-graphic: url("open.png");
}
Run Code Online (Sandbox Code Playgroud)
因此,MenuItem只有红色背景颜色.但没有图标.
我可以直接在FXML中将图标作为ImageView for MenuItem集成,但我不能用CSS来设置它.但我想将样式(css)与结构(fxml)分开.
也许有人可以给我一个提示.我不知道我在这里做错了什么.
非常感谢!
我是JavaFX的新手,我正在尝试创建类似于下图的通知徽章. 
它将被用作通知计数,我希望将它放在JavaFX按钮的角落.我的问题是我找不到支持它的原生解决方案.有人可以推荐一种方法吗?
谢谢
我在该特定控制器中有一个tableView,我想用传递的参数值初始化.问题是,在控制器将参数设置为变量之前,正在执行initialize方法.任何建议都会很棒.
我只是为了清楚地提到Location参数.
这是控制器的调用方法:
protected static void openReservationNewDialog(Location loc){
try{
FXMLLoader loader = new FXMLLoader();
loader.setLocation(viewLogic.class.getResource("openNewReservations.fxml"));
AnchorPane page = (AnchorPane) loader.load();
Stage dialogStage = new Stage();
dialogStage.setTitle("New Reservation");
dialogStage.setResizable(false);
Scene scene = new Scene(page);
dialogStage.setScene(scene);
openReservationNewDialogController controller=loader.getController();
controller.setDialogStage(dialogStage);
controller.setSelectedLocation(loc);
dialogStage.showAndWait();
} catch(Exception e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
这是相关的控制器部分:
public class openReservationNewDialogController implements Initializable{
@FXML
private TextField reservationId;
/**Reservation's member*/
@FXML
private ComboBox<String> member=new ComboBox<String>();
@FXML
private ComboBox<String> sMinutes= new ComboBox<String>();
@FXML
private ComboBox<String> eMinutes= new ComboBox<String>();
@FXML
private ComboBox<String> sHours= new ComboBox<String>(); …Run Code Online (Sandbox Code Playgroud) 我有一个父控制器,其中包含一个按钮.当我点击按钮时,它打开新窗口并将一些数据显示在表格中.我用于打开窗口的代码是
Stage stage = new Stage();
FXMLLoader fxmlLoader = new FXMLLoader(
getClass().getResource("../layout/SearchCustomer.fxml"));
Parent parent = (Parent) fxmlLoader.load();
Scene scene = new Scene(parent);
stage.initModality(Modality.APPLICATION_MODAL);
stage.initOwner(parent.getScene().getWindow());
stage.setScene(scene);
stage.resizableProperty().setValue(false);
stage.showAndWait();
Run Code Online (Sandbox Code Playgroud)
它正确打开窗口.现在我需要的是,当我双击子窗口的表行时,它应该在父控制器文本框中设置一些值.我们如何将这个值从子控制器传递给父控制器?
我有一个JavaFX应用程序,从主窗口开始作为一个TabPane对象。在那个TabPane我有一个特定的Tab,其中包含一个Button对象,可以将新的选项卡添加到main TabPane。但我需要每个缠绕Tab在主TabPane用一个对象实例(每个标签应使用类的一个实例工作Merchant)
该类Merchant具有一些方法createSortiment(),可以ArrayList根据Merchant对象的参数生成随机选择的项目。
主控制器GUIController控制主窗口,另一个控制器GUIMerchantTabController控制选项卡。
我需要能够添加一个新选项卡(我可以这样做)并将其绑定到。Merchant JohnSmith = new Merchant();然后,我需要控制器GUIMerchantTabController能够fx:id="createSortiment"通过调用来响应按钮的动作事件JohnSmith.createSortiment()(我不知道如何做到这一点),并且每个产生的项目添加到一些Accordion在Tab作为TitledPane(我还可以做)。
我的主要问题:
我如何将JohnSmith的实例保存到TabGUIMerchantTabController将能够使用John的方法并访问他的数据的特定位置?可以引用某种对象实例吗?我可以以某种方式将对象作为节点添加到该窗格中吗?Java中是否存在某些“数据”属性(如HTML一样<element data-storeSomething="Some text here, or json object">)?
我认为没有必要查看我的文件,但是出于更好的主意,这些是我的fmxls ...
商户标签的FXML:
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.Accordion?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.control.Label?>
<?import …Run Code Online (Sandbox Code Playgroud) 我正在寻找关于为什么我们应该使用一种方法而不是加载和显示新的FXML阶段的反馈.
大多数时候,我看到教程和类似的东西显示从一个单独的类完成一个阶段的加载.但是,它也可以在FXML文件的控制器本身内完成,我个人认为这样做更清洁,更易于管理.
请考虑以下Main.java类:
public class Main extends Application {
@Override
public void start(Stage stage) throws Exception {
// Method 1:
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("Layout.fxml"));
loader.setController(new LayoutController());
stage.setScene(new Scene(loader.load()));
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
Run Code Online (Sandbox Code Playgroud)
这似乎是流行的方法.它创建控制器并设置Scene然后显示它.
但是,如果我们将start()方法改为:
@Override
public void start(Stage stage) throws Exception {
LayoutController controller = new LayoutController();
controller.showStage();
}
Run Code Online (Sandbox Code Playgroud)
并将FXML加载代码移动到LayoutController构造函数中,结果是一样的:
public class LayoutController {
@FXML …Run Code Online (Sandbox Code Playgroud)