JavaFX从表中获取数据

时间:2014-02-11 13:04:53

标签: javafx

我无法从表中获取数据。 获取项目不起作用,我因为缺少文档而陷入困境。我得到的Person是表模型,但是如何使用getFirstName,这可能就是答案。我已经得到但不能使用它。或者我应该暂时坚持使用Swing?

这是一段代码:

package imenik;

import java.awt.Panel;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

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.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author Anak1n
 */
public class Imenik extends Application {

    Label ime, prezime, brojTelefona, email, mobilni;
    TextField imeTxt;
    TextField prezimeTxt;
    TextField brojTelTxt;
    TextField mobilniTxt;
    TextField emailTxt;
    GridPane panelImenik;
    Panel tabela;
    Button potvrdi, otkazi, sacuvaj, otvori;
    FileChooser fc = new FileChooser();

    VBox vbox;
    private final TableView<Person> table = new TableView<>();

    @Override
    public void start(final Stage primaryStage) {

        ime = new Label("Ime: ");
        prezime = new Label("Prezime: ");
        brojTelefona = new Label("Fixni: ");
        email = new Label("e-mail: ");
        imeTxt = new TextField();
        prezimeTxt = new TextField();
        brojTelTxt = new TextField();
        emailTxt = new TextField();
        mobilni = new Label("Mobilni");
        mobilniTxt = new TextField();

        TableColumn imeT = new TableColumn("Ime");
        imeT.setCellValueFactory(
                new PropertyValueFactory<Person, String>("firstName")
        );
        imeT.prefWidthProperty().bind(table.widthProperty().divide(4));
        TableColumn prezimeT = new TableColumn("Prezime");
        prezimeT.setCellValueFactory(
                new PropertyValueFactory<Person, String>("lastName")
        );
        prezimeT.prefWidthProperty().bind(table.widthProperty().divide(4));

        TableColumn brojTelefonaFix = new TableColumn("Fixni");
        brojTelefonaFix.setCellValueFactory(
                new PropertyValueFactory<Person, String>("fixni")
        );
        brojTelefonaFix.prefWidthProperty().bind(table.widthProperty().divide(4));
        TableColumn brojTelefonaMob = new TableColumn("Mobilni");
        brojTelefonaMob.setCellValueFactory(
                new PropertyValueFactory<Person, String>("mobilni")
        );
        brojTelefonaMob.prefWidthProperty().bind(table.widthProperty().divide(4));
        TableColumn brTel = new TableColumn("Broj telefona");
        brTel.getColumns().addAll(brojTelefonaFix, brojTelefonaMob);
        brTel.prefWidthProperty().bind(table.widthProperty().divide(4));
        TableColumn emailT = new TableColumn("E-mail");
        emailT.setCellValueFactory(
                new PropertyValueFactory<Person, String>("email")
        );
        emailT.prefWidthProperty().bind(table.widthProperty().divide(4));

        sacuvaj = new Button("Sacuvaj");
        sacuvaj.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e) {

                //Set extension filter
                FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("CSV files (*.csv)", "*.csv");
                fc.getExtensionFilters().add(extFilter);

                //Show save file dialog
                File file = fc.showSaveDialog(primaryStage);

                if (file != null) {
                    SaveFile(table.getItems().toString(), file);
                }
            }
        });
        otvori = new Button("Otvori");
        vbox = new VBox();
        vbox.setSpacing(5);
        vbox.setPadding(new Insets(10, 0, 0, 10));

        tabela = new Panel();

        panelImenik = new GridPane();
        panelImenik.setAlignment(Pos.TOP_CENTER);
        panelImenik.setHgap(10);
        panelImenik.setVgap(10);
        panelImenik.setPadding(new Insets(25, 25, 25, 25));
        potvrdi = new Button("Potvrdi");
        potvrdi.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e) {

//      ObservableList<Person> data = table.getItems();
                table.getItems().add(new Person(
                        imeTxt.getText(),
                        prezimeTxt.getText(),
                        emailTxt.getText(),
                        brojTelTxt.getText(),
                        mobilniTxt.getText()
                ));

                prezimeTxt.setText("");
                imeTxt.setText("");
                brojTelTxt.setText("");
                emailTxt.setText("");
                mobilniTxt.setText("");
            }

        });
        table.getColumns().addAll(imeT, prezimeT, brTel, emailT);

        otkazi = new Button("Ponisti");
        otkazi.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e) {                
                prezimeTxt.setText("");
                imeTxt.setText("");
                brojTelTxt.setText("");
                emailTxt.setText("");
                mobilniTxt.setText("");

                System.out.print(table.getColumns().get(0));
            }            

        });

        vbox.getChildren().addAll(table, panelImenik);
        dodaj();
        Scene scene = new Scene(vbox, 411, 600);
        //table.setItems(data);
        primaryStage.setTitle("Imenik");
        scene.getStylesheets().add(Imenik.class.getResource("pozadina.css").toExternalForm());

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

    /**
     * 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);
    }

    private void dodaj() {
        panelImenik.add(ime, 0, 0);
        panelImenik.add(imeTxt, 1, 0);
        panelImenik.add(prezime, 0, 1);
        panelImenik.add(prezimeTxt, 1, 1);
        panelImenik.add(brojTelefona, 0, 2);
        panelImenik.add(brojTelTxt, 1, 2);
        panelImenik.add(mobilni, 0, 3);
        panelImenik.add(mobilniTxt, 1, 3);
        panelImenik.add(email, 0, 4);
        panelImenik.add(emailTxt, 1, 4);
        panelImenik.add(potvrdi, 1, 5);
        panelImenik.add(otkazi, 0, 5);
        panelImenik.add(sacuvaj, 2, 5);
        panelImenik.add(otvori, 3, 5);

    }

    private void dodajUTabelu() {        

    }

    public class Person {

        private final SimpleStringProperty ime;
        private final SimpleStringProperty prezime;
        private final SimpleStringProperty email;
        private final SimpleStringProperty fixni;
        private final SimpleStringProperty mobilni;

        private Person(String ime, String prezime, String email, String fixni, String mobilni) {
            this.ime = new SimpleStringProperty(ime);
            this.prezime = new SimpleStringProperty(prezime);
            this.email = new SimpleStringProperty(email);
            this.fixni = new SimpleStringProperty(fixni);
            this.mobilni = new SimpleStringProperty(mobilni);
        }

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

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

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

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

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

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

        public String getFixni() {
            return fixni.get();
        }

        public void setFixni(String fName) {
            fixni.set(fName);
        }

        public String getMobilni() {
            return mobilni.get();
        }

        public void setMobilni(String fName) {
            mobilni.set(fName);
        }
    }

    private void SaveFile(String content, File file) {
        try {
            FileWriter fileWriter = null;

            fileWriter = new FileWriter(file);
            fileWriter.write(content);
            fileWriter.close();
        } catch (IOException ex) {
            System.out.print("Nije moguce");
        }
    }
}

2 个答案:

答案 0 :(得分:2)

您可以逐行获取项目,在这种情况下,这将是一个人物对象。然后使用Person类的get方法访问字段。我猜你想在SaveFile(String, File)中使用它们你应该把它改成saveFile(ObservableList<Person> persons, File file)之类的东西。然后你写的文件就像

for (Person p : persons){
    fileWriter.write(p.getFirstName()+","+
                     p.getLastName()/*etc..*/+"\n");
}

答案 1 :(得分:1)

我做了这样的事。在oracle论坛上得到了提示:)

for (Person person : table.getItems()) {  
      String firstName = person.getFirstName(); 
      String lastName = person.getLastName(); 
      String email = person.getEmail();
      String broj = person.getFixni();
      String mob = person.getMobilni();

当我拥有它们时,我会轻松保存它们:)