0

私のプログラムは、3 列のテーブルを表示します。列のヘッダーをクリックすると、TableView の内容がソートされません。

このチュートリアル(列内のデータの並べ替えを参照) を正しく理解していれば、テーブルの並べ替えメカニズムを使用できるようにするために特別な実装を行う必要はありません。

FXML:

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml" fx:controller="br.meuspila.javafx.ListarCorretorasController">
  <children>
    <TableView fx:id="tabelaCorretoras" prefHeight="400.0" prefWidth="600.0">
      <columns>
        <TableColumn prefWidth="150.0" text="Nome" fx:id="colunaNome" />
        <TableColumn prefWidth="75.0" text="Código" fx:id="colunaCodigo" />
        <TableColumn prefWidth="200.0" text="Descrição" fx:id="colunaDescricao" />
      </columns>
    </TableView>
  </children>
</AnchorPane>

コントローラ:

package br.meuspila.javafx;

import br.meuspila.db.Corretora;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

/**
 * FXML Controller class
 */
public class ListarCorretorasController implements Initializable {

    @FXML //  fx:id="tabelaCorretoras"
    private TableView<Corretora> tabelaCorretoras; // Value injected by FXMLLoader
    @FXML //  fx:id="colunaNome"
    private TableColumn<Corretora,String> colunaNome; // Value injected by FXMLLoader
    @FXML //  fx:id="colunaCodigo"
    private TableColumn<Corretora,String> colunaCodigo; // Value injected by FXMLLoader
    @FXML //  fx:id="colunaDescricao"
    private TableColumn<Corretora,String> colunaDescricao; // Value injected by FXMLLoader


    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        colunaNome.setCellValueFactory(new PropertyValueFactory<Corretora,String>("nome"));
        colunaCodigo.setCellValueFactory(new PropertyValueFactory<Corretora,String>("codigo"));
        colunaDescricao.setCellValueFactory(new PropertyValueFactory<Corretora,String>("descricao"));

        ObservableList<Corretora> list = FXCollections.observableArrayList();
        Corretora c1 = new Corretora();
        c1.setNome("RICO");
        c1.setCodigo("1234");
        c1.setDescricao("Testando, 123");
        list.add(c1);
        Corretora c2 = new Corretora();
        c2.setNome("MIRAE");
        c2.setCodigo("4321");
        c2.setDescricao("Mirae 123 testando");
        list.add(c2);

        tabelaCorretoras.setItems(list);
    }    
}

エンティティ/モデル:

package br.meuspila.db;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

@Entity
@Table(name = "CORRETORA")
@NamedQueries({
    @NamedQuery(name = "Corretora.findAll", query = "SELECT c FROM Corretora c"),
    @NamedQuery(name = "Corretora.findById", query = "SELECT c FROM Corretora c WHERE c.id = :id"),
    @NamedQuery(name = "Corretora.findByNome", query = "SELECT c FROM Corretora c WHERE c.nome = :nome"),
    @NamedQuery(name = "Corretora.findByCodigo", query = "SELECT c FROM Corretora c WHERE c.codigo = :codigo"),
    @NamedQuery(name = "Corretora.findByDescricao", query = "SELECT c FROM Corretora c WHERE c.descricao = :descricao")})
public class Corretora implements EntityInterface<Corretora>, Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ID")
    private Long id;

    @Basic(optional = false)
    @Column(name = "NOME")
    private String nome;

    @Column(name = "CODIGO")
    private String codigo = "";

    @Column(name = "DESCRICAO")
    private String descricao = "";

    public Corretora() {
    }

    public Corretora(Long id) {
        this.id = id;
    }

    public Corretora(Long id, String nome) {
        this.id = id;
        this.nome = nome;
    }

    @Override
    public Long getId() {
        return id;
    }

    @Override
    public void setId(Long id) {
        this.id = id;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getCodigo() {
        return codigo;
    }

    public void setCodigo(String codigo) {
        this.codigo = codigo;
    }

    public String getDescricao() {
        return descricao;
    }

    public void setDescricao(String descricao) {
        this.descricao = descricao;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Corretora)) {
            return false;
        }
        Corretora other = (Corretora) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "br.meuspila.entity.Corretora[ id=" + id + " ]";
    }

    @Override
    public Corretora clone() {
        Corretora copy = new Corretora();
        copy.setCodigo(this.getCodigo());
        copy.setDescricao(this.getDescricao());
        copy.setId(this.getId());
        copy.setNome(this.getNome());
        return copy;
    }

}
4

0 に答える 0