创建API以添加组件

时间:2018-02-16 23:30:55

标签: javafx fxml

我想创建一个Sub Test() Application.ScreenUpdating = False Dim Template As Worksheet Dim Evaluations As Worksheet Dim Nb_Rows As Integer Dim i As Integer Dim x, Row as Integer Set Template = ThisWorkbook.Sheets("Evaluation Form Template") Set Evaluations = ThisWorkbook.Sheets("Evaluations") Template.Range("A61:A70").ClearContents ' the table in this example starts in A1 ' please mind that blank lines might cause issues Nb_Rows = Evaluations.[a1].CurrentRegion.Rows.Count Row = 61 ' first row to input results in Template x = 0 ' needed to increment For i = 1 to Nb_Rows If Evalutations.Cells(i, 1) = Template.[a1] Then Template.cells(Row + x, 1) = Evalutations.Cells(i, 29) x = x + 1 End If Next i Set Template = Nothing Set Evaluations = Nothing Application.ScreenUpdating = True End Sub 来修改API

对于示例,默认Scene有以下fxml

Default View

使用Components,我不想在连接名称下添加一些API,用于示例:

Wanted

首先,我为Components创建了Controller

  

< BorderPane fx:controller =“ConnectionController”[...]

并向FXML添加ID,我不想修改:

  

< GridPane fx:id =“form”[...]

GridPane方面,我已注册Controller与之交谈:

Component

但是,当我尝试从import javafx.fxml.FXML; import javafx.scene.layout.GridPane; public class ConnectionController extends Controller { @FXML GridPane form; public void init(Main main) { this.main = main; } public GridPane getForm() { return form; } } 获取Controller以获取FXML以添加一些GridPane时,不会应用任何内容,因为示例:

Components

在插件方面:

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/ui/" + file + ".fxml"));
Parent root1 = (Parent) fxmlLoader.load();
theController = fxmlLoader.getController(); // <<<< Here i get the Controller
[...] // Set the Stage, add to Scene and show the Stage...
你能告诉我,有什么不对吗?我没有真正体验Button button = new Button(); button.setText("click me!"); theController.getForm().add(button, 0, 1, 0, 0); - 使用JavaFX / AWT组件的“旧Java技术”,我知道,您必须无效或**重绘*组件......这是同样的可能性吗?

修改

当我使用Swing方法获取GridPane时,我可以更改样式属性,如:

getForm()

enter image description here

但是,当我将theController.getForm().setStyle("-fx-background-color: #FF0000"); 设置为Background Color时,添加red并在此之后设置ButtonBackground Color,背景将是没画绿色:

green

1 个答案:

答案 0 :(得分:1)

这段代码很奇怪:

theController.getForm().add(button, 1, 1, 0, 0). 

被调用的GridPane API是:

  

add(Node child, int columnIndex, int rowIndex, int colspan, int rowspan)

因此,您要求节点跨越0行和列,这实际上是不可能的。

当您尝试在Java 8u144上运行此代码时,我得到一个例外:

java.lang.IllegalArgumentException: rowSpan must be greater or equal to 1, but was 0.

我的猜测是你只是在某个地方吞下这个例外。这可以解释为什么add语句之后的代码似乎没有执行(因为抛出的异常改变了控制流)。