JavaFX中TableView使用

标签: javaFX
2人阅读 评论(0) 收藏 举报
分类:

- TableView初始化代码以及在表格中使用进度条和复选框 -列表的初始化以及导入

package controlller;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.ResourceBundle;

import bean.UserRow;
import comm.DataManagement;
import comm.User;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.ProgressBarTableCell;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;

@SuppressWarnings("rawtypes")
public class TableViewController extends TableView implements Initializable {
    @FXML
    private TableView<UserRow> qqInfoTable;
    private static ObservableList<UserRow> users;
    private static HashMap<String, UserRow> cache = new HashMap<>();

    @Override
    public void initialize(URL url, ResourceBundle source) {
        // qqInfoTable.getSelectionModel().setCellSelectionEnabled(true);
        dragHandler();
        initTable();
        ControllerManager.add("qqInfoTable", this);
    }

    @SuppressWarnings("unchecked")
    private void initTable() {
        // System.out.println(this==qqInfoTable);
        String[] col_Name = { "是否完成", "线程状态", "QQ", "红包状态", "当前G分", "登陆G分", "收益", "进度" };
        String[] col_Property = { "choice", "threadStatus", "qq", "packetStatus", "nowScore", "oldScore", "profit",
                "progress" };
        //设置复选框组件
        TableColumn col = new TableColumn(col_Name[0]);
        col.setEditable(true);
        col.setMinWidth(115);
        col.setCellValueFactory(new PropertyValueFactory<UserRow, CheckBox>(col_Property[0]));
        qqInfoTable.getColumns().add(col);
        for (int i = 1; i < col_Name.length; ++i) {
            col = new TableColumn(col_Name[i]);
            col.setMinWidth(115);
            col.setCellValueFactory(new PropertyValueFactory<>(col_Property[i]));
            if (i == 7) {
                col.setCellFactory(ProgressBarTableCell.forTableColumn());
            }

            qqInfoTable.getColumns().add(col);
        }
    }
    //在表格中实现拖放文件导入
    void dragHandler() {
        qqInfoTable.setOnDragOver((e)->{
            e.acceptTransferModes(TransferMode.MOVE);
        });
        qqInfoTable.setOnDragDropped((e)->{
            Dragboard board=e.getDragboard();
            if (!board.hasFiles()) {
                return;
            }
            String url=board.getFiles().get(0).getAbsolutePath();
            try {
                DataManagement.loadUsers(url);
            } catch (IOException e1) {
                ControllerManager.appendCmd(null, "加载文件错误");
            }
            insertIntoTable();
        });
    }
    //把数据插入表格
    void insertIntoTable() {
        users = FXCollections.observableArrayList();
        HashSet<User> list = DataManagement.getAllUsers();
        Iterator<User> it = list.iterator();
        while (it.hasNext()) {
            User user = it.next();
            users.add(new UserRow(user));
        }
        qqInfoTable.setItems(users);
    }

    TableView<UserRow> getTable() {
        return qqInfoTable;
    }
    //在表格中查找项目
    private UserRow find_(User user) {
        UserRow now = users.stream().filter(e -> user.equals(e)).reduce((res, next) -> res = next).get();
        cache.put(user.getQq(), now);
        return now;
    }
}

**

-TableView中使用的bean映射

**

public class User {

    protected final SimpleStringProperty qq=new SimpleStringProperty("no Login");
    protected final SimpleIntegerProperty oldScore=new SimpleIntegerProperty(0);
    protected final SimpleIntegerProperty nowScore=new SimpleIntegerProperty(0);
    protected final SimpleStringProperty nickName=new SimpleStringProperty("no Login");
    protected final SimpleIntegerProperty profit=new SimpleIntegerProperty(0);

    public User() {

    }
    protected User(User user) {
        this.qq.bind(user.qq);
        this.oldScore.bind(user.oldScore);
        this.nowScore.bind(user.nowScore);
        this.nickName.bind(user.nickName);
        this.profit.bind(user.profit);
    }
    public String getQq() {
        return qq.get();
    }
    public int getOldScore() {
        return oldScore.get();
    }
    public int getNowScore() {
        return nowScore.get();
    }

    public void setOldScore(int oldScore) {
        this.oldScore.set(oldScore);
    }

    public void setNowScore(int nowScore) {
        this.nowScore.set(nowScore);
    }
    @Override
    public int hashCode() {
        return qq.hashCode();
    }

    @Override
    public boolean equals(Object b) {
        return getQq().equals(((User)b).getQq());
    }

    public void computeProfit() {
        setProfit(nowScore.get()-oldScore.get());
    }

    public int getProfit() {
        return profit.get();
    }

    public void setProfit(int profit) {
        this.profit.set(profit);
    }
}
public class UserRow extends User{
    private static int count=1;
    private final SimpleStringProperty threadStatus=new SimpleStringProperty("no Login");
    private final SimpleStringProperty packetStatus=new SimpleStringProperty("no Login");
    private final SimpleStringProperty nickName=new SimpleStringProperty("no Login");
    private final SimpleDoubleProperty progress=new SimpleDoubleProperty(0);
    //private final SimpleBooleanProperty choice=new SimpleBooleanProperty(false);
    private final CheckBox choice=new CheckBox();
    private int sequence;

    {
        setSequence(count++);
    }

    public UserRow(User user){
        super(user);
        choice.selectedProperty().addListener(e->{
            boolean selected=choice.isSelected();
            if (selected) {
                DataManagement.addSelected(user);
            }else {
                DataManagement.removeSelected(user);
            }
        });;
    }


    public int getSequence() {
        return sequence;
    }

    public void setSequence(int sequence) {
        this.sequence = sequence;
    }

    public String getThreadStatus() {
        return threadStatus.get();
    }

    public SimpleStringProperty threadStatusProperty() {
        return threadStatus;
    }

    public String getPacketStatus() {
        return packetStatus.get();
    }

    public SimpleStringProperty packetStatusProperty() {
        return packetStatus;
    }

    public SimpleIntegerProperty oldScoreProperty() {
        return oldScore;
    }
    public String getQq() {
        return qq.get();
    }


    public String getNickName() {
        return nickName.get();
    }

    public double getProgress() {
        return progress.get();
    }
    public SimpleDoubleProperty progressProperty() {
        return progress;
    }
    public void setProgress(double progress) {
        this.progress.set(progress);
    }

    public void setThreadStatus(String threadStatus) {
        this.threadStatus.set(threadStatus);
    }

    public void setPacketStatus(String packetStatus) {
        this.packetStatus.set(packetStatus);
    }

    public SimpleIntegerProperty nowScoreProperty() {
        return nowScore;
    }

    public SimpleIntegerProperty profitProperty() {
        return profit;
    }
    /**
     * @return the choice
     */
    public CheckBox getChoice() {
        return choice;
    }

    public void allStatucSet(String status) {
        threadStatus.set(status);
        packetStatus.set(status);
    }

}

特别需要注意类似public SimpleIntegerProperty nowScoreProperty() ;此种类型的方法”属性名+Property”,属性绑定需要getter和此类方法

-效果截图
—–这里写图片描述

查看评论

JavaFX中TableView的使用(结合scene builder)

以下图运行效果为例: 1.创建TableView和TableColumn直接使用scene builder拖拽控件。2.在控制器中编写数据填充类class StudentInformation { ...
  • qq_25965053
  • qq_25965053
  • 2017-03-01 09:25:06
  • 895

JavaFX之TableView的使用

TableView,算是一个很重要的控件,几乎随处可见,而且功能强大,数据展示效果良好。所以,在JavaFX中,我们自然而然也应该学习一下TableView的使用。 下面我们先看看TableVi...
  • ml3947
  • ml3947
  • 2013-12-05 17:58:02
  • 6027

javafx tableview 示例

javafx tableview 示例  public void initTableBox(TableView listBox) {         listBox.setEditable...
  • csdn_412849834
  • csdn_412849834
  • 2017-01-16 11:22:53
  • 806

Using JavaFX UI Controls 12 Table View

原文链接地址:http://docs.oracle.com/javafx/2/ui_controls/table-view.htm#CJAGAAEE 在这一章,你将学习如:添加一个表格表、数据填充...
  • w605283073
  • w605283073
  • 2015-06-12 22:12:50
  • 1334

JAVAFX 在TableView里面使用CheckBox

艰辛啊,在网上找了不少关于JavaFx2.0 中在表格中使用单选框的例子。其实,是...
  • mexel310
  • mexel310
  • 2014-04-22 14:12:21
  • 3736

JavaFX TableView使用和样式设置

1、新建fxml界面文件,里面就放置一个TableView 对应的Controller为TableViewTestController,css配置文件为TableViewTestCss.css,表格...
  • u010889616
  • u010889616
  • 2018-01-07 23:51:59
  • 957

JavaFX:禁止TableView的列拖拽功能

JavaFx封装的
  • u012994553
  • u012994553
  • 2014-04-14 10:47:48
  • 1419

javafx TableView中的TableColumn如何填充的自带类,以及编辑单元格

在javafx中TableView中有TableColumn,即每一列,对于每一列中每个单元怎么填充,javafx的官方文档提供了两个实现类 [java] view plain copy ...
  • xiaozaq
  • xiaozaq
  • 2017-01-19 17:51:11
  • 2582

JavaFX学习之道:JavaFX之TableView

TableView表     TableColumn列  构建一个表主要有TableView,TableColumn,ObservableList,Bean。  添加列table.getColu...
  • u011511429
  • u011511429
  • 2014-07-29 18:43:12
  • 5404
    个人资料
    等级:
    访问量: 1150
    积分: 62
    排名: 162万+
    文章存档