我正在尝试用javafx构建一个收据应用程序……如何在单击按钮时将标签对象添加到整数表字段中
我已经编写了每一个应该实现这一点的代码,比如InvoiceEntry.java和它的.fxml。请大家帮帮我,我该怎么处理这个问题。如果你愿意,我可以给你看其他代码,因为我不知道如何使用stack exchange一次发布所有代码 这是代码的完成 InvoiceEntry.java我正在尝试用javafx构建一个收据应用程序……如何在单击按钮时将标签对象添加到整数表字段中,java,javafx,Java,Javafx,我已经编写了每一个应该实现这一点的代码,比如InvoiceEntry.java和它的.fxml。请大家帮帮我,我该怎么处理这个问题。如果你愿意,我可以给你看其他代码,因为我不知道如何使用stack exchange一次发布所有代码 这是代码的完成 InvoiceEntry.java //this is the button that add objects to the table cells @FXML private void OK01(ActionEvent event) { r
//this is the button that add objects to the table cells
@FXML
private void OK01(ActionEvent event)
{
rImg20.setVisible(false);
tv.setVisible(true);
String combo022 = combo02.getValue().toString();
Integer result = Integer.valueOf(prCombo01.getText());
del01.setVisible(true);
data.add(new InvoiceEntry(
(String) combo01.getValue(),
combo022,
(String) combo03.getValue(),
(String) combo04.getValue(),
(Integer)prCombo01.getText())); //but i cant get this label to add to the
integer column (price) of the table
}
FXML
Java是一种强类型语言,您不能将字符串强制转换为整数,但您可以使用户,但是您应该准备好,如果字符串实际上不是整数,解析可能会出现NumberFormatException
还有一件事,price可能不是int/Integer,很可能你想用它来代替,但是,请千万不要用double/double或float/float来表示任何与钱有关的东西!。可以使用从字符串创建BigDecimal。您需要向我们展示如何声明表。代码不够。我需要一个标签,其整数值只需单击一个按钮即可添加到表中
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.scene.control.TableColumn;
public class InvoiceEntry
{
private final SimpleStringProperty clothe;
private final SimpleStringProperty pattern;
private final SimpleStringProperty designer;
private final SimpleStringProperty color;
private final SimpleIntegerProperty price;
public InvoiceEntry(
String clothe,
String pattern,
String designer,
String color,
Integer price
)
{
this.clothe = new SimpleStringProperty( clothe );
this.pattern = new SimpleStringProperty( pattern);
this.designer = new SimpleStringProperty( designer );
this.color = new SimpleStringProperty( color );
this.price = new SimpleIntegerProperty( price );
}
public String getClothe()
{
return clothe.get();
}
public void setClothe(String iName)
{
this.clothe.set( iName );
}
public String getPattern()
{
return pattern.get();
}
public void setPattern(String iName)
{
this.pattern.set( iName );
}
public String getDesigner()
{
return designer.get();
}
public void setDesigner(String iName)
{
this.designer.set( iName );
}
public String getColor()
{
return color.get();
}
public void setColor(String iName)
{
this.color.set( iName );
}
public Integer getPrice()
{
return price.get();
}
public void setPrice(Integer iName)
{
this.price.set( iName );
}
}
@FXML
private TableView tv;
@FXML
private TableColumn<InvoiceEntry, String> clothe;
@FXML
private TableColumn<InvoiceEntry, String> pattern;
@FXML
private TableColumn<InvoiceEntry, String> designer;
@FXML
private TableColumn<InvoiceEntry, String> color;
@FXML
private TableColumn<InvoiceEntry, Integer> price;
@FXML
private ComboBox combo01;
@FXML
private ColorPicker combo02;
@FXML
private ComboBox combo03;
@FXML
private ComboBox combo04;
@FXML
private Label prCombo01;
private ObservableList<InvoiceEntry> data;
@Override
public void initialize(URL url, ResourceBundle rb)
{
clothe.setCellValueFactory(
new PropertyValueFactory<InvoiceEntry, String>("clothe") );
pattern.setCellValueFactory(
new PropertyValueFactory<InvoiceEntry, String>("pattern") );
designer.setCellValueFactory(
new PropertyValueFactory<InvoiceEntry, String>("designer") );
color.setCellValueFactory(
new PropertyValueFactory<InvoiceEntry, String>("color") );
price.setCellValueFactory(
new PropertyValueFactory<InvoiceEntry, Integer>("price") );
data = FXCollections.observableArrayList(
new InvoiceEntry("", "", "", "", 0));
tv.setItems(data);
}
//this is the button that adds objects to the table cells
@FXML
private void OK01(ActionEvent event)
{
rImg20.setVisible(false);
tv.setVisible(true);
String combo022 = combo02.getValue().toString();
Integer result = Integer.valueOf(prCombo01.getText());
del01.setVisible(true);
data.add(new InvoiceEntry(
(String) combo01.getValue(),
combo022,
(String) combo03.getValue(),
(String) combo04.getValue(),
(Integer)prCombo01.getText()));//but i cant get this label to add to the
integer column (price) of the table
}