- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在 try catch 从 addItems.java 类输入的 TextField 和 Control Field 值。然后在 ShoppingCartController.java 类上生成的 TableView 上显示该数据。当我在 addItems.java 文件上点击 add 时,我没有收到错误消息,但是当我返回到主 shoppingCartController 场景时,输入的数据不存在。
以下是选择“添加”按钮时调用的内容:
public void handleitemAdd(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("additems.fxml"));
Parent addItem_page = loader.load();
ObservableList<Products> list = FXCollections.observableArrayList();
list.add(new Products(productPriority.toString(),
productName.getText(),
Double.parseDouble(productPrice.getText()),
Integer.parseInt(productQty.getText())));
table.getItems().add(list);
System.out.println("Displaying information to consoles: Ensuring the addItem method worked as expected.");
}
如果您能在审查此问题方面提供任何帮助,我们将不胜感激。
addItems.java
package shoppingcart;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
public class addItems implements Initializable {
// Fields used to add items to cart
@FXML private TextField productName = new TextField();
@FXML private TextField productQty = new TextField();
@FXML private TextField productPrice = new TextField();
@FXML private ChoiceBox productPriority = new ChoiceBox();
// Create the TableView
TableView table = new TableView(shoppingCartController.getProduct());
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
//Used to Initialize the Scene
}
public void handleitemAdd(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("additems.fxml"));
Parent addItem_page = loader.load();
ObservableList<Products> list = FXCollections.observableArrayList();
list.add(new Products(productPriority.toString(),
productName.getText(),
Double.parseDouble(productPrice.getText()),
Integer.parseInt(productQty.getText())));
table.getItems().add(list);
System.out.println("Displaying information to consoles: Ensuring the addItem method worked as expected.");
}
public void handleitemReturnCart(ActionEvent event) throws IOException {
Parent shoppingCart_page = FXMLLoader.load(getClass().getResource("shoppingcart.fxml"));
Scene shoppingCart_scene = new Scene(shoppingCart_page);
Stage shoppingCart_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
shoppingCart_stage.setScene(shoppingCart_scene);
shoppingCart_stage.show();
System.out.println("Displaying information to console: Ensuring that user returned to main page");
}
}
addItems.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane id="" fx:id="productPage" prefHeight="750.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="shoppingcart.addItems">
<children>
<GridPane layoutX="189.0" layoutY="193.0" prefHeight="364.0" prefWidth="399.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="83.0" minHeight="10.0" prefHeight="66.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="88.0" minHeight="10.0" prefHeight="88.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="76.0" minHeight="10.0" prefHeight="59.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="99.0" minHeight="10.0" prefHeight="70.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Text id="" fx:id="labelitemPriority" strokeType="OUTSIDE" strokeWidth="0.0" text="Item Priority:" />
<ChoiceBox id="" fx:id="productPriority" disable="true" prefWidth="200.0" GridPane.columnIndex="1" />
<TextField id="" fx:id="productName" prefHeight="14.0" prefWidth="63.0" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Text id="" fx:id="labelitemName" strokeType="OUTSIDE" strokeWidth="0.0" text="Item Name:" GridPane.rowIndex="1" />
<TextField id="" fx:id="productQty" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Text id="" fx:id="labelitemQty" strokeType="OUTSIDE" strokeWidth="0.0" text="Item Qty:" GridPane.rowIndex="2" />
<Text id="" fx:id="labelitemPrice" strokeType="OUTSIDE" strokeWidth="0.0" text="Item Price:" GridPane.rowIndex="3" />
<TextField id="" fx:id="productPrice" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<Button id="itemsAdd" fx:id="productAdd" onAction="#handleitemAdd" prefHeight="37.0" prefWidth="210.0" text="ADD" GridPane.columnIndex="1" GridPane.rowIndex="4" />
</children>
</GridPane>
<Text layoutX="354.0" layoutY="146.0" scaleX="4.085561690303489" scaleY="4.947136563876652" strokeType="OUTSIDE" strokeWidth="0.0" text="Add Items" wrappingWidth="67.541015625">
<font>
<Font size="14.0" />
</font></Text>
<Button id="returnCartHome" fx:id="productHome" layoutX="288.0" layoutY="609.0" mnemonicParsing="false" onAction="#handleitemReturnCart" prefHeight="48.0" prefWidth="201.0" text="Return to Cart" />
</children>
</AnchorPane>
shoppingCartController.java
package shoppingcart;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
public class shoppingCartController implements Initializable {
//Table used for Shopping Cart
@FXML private TableView<Products> item_Table;
@FXML private TableColumn<Products, String> item_Priority;
@FXML private TableColumn<Products, String> item_Name;
@FXML private TableColumn<Products, Number> item_Qty;
@FXML private TableColumn<Products, Number> item_Price;
private ObservableList<Products> productItems;
//The Initializer used to load data prior to loading view.
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
item_Priority.setCellValueFactory(cellData -> cellData.getValue().itemPriorityProperty());
item_Name.setCellValueFactory(cellData -> cellData.getValue().itemNameProperty());
item_Qty.setCellValueFactory(cellData -> cellData.getValue().itemQtyProperty());
item_Price.setCellValueFactory(cellData -> cellData.getValue().itemPriceProperty());
//Display all items in table
item_Table.setItems(getProduct());
}
// Method used to get the list of products
public static ObservableList<Products> getProduct() {
//Obseravable list which can be used to collect items
ObservableList<Products> products = FXCollections.observableArrayList();
return products;
}
public void handleitemAddition(ActionEvent event) throws IOException {
Parent addItem_page = FXMLLoader.load(getClass().getResource("addItems.fxml"));
Scene addItem_scene = new Scene(addItem_page);
Stage addItem_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
addItem_stage.setScene(addItem_scene);
addItem_stage.show();
System.out.println("Displaying information to consoles: Deleting Selected Item");
}
public void handleitemDelete(ActionEvent event) throws IOException {
System.out.println("Displaying information to consoles: Deleting Selected Item");
}
}
shoppingcart.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.cell.PropertyValueFactory?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane prefHeight="750.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="shoppingcart.shoppingCartController">
<TableView fx:id="item_Table" layoutX="77.0" layoutY="174.0" prefHeight="352.0" prefWidth="646.0" tableMenuButtonVisible="true">
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
<columns>
<TableColumn fx:id="item_Priority" editable="false" prefWidth="75.0" text="Priority">
<cellValueFactory>
<PropertyValueFactory property="itemPriority" />
</cellValueFactory>
</TableColumn>
<TableColumn fx:id="item_Name" editable="false" prefWidth="75.0" text="Item Name">
<cellValueFactory>
<PropertyValueFactory property="itemName" />
</cellValueFactory>
</TableColumn>
<TableColumn fx:id="item_Qty" editable="false" prefWidth="75.0" text="Item Qty">
<cellValueFactory>
<PropertyValueFactory property="itemQty" />
</cellValueFactory>
</TableColumn>
<TableColumn fx:id="item_Price" editable="false" prefWidth="75.0" text="Item Price">
<cellValueFactory>
<PropertyValueFactory property="itemPrice" />
</cellValueFactory>
</TableColumn>
</columns>
</TableView>
<Text layoutX="199.0" layoutY="119.0" scaleX="1.4035087719298245" scaleY="1.7005740221599253" strokeType="OUTSIDE" strokeWidth="0.0" text="Shopping Cart Application" wrappingWidth="401.1374694108964">
<font>
<Font size="34.0" />
</font>
</Text>
<Button id="itemsDelete" layoutX="501.0" layoutY="571.0" mnemonicParsing="false" onAction="#handleitemDelete" prefHeight="46.0" prefWidth="222.0" text="Delete Selected" />
<Button fx:id="itemsAdd" layoutX="77.0" layoutY="571.0" onAction="#handleitemAddition" prefHeight="46.0" prefWidth="222.0" text="Add Items" />
</AnchorPane>
编辑:
非常感谢您的帮助,我仍在学习 Java,并且有一个后续问题。
我更新了 addItems.java 以包括:
public void handleitemAdd(ActionEvent event) throws IOException {
products.add(new Products(productPriority.toString(),
productName.getText(),
Double.parseDouble(productPrice.getText()),
Integer.parseInt(productQty.getText())));
System.out.println("Displaying information to consoles: Ensuring the addItem method worked as expected.");
}
以及要包含的 shoppingCartController:
public void setTableItems(ObservableList<Products> products) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("shoppingcart.fxml"));
Scene shoppingCart_scene = new Scene(loader.load());
shoppingCartController controller = loader.getController();
controller.setTableItems(products);
}
但是该值仍然没有被继承,我还确保了参数被捕获(通过 Debug模式确认)。
最佳答案
正如 @Kleopatra 在评论中所说,静态访问有时可能很棘手,所以要小心并坚持 Java 通用命名约定,你的代码很难跟踪
当返 repo 物车时,您可以通过 Controller 设置列表将商品传回 ShoppingCart
FXMLLoader loader = new FXMLLoader(getClass().getResource("shoppingcart.fxml"));
Scene shoppingCart_scene = new Scene(loader.load());
shoppingCartController controller = loader.getController();
controller.setProducts(list);
并且在setProduct()
函数中您可以更新表格
或者
您可以在静态函数之外创建 ObservableList
但我不建议这样做,我建议您搜索有关 javafx 中 Controller 之间传递数据的信息,请参阅下面的链接 https://stackoverflow.com/a/14190310/5303683
关于JavaFX - 从不同场景向 TableView 添加数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58187586/
我不知道这是不是问这种问题的最佳地点, 我看到一些 JavaFX伙计们,重新标记 一些问题通过替换 javafx来自 javafx-2并采用新的 javafx-8 .它被弃用了还是什么? 编辑 : 不
错误本身: Error:java: invalid flag: --add-modules=javafx.fxml,javafx.graphics,javafx.controls,javafx.bas
这个想法是让一个应用程序在每个显示器上显示两个不同的窗口(阶段),该应用程序应该知道计算机有多少个显示器及其分辨率。 javafx有可能吗? 最佳答案 对于当前版本的 JavaFX (2.2),您可以
我正在将我的项目从 javafx 1.3 转换为 javafx 2.1。但我对 javafx.lang 有疑问包裹。 最佳答案 JavaFX 1.3 lang 包内容被拆分并移至下一个位置: 时长变为
当我尝试将标签添加到 gridpane 中时,如第二张图片所示,它不起作用。我已经尝试了很多东西,比如添加 CSS,但它仍然无法正常工作。为什么第 113 和 114 行不起作用? (opcje.se
我有一个JavaFX ContextMenu分配给滚动面板的鼠标右键单击。它会打开,但在滚动 Pane 外部单击时不会关闭。我可以在滚动 Pane 中添加另一个鼠标事件以将其隐藏,但这只能解决1个问题
我有一个tableview,其中附有一个可观察到的自定义类对象的列表(类类型:SalesInvoiceNetSale)。该表中的所有数据都可以正常显示。可观察列表中的最后一项是总计行(类类型:Sale
关闭。这个问题需要更多 focused .它目前不接受答案。 想改进这个问题?更新问题,使其仅关注一个问题 editing this post . 2年前关闭。 Improve this questi
我想知道如何在JavaFX中绘制半圆。我尝试使用Shape和QuadCurve,但无法制作出完美的半圆。 这是我要绘制的图片: 最佳答案 您链接的图片实际上是一个半圆环。您可以通过绘制嵌套的2条圆弧和
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我正在寻找 JavaFX 支持的图像类型(最新)列表,例如PNG、JPEG、TIFF。不同的搜索引擎没有帮助......知道从哪里开始吗? 更特别的是,我对 16 位灰度图像(不同格式)和罕见的受支持
我希望在 javafx 中让标签每 0.1 秒闪烁一次。文本显示在后台运行的 ImageView gif 的顶部。我将如何去做,或者您对最佳方法有什么建议? 谢谢 最佳答案 @fabian 的解决方案
我需要测试所选项目的值以调用不同的方法,因此我编写了添加侦听器的代码,但是该代码生成语法错误 @FXML private JFXComboBox cmbComp; cmbComp.valuePrope
我正在 Javafx 中编写一个非常简单的应用程序,其中舞台上有一个带有文本框的按钮作为一个场景。现在,我想要的行为是,当我单击按钮时,我可以使用另一个按钮加载另一个场景和舞台上的一个文本框,然后删除
编辑:如果用户单击“删除”以删除 ListView 中的项目,我会弹出一个警告框。它有效,但我希望它能超越原来的舞台。它出现在我的第一台显示器上。有什么方法可以设置警报显示时的位置吗? 请注意,“所有
我想使用 JavaFX 编写一个笔画绘图应用程序。我有一个压敏绘图板,如果能够读取笔的压力和倾斜值,那就太好了。 JavaFX 有一个 API 可以处理鼠标、触摸和滑动输入,但似乎没有任何东西可以产生
我在 JavaFX 中使用条形图和折线图。当我使两个图表大小相同并将它们放在同一位置时,它们完美地相互重叠。我如何使折线图显示在条形图的顶部。 目前我已将它们的不透明度设置为 0.7,这样它们“看起来
此问题与 this 相关。现在我想为字段值等于某个值的行着色。 @FXML private TableView tv_mm_view; @FXML private Ta
我有一个程序,可以生成高度图(0-255 的整数的 2D 数组),并使用 Shape3D“Box”对象为每个“像素”构建 3D View ,其高度与其在高度图中的值成比例。这会创建一个看起来很酷的四四
我想为 JavaFX 创建上下文菜单。这是我测试过的代码。但是由于某种原因,当我右键单击树节点时没有上下文菜单。你能帮我找出我的错误吗。 import java.util.Arrays; import
我是一名优秀的程序员,十分优秀!