简体   繁体   English

JAVAFX表格栏

[英]JAVAFX tablecolumn

I am trying to get my javafx(using scene builder) to read in a list of employee id and name. 我试图让我的javafx(使用场景生成器)读取员工ID和姓名的列表。 Most of it works, but I want the names that have been added to the list to show when I click the show button. 大多数功能都可以,但是我希望单击“显示”按钮时显示已添加到列表中的名称。 Also, I created a tableview in scene builder and I want that window to pop up when the "show" button is clicked. 另外,我在场景生成器中创建了一个表格视图,我希望当单击“显示”按钮时弹出该窗口。 Is the only way to do this with a obeservablelist? 用observablelist做到这一点的唯一方法是吗? How can accomplish this. 如何做到这一点。 My code so far is below. 到目前为止,我的代码如下。 I didn't attach the employee class or the primary fxml file, but i Can if it helps. 我没有附加员工类或主要的fxml文件,但我可以,如果有帮助。

enter
package queue5230;

import java.io.IOException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Locale;
import java.util.Queue;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.stage.Modality;
import javafx.stage.Stage;



public class FXMLDocumentController {

    Queue<Employee> empQu = new LinkedList<>();

    String name;
    String startTime;
    String endTime;
    int id;

    @FXML
    private TextField employeeId;

    @FXML
    private TextField employeeName;

    @FXML
    private Button insertButton;

    @FXML 
    private Button removeButton;

    @FXML 
    private Button showButton;

    @FXML 
    private TableView<Employee> showTable;

    @FXML
    private TableColumn<Employee, String> idField;

    @FXML
    private TableColumn<Employee, String> nameField;

    @FXML
    private void handleButtonAction(ActionEvent event) {

    }

    @FXML
    private void handleTextInsert(ActionEvent event) throws IOException{

        if(insertButton == event.getSource())
        {
         //get values entered   
         id = Integer.parseInt(employeeId.getText());
         name = employeeName.getText();

         //get time entered
         Date d = new Date();

         //get the date format
         startTime = new SimpleDateFormat("HH:mm:ss", Locale.ENGLISH).format(d);

         //Create employee object
         Employee emp = new Employee(id, name, startTime);

         //ad object to queue
         empQu.add(emp);

        }else if(removeButton == event.getSource()){

            Employee empr = empQu.remove();

            Date end = new Date();

            endTime = new SimpleDateFormat("HH:mm:ss", Locale.ENGLISH).format(end);

            String elapse = getElapsedTime(empr.getTime(), endTime);

        }else if(showButton == event.getSource()){


            Stage stage;
            Parent root;
            String str = "ID  NAME  TIME START\n\n";

            Iterator<Employee> it = empQu.iterator();

            while(it.hasNext()){

                //str += it.next() + "\n";

            }


            stage = new Stage();
            root = FXMLLoader.load(getClass().getClassLoader().getResource("Show.fxml"));
            stage.setScene(new Scene(root));
            stage.initModality(Modality.APPLICATION_MODAL);
            stage.showAndWait();


        }


    }

    public String getElapsedTime(String starting, String ending)
    {
        String startToken[] = starting.split(":");
        String endToken[] = ending.split(":");

        int hours = Math.abs(Integer.parseInt(startToken[0])-Integer.parseInt(endToken[0]));
        int minutes = Math.abs(Integer.parseInt(startToken[1])-Integer.parseInt(endToken[1]));
        int seconds = Math.abs(Integer.parseInt(startToken[2])-Integer.parseInt(endToken[2]));

        return hours + ":" + minutes + ":" + seconds;
    }


    public void initialize() {



    }    

}

code here 这里的代码

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="queue5230.FXMLDocumentController">
   <children>
      <Pane layoutX="31.0" layoutY="22.0" prefHeight="361.0" prefWidth="540.0">
         <children>
            <TableView fx:id="showTable" layoutX="101.0" layoutY="11.0" prefHeight="340.0" prefWidth="401.0">
              <columns>
                <TableColumn fx:id="idField" prefWidth="75.0" text="ID" />
                <TableColumn fx:id="nameField" prefWidth="75.0" text="Name" />
              </columns>
            </TableView>
         </children>
      </Pane>
   </children>
</AnchorPane>

fine in this page https://docs.oracle.com/javafx/2/ui_controls/table-view.htm , there are example of how to show a object in the TableView, but you can define in the fxml: 在此页面上很好https://docs.oracle.com/javafx/2/ui_controls/table-view.htm ,其中有示例如何在TableView中显示对象的示例,但是您可以在fxml中进行定义:

<TableColumn fx:id="nameField" prefWidth="75.0" text="Name" >
     <cellValueFactory>
         <javafx.scene.control.cell.PropertyValueFactory property="the field name"/> 
     </cellValueFactory>
</TableColumn>

your fxml code, it will look like this: 您的fxml代码,如下所示:

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

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="queue5230.FXMLDocumentController">
   <children>
      <Pane layoutX="31.0" layoutY="22.0" prefHeight="361.0" prefWidth="540.0">
         <children>
            <TableView fx:id="showTable" layoutX="101.0" layoutY="11.0" prefHeight="340.0" prefWidth="401.0">
              <columns>
                <TableColumn fx:id="idField" prefWidth="75.0" text="ID" >
                      <cellValueFactory>
                        <javafx.scene.control.cell.PropertyValueFactory property="id"/> 
                      </cellValueFactory>
                </TableColumn>
                <TableColumn fx:id="nameField" prefWidth="75.0" text="Name" >
                    <cellValueFactory>
                       <javafx.scene.control.cell.PropertyValueFactory property="name"/> 
                    </cellValueFactory>
                </TableColumn>
              </columns>
            </TableView>
         </children>
      </Pane>
   </children>
</AnchorPane>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM