iT邦幫忙

DAY 11
1

java菜鳥的學習筆記系列 第 11

灰煞煞之Java FX 2.0-使用TableView-Part5

  • 分享至 

  • xImage
  •  

今天分享使用java FX UI中的TableView,因為這項UI在工作上常常會用到,用來呈現資料最基本的原件,從官方網頁的說明http://docs.oracle.com/javafx/2/ui_controls/table-view.htm#CJAGAAEE,其實真的不難,我自己練習的是把資料庫的資料抓出來,用TableView Show出來,其中只有setCellValueFactory以及PropertyValueFactory需要注意一下,個人覺得比較難懂一點點。
FXBean-FPR

package data;

import javafx.beans.property.SimpleStringProperty;

/**
 *
 * @author joombuopre
 */
public class FPR {
    private SimpleStringProperty fprNo;
    private SimpleStringProperty fddrNo;

    public FPR(String fpr, String fddr){
        this.fprNo=new SimpleStringProperty(fpr);
        this.fddrNo=new SimpleStringProperty(fddr);
    }
    
    public String getFddrNo() {
        return fddrNo.get();
    }

    public void setFddrNo(String fddrNo) {
       this.fddrNo=new SimpleStringProperty(fddrNo);
    }

    /**
     * @return the fprNo
     */
    public String getFprNo() {
        return fprNo.get();
    }

    /**
     * @param fprNo the fprNo to set
     */
    public void setFprNo(String fprNo) {
        this.fprNo=new SimpleStringProperty(fprNo);
    }
    
}

TableView

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package myfx3;

import dao.GetDAOResultSet;
import data.FPR;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

/**
 *
 * @author joombuopre
 */
public class MyFX3 extends Application {
    public static final String sql="Select no, fddrno from fpr_fddr where no like '%FPR%' order by no desc";
    @Override
    public void start(Stage primaryStage) {
      
        Scene scene = new Scene(new Group());
        TableView table=new TableView(); //create TableView instance
        TableColumn t1=new TableColumn(); //add column
        TableColumn t2=new TableColumn(); // add column
        
        t1.setText("FPR"); // set column name
        t1.setMinWidth(150); //set column width
        t1.setCellValueFactory(new PropertyValueFactory<FPR, String>("fprNo"));
        //setCellValueFactory receive ObservableValue object, and PropertyValueFactory returns "fprNo" value.
        t2.setText("FDDR"); // set column name
        t2.setMinWidth(250); //set column width
        t2.setCellValueFactory(new PropertyValueFactory<FPR, String>("fddrNo"));
        //setCellValueFactory receive ObservableValue object, and PropertyValueFactory returns "fddrNo" value.
        table.setEditable(true);
        table.getColumns().addAll(t1, t2); // add column header
        try {        
            table.setItems(getTableContent()); //add tablecontent in form of ObservableList by calling setItems method
        } catch (SQLException ex) {
            Logger.getLogger(MyFX3.class.getName()).log(Level.SEVERE, null, ex);
        }
        ((Group)scene.getRoot()).getChildren().add(table);
        
        primaryStage.setTitle("My TableView");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
     public ObservableList<FPR> getTableContent() throws SQLException{  
         ObservableList<FPR> tc= FXCollections.observableArrayList();           
         GetDAOResultSet dao=new GetDAOResultSet();
         ResultSet rs=dao.getResultSet(sql); // get data from database
         while(rs.next()){
             FPR fpr=new FPR(rs.getString("no"), rs.getString("fddrno"));
             tc.add(fpr); // add to ObservableList
         }
         return tc;
     }
    /**
     * The main() method is ignored in correctly deployed JavaFX application.
     * main() serves only as fallback in case the application can not be
     * launched through deployment artifacts, e.g., in IDEs with limited FX
     * support. NetBeans ignores main().
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
}

上一篇
灰煞煞之Java FX 2.0-產生PieChart與LineChart-Part4
下一篇
灰煞煞之Java FX 2.0-使用ProgressBar-Part 6(失敗,未完體)
系列文
java菜鳥的學習筆記28
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言