我如何在一个java控制器类中使用两个TableView

时间:2018-12-11 14:45:46

标签: java javafx

我想在一个Java控制器中使用2个具有不同类的tableview。我该怎么做:

public class TransactionHistoryController implements Initializable {

@FXML
private  Label acc_no;
@FXML
private  Label amt;
@FXML
private TableView<History> withdraw_table;
@FXML
private TableColumn<History, String> accountno;
@FXML
private TableColumn<History, String> amount;
@FXML
private TableColumn<History, String> remainingamount;
@FXML
private TableColumn<History, String> dates;
@FXML
private TableColumn<History, String> times;

@FXML
private TableView<Deposit> deposit_table;
@FXML
private TableColumn<Deposit, String> deposit_accountno;
@FXML
private TableColumn<Deposit, String> deposit_amount;
@FXML
private TableColumn<Deposit, String> deposit_newamount;
@FXML
private TableColumn<Deposit, String> deposit_dates;
@FXML
private TableColumn<Deposit, String> deposit_times;

public static String ac = DashboardContoller.a1;

公共无效存款(){

        deposit_accountno.setCellValueFactory(new PropertyValueFactory<Deposit,String>("deposit_acc"));
        deposit_amount.setCellValueFactory(new PropertyValueFactory<Deposit,String>("deposit_amt"));
        deposit_newamount.setCellValueFactory(new PropertyValueFactory<Deposit,String>("new_amt"));
        deposit_dates.setCellValueFactory(new PropertyValueFactory<Deposit,String>("deposit_date"));
        deposit_times.setCellValueFactory(new PropertyValueFactory<Deposit,String>("deposit_time"));

    Connection con=null;
    PreparedStatement ps=null;
    ResultSet rs=null;
    ObservableList<Deposit> list = FXCollections.observableArrayList();

     try
    {
        Class.forName("com.mysql.jdbc.Driver");
        con=DriverManager.getConnection("jdbc:mysql://localhost:3306/udemy","root","");

        ps=con.prepareStatement("SELECT * FROM  deposit WHERE AccountNo=?");

        ps.setString(1, ac);


         rs=ps.executeQuery();
        while(rs.next())
        {

             list.addAll(new Deposit(rs.getString("AccountNo"), rs.getString("DepositAmount"), rs.getString("NewAmount"), rs.getString("Date"), rs.getString("Time")));
         JOptionPane.showMessageDialog(null,list);
        }




    }catch(Exception e)
    {
        JOptionPane.showMessageDialog(null,e);
    }

     deposit_table.setItems(list);

}

公共无效withdraw() {

accountno.setCellValueFactory(new PropertyValueFactory(“ account_number”));         amount.setCellValueFactory(new PropertyValueFactory(“ withDrawAmount”));;         missingamount.setCellValueFactory(new PropertyValueFactory(“ remainingAmount”));;         date.setCellValueFactory(new PropertyValueFactory(“ date”));         times.setCellValueFactory(new PropertyValueFactory(“ time”));

    Connection con=null;
    PreparedStatement ps=null;
    ResultSet rs=null;
    ObservableList<History> list = FXCollections.observableArrayList();

    try
    {
        Class.forName("com.mysql.jdbc.Driver");
        con=DriverManager.getConnection("jdbc:mysql://localhost:3306/udemy","root","");
        ps=con.prepareStatement("SELECT * FROM  withdraw WHERE AccountNo=?");

        ps.setString(1, ac);


         rs=ps.executeQuery();
        while(rs.next())
        {

             list.addAll(new History(rs.getString("AccountNo"), rs.getString("WithdrawAmount"), rs.getString("RemainingAmount"), rs.getString("Date"), rs.getString("Time")));
         }


    }catch(Exception e)
    {
        JOptionPane.showMessageDialog(null,e);
    }

    withdraw_table.setItems(list);

}

@Override
public void initialize(URL url, ResourceBundle rb) {

    deposit();
    withdraw();

} 

}

下面是历史记录类

公共课历史{

private String account_number;
 private String withDrawAmount;
 private String remainingAmount;
 private String date;
 private String time;

public History(String account_number, String withDrawAmount, String remainingAmount, String date, String time) {
    this.account_number = account_number;
    this.withDrawAmount = withDrawAmount;
    this.remainingAmount = remainingAmount;
    this.date = date;
    this.time = time;
}

public String getAccount_number() {
    return account_number;
}

public String getWithDrawAmount() {
    return withDrawAmount;
}

public String getRemainingAmount() {
    return remainingAmount;
}

public String getDate() {
    return date;
}

public String getTime() {
    return time;
}

}

这是存款类:

class Deposit{

private String name;
private String amount;
private String newAmount;
private String date;
private String time;

public Deposit(String name, String amount, String newAmount, String date, String time) {
    this.name = name;
    this.amount = amount;
    this.newAmount = newAmount;
    this.date = date;
    this.time = time;
}

public String getName() {
    return name;
}

public String getAmount() {
    return amount;
}

public String getNewAmount() {
    return newAmount;
}

public String getDate() {
    return date;
}

public String getTime() {
    return time;
}

}

0 个答案:

没有答案