提问人:Yarin0600 提问时间:2/10/2023 更新时间:2/11/2023 访问量:86
Javafx 如何每次将新元素添加到新列中的同一行 tableview [已关闭]
Javafx how to add everytime new element to the same row in new column tableview [closed]
问:
我有 TableView,我的目的是只有一行 [不包括每个列的列标题]。在第一行的每个单元格中。我会有 ProgressIndicator。
现在,我的目的是每次有人按“添加”时,它都会转到 OnAddButtonPressed 函数操作并添加新的列,并且在这个特定列的第一行中刚刚添加一个新的 progressIndicator。
在我现在的代码中,发生的事情是每次有人按下“ADD”时,发生的情况是之前的所有 ProgressIndicator 都移动到新列,行数增加,所有 ProgressIndicator 从 earalier 列被删除并被一个接一个地“推送”到新列。
@FXML
private void OnAddButtonPressed(ActionEvent event) {
int currentCPUTimeRequestedInteger;
String currentCPUTimeRequested = this.addTextField.getText();
if(currentCPUTimeRequested.isEmpty() || currentCPUTimeRequested.length() > 1 || !Character.isDigit(currentCPUTimeRequested.toCharArray()[0]) || currentCPUTimeRequested.toCharArray()[0] == '0')
{
PopAddButtonError();
return;
}
if(m_NumOfCols == MAX_NUM_OF_COLS - 1)
{
this.addButton.setDisable(true);
this.addTextField.setDisable(true);
this.enterCPUTimeLabel.setDisable(true);
}
if(firstTimePressedAdd == false)
{
this.firstTimePressedAdd = true;
this.startButton.setDisable(false);
this.listOfProcesses = new LinkedList<Process>();
}
currentCPUTimeRequestedInteger = Integer.parseInt(currentCPUTimeRequested);
this.addTextField.setText("");
AddNewColToTable(currentCPUTimeRequestedInteger);
}
private void AddNewColToTable(int CPUTimeRequested)
{
Process newProcess = new Process(CPUTimeRequested);
this.listOfProcesses.add(newProcess);
TableColumn newCol = new TableColumn("P" + newProcess.GetId());
newCol.setCellValueFactory(new PropertyValueFactory<Process, String>("progressIndicator"));
ObservableList<Process> data = FXCollections.observableArrayList();
data.add(newProcess);
this.tableOfProcesses.getColumns().add(newCol);
this.tableOfProcesses.getItems().addAll(data);
this.tableOfProcesses.refresh();
this.m_NumOfCols++;
}
我的目的是让它像这张照片一样的下一个方式:在这里输入图像描述
谢谢你的帮助!
答:
2赞
DaveB
2/11/2023
#1
将此视为 XY 问题。
以下是主要通过 HBox 实现示例图像的方法:
class Progresses : Application() {
private var counter: Int = 0
override fun start(primaryStage: Stage) {
primaryStage.scene = Scene(createContent())
primaryStage.show()
}
private fun createContent(): Region = BorderPane().apply {
val hBox = HBox(20.0)
bottom = createButton(hBox)
center = hBox
padding = Insets(20.0)
minWidth = 400.0
minHeight = 150.0
}
private fun createButton(targetPane: Pane) = buttonOf("New Progress") { buttonAction(targetPane) }
private fun buttonAction(targetPane: Pane) {
targetPane.children += Label("P${counter++}").apply {
contentDisplay = ContentDisplay.BOTTOM
graphic = ProgressIndicator().apply {
progress = -1.0
object : AnimationTimer() {
var count = 0
override fun handle(now: Long) {
progress = (count++ / 5000.0).toDouble()
}
}.start()
}
}
}
}
fun main() = Application.launch(Progresses::class.java)
这是 Kotlin,但你应该能够弄清楚。它看起来像这样:
如果确实需要列标题样式,您可以设置标签样式以提供相同的外观和感觉,或者将其划分为 VBox(如果这样更容易)。
评论
0赞
SedJ601
2/11/2023
这是纯洁的吗?我认为答案应该写在.JavaFX
JavaFX
JavaFX
0赞
DaveB
2/11/2023
这就是纯粹的、纯粹的 JavaFX。
0赞
jewelsea
2/11/2023
它是用 Kotlin 编码的 JavaFX,只要原始海报了解哪些部分适用于他们的情况,我认为没问题。
0赞
SedJ601
2/11/2023
大概没问题。我只是想象一个初学者在理解答案时遇到问题,因为它是用 .Java/JavaFX
Kotlin
0赞
DaveB
2/11/2023
@SedJ601 我认为,如果您了解 Java,那么 Kotlin 代码通常很容易理解要点。这大概不难弄清楚,特别是如果你认识到 、 等是 的配置元素。在许多方面,我认为 Kotlin 作为答案代码比 Java for JavaFX 更容易理解,因为它更简洁。我回答中的代码是一个完整的、可运行的示例,大约有 25 行。它超越了它,因为它实际上做了一些事情。BorderPane().apply{}
bottom
center
padding
BorderPane
ProgressIndicators
评论
ListView
HBox
Labels
ProgressIndicator
Label
setGraphic()
setText()
TableView
TilePane
GridPane
ProgressBar
TableView