JavaFx table creation and data import

Using JavaFx to implement dynamic table output of three page replacement algorithms

package pageRA;

import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.Scene;
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.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class GUI extends Application {
Button bt = new Button("Refresh data");
TableView<Data1> tbv1 = new TableView<>();// Table 1, used to display page access sequence and page number
static TableView<Data2> tbv2 = new TableView<>();// Table 2, used for the number of page frames and the hit rate of each algorithm

static HBox hbox = new HBox();
VBox vbox = new VBox();
VBox vbox1 = new VBox();

@SuppressWarnings("unchecked")
@Override
public void start(Stage primaryStage) throws Exception {
//Set button to refresh data
bt.setOnAction((ActionEvent) -> {
// Create data objects and add them to the table view
// Use int[] and String[] to receive respectively
// Use the dataList variable of type ObservableList<Data1> to store data.
Address ad = new Address();
PageAccess pa = new PageAccess(ad);
//Storage table 1 data
int addressColum[] = ad.address;
String pageAddressColumn[] = pa.pageAccessStr;
\t\t\t
ObservableList<Data1> dataList1 = FXCollections.observableArrayList();
for (int i = 0; i < ad.address.length; i + + ) {
Data1 d1 = new Data1(i, addressColum[i], pageAddressColumn[i]);
dataList1.add(d1);
}
tbv1.setItems(dataList1);
\t\t\t
//Storage table 2 data
ObservableList<Data2> dataList2 = FXCollections.observableArrayList();
for(int i=4;i<=40;i + + ) {
double hit[] = new double[3];//Storage the hit rates of the three algorithms
PageReAlgor pra = new PageReAlgor(i, pa);
hit[0] = pra.opt(i, pa);
hit[1] = pra.fifo(i, pa);
hit[2] = pra.lru(i, pa);
Data2 data2 = new Data2(i, hit[0], hit[1], hit[2]);
dataList2.add(data2);
}
tbv2.setItems(dataList2);
\t\t\t

});

//Set table 1
tbv1.setEditable(false);//Not editable
TableColumn<Data1, Integer> id = new TableColumn<>("ID");
id.setMinWidth(100);
id.setCellValueFactory(new PropertyValueFactory<>("id"));
TableColumn<Data1, Integer> address = new TableColumn<>("Address");
address.setMinWidth(100);
address.setCellValueFactory(new PropertyValueFactory<>("address"));
TableColumn<Data1, String> pageAddress = new TableColumn<>("PageAddress");
pageAddress.setMinWidth(150);
pageAddress.setCellValueFactory(new PropertyValueFactory<>("pageAddress"));

//Add table columns to the table view
tbv1.getColumns().addAll(id, address, pageAddress);

//Set table 2
setData2();
//Put the component into the VBox
vbox.setSpacing(5);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(bt, tbv1);
vbox1.setSpacing(5);
vbox1.setPadding(new Insets(38, 0, 0, 10));
vbox1.getChildren().addAll(tbv2);
hbox.setSpacing(10);
hbox.setPadding(new Insets(10, 5, 5, 10));
hbox.getChildren().addAll(vbox,vbox1);

Scene scene = new Scene(hbox);
primaryStage.setTitle("GUI Sample");
primaryStage.setWidth(900);
primaryStage.setHeight(500);

primaryStage.setScene(scene);
primaryStage.show();

}

public static void main(String[] args) {
launch(args);
}

public static class Data1 {//Two columns
private final int address;
private final String pageAddress;
private final int id;

public Data1(int id, int address, String pageAddress) {
this.id = id;
this.address = address;
this.pageAddress = pageAddress;
}

//There must be data in the return value table
public int getAddress() {
return address;
}

public String getPageAddress() {
return pageAddress;
}

public int getId() {
return id;
}

}

public static class Data2 {
private int pageNum;//Number of page frames
private double optR;// hit rate
private double fifoR;
private double lruR;
public int getPageNum() {
return pageNum;
}

public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}

public double getOptR() {
return optR;
}

public void setOptR(double optR) {
this.optR = optR;
}

public double getFifoR() {
return fifoR;
}

public void setFifoR(double fifoR) {
this.fifoR = fifoR;
}

public double getLruR() {
return lruR;
}

public void setLruR(double lruR) {
this.lruR = lruR;
}


public Data2(int pageNum, double optR, double fifoR, double lruR) {
super();
this.pageNum = pageNum;
this.optR = optR;
this.fifoR = fifoR;
this.lruR = lruR;
}

\t\t
}

@SuppressWarnings("unchecked")
public static void setData2() {
//Set header
tbv2.setEditable(false);//Not editable
TableColumn<Data2, Integer> pageNumCol = new TableColumn<>("number of page frames");
pageNumCol.setMinWidth(100);
pageNumCol.setCellValueFactory(new PropertyValueFactory<>("pageNum")); // To correspond to the variable name set by Data2
TableColumn<Data2, Double> optC = new TableColumn<>("OPT");
optC.setMinWidth(100);
optC.setCellValueFactory(cellData -> {
Double value = cellData.getValue().getOptR();
Double formattedValue = Double.parseDouble(String.format("%.3f", value));
return new ReadOnlyObjectWrapper<>(formattedValue);
});
//optC.setCellValueFactory(new PropertyValueFactory<>("optR"));
TableColumn<Data2, Double> fifoC = new TableColumn<>("FIFO");
fifoC.setMinWidth(150);
fifoC.setCellValueFactory(cellData -> {
Double value = cellData.getValue().getFifoR();
Double formattedValue = Double.parseDouble(String.format("%.3f", value));//Set the table to 3 decimal places
return new ReadOnlyObjectWrapper<>(formattedValue);
});
// fifoC.setCellValueFactory(new PropertyValueFactory<>("fifoR"));
TableColumn<Data2, Double> lruC = new TableColumn<>("LRU");
lruC.setMinWidth(150);
lruC.setCellValueFactory(cellData -> {
Double value = cellData.getValue().getLruR();
Double formattedValue = Double.parseDouble(String.format("%.3f", value));
return new ReadOnlyObjectWrapper<>(formattedValue);
});
// lruC.setCellValueFactory(new PropertyValueFactory<>("lruR"));

//Add table columns to the table view
tbv2.getColumns().addAll(pageNumCol, optC, fifoC, lruC);

}
}

Overall, designing a JavaFx table requires the following steps:

  1. Analyze what components are needed for the GUI and how the panels should be arranged.
  2. Design the header TableColumn and type (Data) in the TableView to be output
  3. Put the component into the panel (Pane), and then add the panel to (Stage)
  4. Design the event handler setOnAction, use ObservableList to save the data row by row in the form of Data, and then input it into the table through the TableView.setItems() method.

The effect is as shown in the figure:

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Algorithm skill tree Home page Overview 57116 people are learning the system