gpt4 book ai didi

JavaFX 表格 View 不显示按钮

转载 作者:行者123 更新时间:2023-12-02 05:45:46 27 4
gpt4 key购买 nike

我试图关注this example在桌面 View 上。

这是SSCCE :

摘要.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.control.cell.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.shape.*?>

<fx:root maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" type="javafx.scene.layout.AnchorPane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<TableView fx:id="table" layoutY="50.0" prefHeight="350.0" prefWidth="600.0">
<columns>
<TableColumn prefWidth="79.5" text="Date">
<cellValueFactory><PropertyValueFactory property="date" />
</cellValueFactory>
</TableColumn>
<TableColumn prefWidth="78" text="Label">
<cellValueFactory><PropertyValueFactory property="label" />
</cellValueFactory>
</TableColumn>
<TableColumn prefWidth="81" text="NumSlices">
<cellValueFactory><PropertyValueFactory property="numSlices" />
</cellValueFactory>
</TableColumn>
</columns>
</TableView>
</children>
</fx:root>

这是类(class):

摘要.java

package sum;

import java.io.IOException;
import java.net.URL;
import java.util.Comparator;
import java.util.ResourceBundle;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.AnchorPane;
import javafx.util.Callback;

public class Summary extends AnchorPane implements Initializable {
@FXML
private TableView<SummaryElement> table;
//ObservableList<SummaryElement> data = FXCollections.observableArrayList();
public Summary() {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(
"/res/summary.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}

@SuppressWarnings("unchecked")
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
ObservableList<SummaryElement> data = table.getItems();
data.add(new SummaryElement("2009/12/12", "T1", 23));
data.add(new SummaryElement("2006/12/12", "T1", 2));
table.getItems().add(new SummaryElement("2011/12/12", "T2",23));
TableColumn<SummaryElement,SummaryElement> btnCol = new TableColumn<>("btnCol");
btnCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<SummaryElement,SummaryElement>,ObservableValue<SummaryElement>>(){

@SuppressWarnings("rawtypes")
@Override
public ObservableValue<SummaryElement> call(
javafx.scene.control.TableColumn.CellDataFeatures<SummaryElement, SummaryElement> features) {
return new ReadOnlyObjectWrapper(features.getValue());
}

});
btnCol.setComparator(new Comparator<SummaryElement>() {
@Override public int compare(SummaryElement p1, SummaryElement p2) {
return p1.getLabel().compareTo(p2.getLabel());
}
});
btnCol.setCellFactory(new Callback<TableColumn<SummaryElement, SummaryElement>, TableCell<SummaryElement, SummaryElement>>() {
@Override public TableCell<SummaryElement, SummaryElement> call(TableColumn<SummaryElement, SummaryElement> btnCol) {
return new TableCell<SummaryElement, SummaryElement>() {
final Button button = new Button(); {
button.setMinWidth(130);
}
@Override public void updateItem(final SummaryElement SummaryElement, boolean empty) {
super.updateItem(SummaryElement, empty);
if (SummaryElement != null) {
button.setText("Buy coffee" + SummaryElement.getLabel());
button.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent event) {
// actionTaken.setText("Bought " + SummaryElement.getLikes().toLowerCase() + " for: " + SummaryElement.getFirstName() + " " + SummaryElement.getLastName());
}
});
} else {
setGraphic(null);
}
}
};
}
});
table.getColumns().add(btnCol);

}
}

以及一个调用应用程序的类:

package sum;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;



public class CustomDemo extends Application {
@Override
public void start(Stage stage) throws Exception {
Summary custom = new Summary();

Scene scene = new Scene(custom);
stage.setScene(scene);
stage.show();
}
/**
* The main() method is ignored in correctly deployed JavaFX application.
* main() serves only as fallback in case the application can not be
* launched through deployment artifacts, e.g., in IDEs with limited FX
* support. NetBeans ignores main().
*
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}

summaryElement 就是这样:

package sum;

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
public class SummaryElement {
private final SimpleStringProperty date = new SimpleStringProperty("");
private final SimpleStringProperty label = new SimpleStringProperty("");
private final IntegerProperty numSlices = new SimpleIntegerProperty(this, "numSlices", 0);



public SummaryElement(String date, String label, int numSlices){
setDate(date);
setLabel(label);
setNumSlices(numSlices);


}
public void setDate(String gDate){
date.set(gDate);
}
public String getDate(){
return date.get();
}
public void setLabel(String gLabel){
label.set(gLabel);
}
public String getLabel(){
return label.getValue();
}
public void setNumSlices(int gNumSlices){
numSlices.set(gNumSlices);
}
public int getNumSlices(){
return numSlices.get();
}

}

当我在 Eclipse 中进行调试时,在 Summary.java 中的第 77 行设置断点,我可以在“变量”窗口中看到它加载了 SummaryElement,但是当我单击进入下一步时,它会显示错误消息:“未找到源”。

然后它会在 btnCol 上显示没有任何按钮的 tableview,即使它进入 if (SummaryElement != null) 几次。

如果这篇文章看起来像是一篇帮助吸血鬼的文章,我很抱歉,但我尝试了 3 天,但没有成功。

最佳答案

TableCellupdateItem(...) 方法中,在 if (SummaryElement != null) 子句中,您需要setGraphic(按钮);

关于JavaFX 表格 View 不显示按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24087085/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com