2017-12-27 914 views
-3

我需要在有表(例如用戶)的情況下使用javafx創建項目,並在每行內使用另一個表(例如用戶的訂單)。 我希望它是可以切換的,所以我會通過點擊它來擴展一行,然後內部表將會被視爲可見的。 一些抽籤GUI〔實施例: pic for example行表中的Javafx表

+3

你嘗試過什麼到目前爲止有用嗎? – Dragonthoughts

+0

到目前爲止,我嘗試了一些exmaple,但它是隻有一張桌子裏面treetableview –

+1

也許一張樹桌更適合這個? –

回答

1

這將是我猜

import java.util.ArrayList; 
import java.util.List; 

import javafx.application.Application; 
import javafx.beans.property.SimpleStringProperty; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.geometry.Insets; 
import javafx.scene.Group; 
import javafx.scene.Node; 
import javafx.scene.Scene; 
import javafx.scene.control.Label; 
import javafx.scene.control.TableColumn; 
import javafx.scene.control.TableRow; 
import javafx.scene.control.TableView; 
import javafx.scene.control.cell.PropertyValueFactory; 
import javafx.scene.layout.VBox; 
import javafx.scene.text.Font; 
import javafx.stage.Stage; 

public class TreeTableView extends Application { 
private TableView<Person> table = new TableView<Person>(); 

public static void main(String[] args) { 
    launch(args); 
} 

@Override 
public void start(Stage stage) { 
    Scene scene = new Scene(new Group()); 
    stage.setTitle("Table View Sample"); 
    stage.setWidth(450); 
    stage.setHeight(500); 

    final Label label = new Label("Address Book"); 
    label.setFont(new Font("Arial", 20)); 

    constructTable(); 
    final VBox vbox = new VBox(); 
    vbox.setSpacing(5); 
    vbox.setPadding(new Insets(10, 0, 0, 10)); 
    vbox.getChildren().addAll(label, table); 

    ((Group) scene.getRoot()).getChildren().addAll(vbox); 

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

private void constructTable() { 
    TableColumn<Person, String> firstNameCol = new TableColumn<Person, String>("First Name"); 
    firstNameCol.setMinWidth(100); 
    firstNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName")); 

    TableColumn<Person, String> lastNameCol = new TableColumn<Person, String>("Last Name"); 
    lastNameCol.setMinWidth(100); 
    lastNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName")); 

    TableColumn<Person, String> emailCol = new TableColumn<Person, String>("Email"); 
    emailCol.setMinWidth(200); 
    emailCol.setCellValueFactory(new PropertyValueFactory<Person, String>("email")); 

    table.setItems(getData()); 
    table.getColumns().addAll(firstNameCol, lastNameCol, emailCol); 

    table.setRowFactory(tv -> new TableRow<Person>() { 
      Node detailsPane ; 
      { 
       this.selectedProperty().addListener((obs, wasSelected, isNowSelected) -> { 
        if (isNowSelected) { 
         detailsPane = constructSubTable(getItem()); 
         this.getChildren().add(detailsPane); 
        } else { 
         this.getChildren().remove(detailsPane); 
        } 
        this.requestLayout(); 
       }); 

      } 

      @Override 
      protected double computePrefHeight(double width) { 
       if (isSelected()) { 
        return super.computePrefHeight(width)+detailsPane.prefHeight(60); 
       } else { 
        return super.computePrefHeight(width); 
       } 
      } 

      @Override 
      protected void layoutChildren() { 
       super.layoutChildren(); 
       if (isSelected()) { 
        double width = getWidth(); 
        double paneHeight = detailsPane.prefHeight(width); 
        detailsPane.resizeRelocate(0, getHeight()-paneHeight, width, paneHeight); 
       } 
      } 
     }); 
} 

private TableView<Address> constructSubTable(Person person) { 

    List<Address> addresses = new ArrayList<>(); addresses.add(person.getAddress()); 

    TableView<Address> subTable = new TableView<Address>(); 
    TableColumn<Address, String> streetCol = new TableColumn<Address, String>("Street"); 
    streetCol.setMinWidth(100); 
    streetCol.setCellValueFactory(new PropertyValueFactory<Address, String>("Street")); 

    TableColumn<Address, String> cityCol = new TableColumn<Address, String>("City"); 
    cityCol.setMinWidth(100); 
    cityCol.setCellValueFactory(new PropertyValueFactory<Address, String>("city")); 

    subTable.setItems(FXCollections.observableArrayList(addresses)); 
    subTable.getColumns().addAll(streetCol, cityCol); 
    subTable.setPrefHeight(50+(addresses.size()*30)); 
    subTable.setStyle("-fx-border-color: #42bff4;"); 
    return subTable; 
} 


private ObservableList<Person> getData() { 
    return FXCollections.observableArrayList(new Person("Jacob", "Smith", "[email protected]","Jacob Street","NY"), 
      new Person("Isabella", "Johnson", "[email protected]","Isabella Street","DL"), 
      new Person("Ethan", "Williams", "[email protected]","Ethan Street"," ML"), 
      new Person("Emma", "Jones", "[email protected]","Emma Street","EL"), 
      new Person("Michael", "Brown", "[email protected]","Michael Street","ML")); 
} 

public static class Person { 
    private final SimpleStringProperty firstName; 
    private final SimpleStringProperty lastName; 
    private final SimpleStringProperty email; 
    private Address address; 

    Person(String fName, String lName, String email,String streetS, String cityS) { 
     this.firstName = new SimpleStringProperty(fName); 
     this.lastName = new SimpleStringProperty(lName); 
     this.email = new SimpleStringProperty(email); 
     address = new Address(streetS, cityS); 
    } 

    public String getFirstName() { 
     return firstName.get(); 
    } 

    public void setFirstName(String fName) { 
     firstName.set(fName); 
    } 

    public String getLastName() { 
     return lastName.get(); 
    } 

    public void setLastName(String fName) { 
     lastName.set(fName); 
    } 

    public String getEmail() { 
     return email.get(); 
    } 

    public void setEmail(String fName) { 
     email.set(fName); 
    } 

    public Address getAddress() { 
     return address; 
    } 

    public void setAddress(Address address) { 
     this.address = address; 
    } 

} 


public static class Address{ 
    private final SimpleStringProperty street; 
    private final SimpleStringProperty city; 

    Address(String streetS, String cityS) { 
     this.street = new SimpleStringProperty(streetS); 
     this.city = new SimpleStringProperty(cityS); 
    } 
    public String getStreet() { 
     return street.get(); 
    } 

    public void setStreet(String streetS) { 
     street.set(streetS); 
    } 

    public String getCity() { 
     return city.get(); 
    } 

    public void setCity(String cityS) { 
     city.set(cityS); 
    } 


} 
} 

https://gist.github.com/sh9va/c81b9de44811cc860951701124941c1e

1

您需要在場景構建使用TreeTableView等從添加你想要的列,放在一邊,你應該創建TabPane佈局,將包含您想要的信息。

p.s不要忘了做一個模型,TreeTableView需要一個模型來創建單元格。

+0

請參閱:[http://www.java2s.com/Tutorials/Java/JavaFX/0670__JavaFX_Tree_Table_View.htm] –