JavaFX在单独的选项卡中填充多个表 [英] JavaFX populating multiple tables in separate Tabs

查看:73
本文介绍了JavaFX在单独的选项卡中填充多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在TabPane的单独选项卡中填充表. 我只能像这样在第一个标签中填充一个:

I am trying to populate tables in separate tabs in TabPane. I was able to populate only the one in first tab like this:

@FXML
public void initialize(URL url, ResourceBundle rb) {
    //assert serviceChoiceBox1 != null : "fx:id=\"serviceChoiceBox1\" was not injected: check your FXML file 'ScheduleServicePane.fxml'.";

    ServiceName.setCellValueFactory(new PropertyValueFactory<Service, String>("name"));
    ServicePrice.setCellValueFactory(new PropertyValueFactory<Service, Double>("price"));
    ServiceDuration.setCellValueFactory(new PropertyValueFactory<Service, Integer>("duration"));
    TableView.setItems(List());

}


private ObservableList<Service> List(){
    ObservableList<Service> service = FXCollections.observableArrayList(serviceRepository.list());
    return service;
}

我还有两个带有表的选项卡,但是不知何故我无法用准备好的数据列表来填充它们

I have two more tabs with tables but somehow I am not able to fill them with prepared list of data that I have

我想用类似的数据填充其他标签页

I want to populate other tabs with similar data

任何人都可以帮忙吗?

我还阅读了每个选项卡的单独控制器,但是在这种情况下,这似乎就足够了.

I also read about separate Controllers for each tab but this seems to be to much in such a case.

推荐答案

在这种情况下,我会使用FilteredList.这将允许您创建原始列表的子列表".在此演示中,我在Service类中创建一个类型"字段.然后,我根据类型"过滤原始列表.您不必创建类型"字段,但如果没有,则需要查找一些内容进行过滤.

I would use FilteredList in this case. This will allow you to create "sublist" of the original list. In this demo, I create a "type" field in the Service class. I then filter the original list based on the "type". You don't have to create a "type" field, but you will need to find something to filter on if you don't.

主要:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication184 extends Application
{

    @Override
    public void start(Stage stage) throws Exception
    {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}

控制器:

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.function.Predicate;
import javafx.collections.FXCollections;
import javafx.collections.transformation.FilteredList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TabPane;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

/**
 *
 * @author blj0011
 */
public class FXMLDocumentController implements Initializable
{

    @FXML
    private TabPane tpMain;
    @FXML
    private TableView<Service> tvTab1, tvTab2;
    @FXML
    private TableColumn<Service, String> tcServiceNameTab1, tcServicePriceTab1, tcServiceDurationTab1;
    @FXML
    private TableColumn<Service, String> tcServiceNameTab2, tcServicePriceTab2, tcServiceDurationTab2;

    @FXML
    private void handleButtonAction(ActionEvent event)
    {
        System.out.println("You clicked me!");
    }

    @Override
    public void initialize(URL url, ResourceBundle rb)
    {
        tcServiceNameTab1.setCellValueFactory(new PropertyValueFactory("name"));
        tcServicePriceTab1.setCellValueFactory(new PropertyValueFactory("price"));
        tcServiceDurationTab1.setCellValueFactory(new PropertyValueFactory("duration"));

        tcServiceNameTab2.setCellValueFactory(new PropertyValueFactory("name"));
        tcServicePriceTab2.setCellValueFactory(new PropertyValueFactory("price"));
        tcServiceDurationTab2.setCellValueFactory(new PropertyValueFactory("duration"));

        //Add sanoke data
        List<Service> services = new ArrayList();
        services.add(new Service("Manicure 1", "40.0", "60", "Manicure"));
        services.add(new Service("Manicure 2", "40.0", "60", "Manicure"));
        services.add(new Service("Pedicure 1", "40.0", "60", "Pedicure"));
        services.add(new Service("Pedicure 2", "40.0", "60", "Pedicure"));

        FilteredList<Service> flTab1 = new FilteredList(FXCollections.observableArrayList(services), getManicures());
        FilteredList<Service> flTab2 = new FilteredList(FXCollections.observableArrayList(services), getPedicures());

        tvTab1.setItems(flTab1);
        tvTab2.setItems(flTab2);
    }

    Predicate<Service> getManicures()
    {
        return p -> p.getType().equals("Manicure");
    }

    Predicate<Service> getPedicures()
    {
        return p -> p.getType().equals("Pedicure");
    }

}

FXML:

FXML:

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

<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="AnchorPane" prefHeight="406.0" prefWidth="369.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.141" fx:controller="javafxapplication184.FXMLDocumentController">
   <children>
      <TabPane fx:id="tpMain" layoutX="56.0" layoutY="67.0" prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <tabs>
          <Tab text="Manicure">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                     <children>
                        <TableView fx:id="tvTab1" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                          <columns>
                            <TableColumn fx:id="tcServiceNameTab1" prefWidth="75.0" text="Service" />
                            <TableColumn fx:id="tcServicePriceTab1" prefWidth="75.0" text="Price" />
                              <TableColumn fx:id="tcServiceDurationTab1" prefWidth="75.0" text="Duration" />
                          </columns>
                        </TableView>
                     </children>
                  </AnchorPane>
            </content>
          </Tab>
          <Tab text="Pedicure">
            <content>
              <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                     <children>
                        <TableView fx:id="tvTab2" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                           <columns>
                              <TableColumn fx:id="tcServiceNameTab2" prefWidth="75.0" text="Service" />
                              <TableColumn fx:id="tcServicePriceTab2" prefWidth="75.0" text="Price" />
                              <TableColumn fx:id="tcServiceDurationTab2" prefWidth="75.0" text="Duration" />
                           </columns>
                        </TableView>
                     </children>
                  </AnchorPane>
            </content>
          </Tab>
        </tabs>
      </TabPane>
   </children>
</AnchorPane>

服务:

/**
 *
 * @author blj0011
 */
public class Service
{

    private String name;
    private String price;
    private String duration;
    private String type;

    public Service(String name, String price, String duration, String type)
    {
        this.name = name;
        this.price = price;
        this.duration = duration;
        this.type = type;
    }

    /**
     * @return the name
     */
    public String getName()
    {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name)
    {
        this.name = name;
    }

    /**
     * @return the price
     */
    public String getPrice()
    {
        return price;
    }

    /**
     * @param price the price to set
     */
    public void setPrice(String price)
    {
        this.price = price;
    }

    /**
     * @return the duration
     */
    public String getDuration()
    {
        return duration;
    }

    /**
     * @param duration the duration to set
     */
    public void setDuration(String duration)
    {
        this.duration = duration;
    }

    /**
     * @return the type
     */
    public String getType()
    {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(String type)
    {
        this.type = type;
    }

}

这篇关于JavaFX在单独的选项卡中填充多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆