来自PostgreSQL的Json的JavaFX Tableview集合问题

时间:2018-01-26 19:44:05

标签: java json postgresql javafx

我刚刚放弃了JavaFX应用程序开发,因为我是JavaFX的新手。

我无法将Json数据映射到TableView。 这是截图:

Json应用程序截图

enter image description here

该列表显示2个条目,但不显示任何数据。

Json数据:

 {"userid":5126,"datetime":"2018-01-22T08:42:38","flag":"0"}, 
 {"userid":5126,"datetime":"2018-01-20T17:12:37","flag":"0"}

数据Bean:

package com.JsonDemo;

import java.io.Serializable;
import java.sql.Timestamp;

public class attTrans implements Serializable {
    Integer userid;
    Timestamp datetime;
    Integer flag;

    public Integer getUserid() {
        return userid;
    }

    public void setUserid(Integer userid) {
        this.userid = userid;
    }

    public Timestamp getDatetime() {
        return datetime;
    }

    public void setDatetime(Timestamp datetime) {
        this.datetime = datetime;
    }

    public Integer getFlag() {
        return flag;
    }

    public void setFlag(Integer flag) {
        this.flag = flag;
    }
}

JsonUtil类:

package com.JsonDemo;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import java.io.IOException;

public class JsonUtil {

    private static ObjectMapper mapper;

    static {
        mapper = new ObjectMapper();
    }

    public static String convertJavatoJson(Object object) {
        String jsonResult = "";

        try {
            jsonResult = mapper.writeValueAsString(object);
        } catch (JsonGenerationException e) {
            System.out.print("JSON convertion exception occured: " + e.getMessage());
        } catch (JsonMappingException e) {
            System.out.print("JSON convertion exception occured: " + e.getMessage());
        } catch (IOException e) {
            System.out.print("JSON convertion exception occured: " + e.getMessage());
        }

        return jsonResult;
    }

    public static <T> T convertJsontoJava(String jsonString, Class<T> cls) {
        T result = null;
        try {
            result = mapper.readValue(jsonString, cls);
        } catch (JsonMappingException e) {
            System.out.print("Java convertion exception occured: " + e.getMessage());
        } catch (JsonParseException e) {
            System.out.print("Java convertion exception occured: " + e.getMessage());
        } catch (IOException e) {
            System.out.print("Java convertion exception occured: " + e.getMessage());
        }

        return result;
    }
}

以下是MainController类,它有一些错误或歧义:

package com.JsonDemo;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;

public class MainController {

    @FXML
    private TextField txtUserid;

    @FXML
    private Button btnGo;

    @FXML
    private Button btnQuit;

    @FXML
    private TableColumn<attTrans, Integer> colId;

    @FXML
    private TableColumn<attTrans, Timestamp> colTime;

    @FXML
    private TableColumn<attTrans, Integer> colStatus;

    @FXML
    private TableView<attTrans> tableUsers;

    @FXML
    private void btnQuitPressed(ActionEvent event) {
        System.exit(0);
    }

    @FXML
    private void btnGoPressed() throws IOException {

        String usid = txtUserid.getText();
        System.out.println("Before: {" + usid + "}");
        if (usid.isEmpty()) {
            usid = "6249";
        }
        System.out.println("After:  {" + usid + "}");
        String url = "http://localhost:3000/att_trans?userid=eq." + usid + "&limit=2";
        URL urlObj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) urlObj.openConnection();
        con.setRequestMethod("GET");
        con.setRequestProperty("User-Agent", "Mozilla/5.0");
        int responseCode = con.getResponseCode();
        System.out.println("Response Code: " + responseCode);
        BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));

        String inputline;
        ArrayList<attTrans> jsonList = new ArrayList<attTrans>();
        ObservableList<attTrans> tabledata = FXCollections.observableArrayList();

        while ((inputline = br.readLine()) != null) {
            attTrans att1 = JsonUtil.convertJsontoJava(inputline.replace("[", "").replace("]", ""), attTrans.class);
            jsonList.add(att1);
            tabledata.add(att1);
        }
        br.close();

        System.out.println("Array Size: " + jsonList.size());
        System.out.println("ObservableList Size: " + tabledata.size());

        Iterator itr = jsonList.iterator();

        while (itr.hasNext()) {
            attTrans at = (attTrans) itr.next();
            System.out.println("UserID: " + at.getUserid() + " Timestamp: " + at.getDatetime().toString() + " Status: " + at.getFlag());
            System.out.println(tabledata.toString());
        }
        tableUsers.setItems(tabledata);
        tableUsers.getColumns().setAll(colId,colTime,colStatus);
    }
}

这只是一个主类:

package com.JsonDemo;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

import java.io.IOException;

public class Main extends Application{

    public static void main(String[] args) throws IOException {
        Application.launch(args);

    }

    @Override
    public void start(Stage stage) throws Exception {
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(Main.class.getResource("MainWindow.fxml"));
            BorderPane root = (BorderPane) loader.load();
            Scene scene = new Scene(root);
            stage.setScene(scene);
            stage.setTitle("User ID Checker");
            //stage.setResizable(false);
            stage.sizeToScene();
            stage.show();
        } catch (IOException e) {
            e.getMessage();
        }
    }

}

MainWindow.fxml

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.ToolBar?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.text.Font?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.anostic.MainController">
   <top>
      <ToolBar prefHeight="40.0" prefWidth="200.0" BorderPane.alignment="CENTER">
        <items>
            <Label text="User ID:">
               <font>
                  <Font name="System Bold" size="13.0" />
               </font>
               <padding>
                  <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
               </padding>
            </Label>
            <TextField fx:id="txtUserid" />
          <Button fx:id="btnGo" mnemonicParsing="false" onAction="#btnGoPressed" text="Go" />
        </items>
      </ToolBar>
   </top>
   <bottom>
      <ToolBar prefHeight="40.0" prefWidth="200.0" BorderPane.alignment="CENTER">
        <items>
          <Button fx:id="btnQuit" mnemonicParsing="false" onAction="#btnQuitPressed" text="Quit" />
        </items>
      </ToolBar>
   </bottom>
   <center>
      <TableView fx:id="tableUsers" BorderPane.alignment="CENTER">
        <columns>
          <TableColumn fx:id="colId" prefWidth="127.0" text="ID" />
          <TableColumn fx:id="colTime" prefWidth="245.0" text="TImeStamp" />
            <TableColumn fx:id="colStatus" minWidth="0.0" prefWidth="154.0" text="Status" />
        </columns>
      </TableView>
   </center>
</BorderPane>

0 个答案:

没有答案