Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package test3;
- import javafx.application.Application;
- import javafx.beans.property.SimpleStringProperty;
- import javafx.collections.FXCollections;
- import javafx.collections.ObservableList;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- import javafx.geometry.Insets;
- import javafx.scene.Group;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.Hyperlink;
- import javafx.scene.control.Label;
- import javafx.scene.control.TableColumn;
- import javafx.scene.control.TableView;
- import javafx.scene.control.cell.PropertyValueFactory;
- import javafx.scene.layout.HBox;
- import javafx.scene.layout.VBox;
- import javafx.scene.text.Font;
- import javafx.stage.Stage;
- public class TableViewSample extends Application {
- private TableView<Design> table = new TableView<Design>();
- private final ObservableList<Design> data = FXCollections
- .observableArrayList(new Design("1", "sword", "3",
- "very good sword, defeats absolutely any oponent "));
- final VBox hb = new VBox();
- public static void main(String[] args) {
- launch(args);
- }
- @Override
- public void start(Stage stage) {
- Scene scene = new Scene(new Group());
- stage.setTitle("Market");
- stage.setWidth(700);
- stage.setHeight(650);
- Button button = new Button("Market");
- final Hyperlink link = new Hyperlink("https://google.com/");
- button.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent e) {
- getHostServices().showDocument(link.getText());
- }
- });
- final Label label = new Label("user name");
- label.setFont(Font.font("Arial", 14));
- final HBox head = new HBox();
- head.setSpacing(450);
- head.setPadding(new Insets(10, 0, 0, 10));
- head.getChildren().addAll(button, label);
- table.setEditable(true);
- TableColumn designIdCol = new TableColumn("Design ID");
- designIdCol.setPrefWidth(90);
- designIdCol
- .setCellValueFactory(new PropertyValueFactory<Design, String>(
- "Design ID"));
- TableColumn designNameCol = new TableColumn("Design Name");
- designNameCol.setPrefWidth(150);
- designNameCol
- .setCellValueFactory(new PropertyValueFactory<Design, String>(
- "Design Name"));
- TableColumn printsAvailableCol = new TableColumn("Prints Available");
- printsAvailableCol.setPrefWidth(130);
- printsAvailableCol
- .setCellValueFactory(new PropertyValueFactory<Design, String>(
- "Prints Available"));
- TableColumn partDescriptionCol = new TableColumn("Description");
- partDescriptionCol.setPrefWidth(310);
- partDescriptionCol
- .setCellValueFactory(new PropertyValueFactory<Design, String>(
- "Description"));
- table.setItems(data);
- table.getColumns().addAll(designIdCol, designNameCol,
- printsAvailableCol, partDescriptionCol);
- final Label id = new Label();
- id.setFont(Font.font("Arial", 12));
- final Label name = new Label();
- name.setFont(Font.font("Arial", 12));
- final Label availability = new Label();
- availability.setFont(Font.font("Arial", 12));
- final Label description = new Label();
- description.setFont(Font.font("Arial", 12));
- table.getSelectionModel()
- .selectedItemProperty()
- .addListener((observableValue, oldValue, newValue) -> {
- // Check whether item is selected and set value of selected
- // item to Label
- if (table.getSelectionModel().getSelectedItem() != null) {
- id.setText("ID: " + newValue.getID());
- }
- });
- table.getSelectionModel()
- .selectedItemProperty()
- .addListener((observableValue, oldValue, newValue) -> {
- // Check whether item is selected and set value of selected
- // item to Label
- if (table.getSelectionModel().getSelectedItem() != null) {
- name.setText("Design Name: " + newValue.getName());
- }
- });
- table.getSelectionModel()
- .selectedItemProperty()
- .addListener((observableValue, oldValue, newValue) -> {
- // Check whether item is selected and set value of selected
- // item to Label
- if (table.getSelectionModel().getSelectedItem() != null) {
- availability.setText("Availability: " + newValue.getAvailability());
- }
- });
- table.getSelectionModel()
- .selectedItemProperty()
- .addListener((observableValue, oldValue, newValue) -> {
- // Check whether item is selected and set value of selected
- // item to Label
- if (table.getSelectionModel().getSelectedItem() != null) {
- description.setText("Description: " + newValue.getDescription());
- }
- });
- final Label selected = new Label("Selected: ");
- final Button printButton = new Button("Print!");
- printButton.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent e) {
- System.out.print("print");
- }
- });
- // bottom box
- hb.getChildren().addAll(selected, id, name, availability, description, printButton);
- hb.setSpacing(3);
- final VBox vbox = new VBox();
- vbox.setSpacing(10);
- vbox.setPadding(new Insets(10, 0, 0, 10));
- vbox.getChildren().addAll(head, table, hb);
- ((Group) scene.getRoot()).getChildren().addAll(vbox);
- stage.setScene(scene);
- stage.show();
- }
- public static class Design {
- private final SimpleStringProperty id;
- private final SimpleStringProperty name;
- private final SimpleStringProperty availability;
- private final SimpleStringProperty description;
- private Design(String ID, String NAME, String AVAILABILITY,
- String DESCRIPTION) {
- this.id = new SimpleStringProperty(ID);
- this.name = new SimpleStringProperty(NAME);
- this.availability = new SimpleStringProperty(AVAILABILITY);
- this.description = new SimpleStringProperty(DESCRIPTION);
- }
- public String getID() {
- return id.get();
- }
- public void setID(String ID) {
- id.set(ID);
- }
- public String getName() {
- return name.get();
- }
- public void setName(String NAME) {
- name.set(NAME);
- }
- public String getAvailability() {
- return availability.get();
- }
- public void setAvailability(String AVAILABILITY) {
- availability.set(AVAILABILITY);
- }
- public String getDescription() {
- return description.get();
- }
- public void setDescription(String DESCRIPTION) {
- description.set(DESCRIPTION);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment