Marco Canora
Marco Canora

Reputation: 335

How can I know if a TableView is empty?

I'm trying to do a BooleanBinding in JavaFX 8 that determinates if a TableView is empty. How can I do this?

Upvotes: 5

Views: 4649

Answers (7)

Ricardo Roa
Ricardo Roa

Reputation: 173

Or Try This:

package application;

import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXDialog;
import com.jfoenix.controls.JFXDialogLayout;
import javafx.collections.ObservableList;
import javafx.scene.control.TableView;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;

public class Mensaje {


    public Object mensaje( @SuppressWarnings("rawtypes") TableView table, StackPane Stack) {
        ObservableList<?> items = table.getItems();
        if(items.isEmpty()) {
                Text cabecera = new Text();
                cabecera.setText("NOTIFICACION");
                cabecera.setStyle("-fx-fill:red;-fx-font-weight:bold");
                Text mensaje= new Text();
                mensaje.setText("DATOS NO ENCONTRADOS");
                mensaje.setStyle("-fx-fill:black;-fx-font-weight:bold");
                JFXDialogLayout contenido = new JFXDialogLayout();
                contenido.setHeading((cabecera));
                contenido.setBody(mensaje);
                contenido.setStyle(" -fx-background-color:  linear-gradient( from 0.0% 0.0% to 100.0% 100.0%, rgb(153,204,153) 0.0, rgb(153,204,153) 100.0);");
                JFXDialog dialogo = new JFXDialog(Stack,contenido, JFXDialog.DialogTransition.CENTER);
                JFXButton cerrar = new JFXButton("CERRAR");
                cerrar.setStyle(" -fx-background-color: white;-fx-border-color:  linear-gradient(to bottom, red 14%, red 91%); -fx-border-radius:  15%; -fx-text-fill: red;  -fx-font-family: 'Oswald Regular';-fx-font-weight: bold; -fx-border-width: 5px;-fx-background:none;-fx-border-insets: -5.8;");
                cerrar.setOnAction(e3->{
                dialogo.close();
                });
                contenido.setActions(cerrar);
                dialogo.show(); 
        }
        return items;

    }
}

Upvotes: 0

Ricardo Roa
Ricardo Roa

Reputation: 173

You Can Try this:

public Object mensaje(@SuppressWarnings("rawtypes") TableView table) {
    ObservableList<?> items = table.getItems();
    if(items.isEmpty()) {
            Text cabecera = new Text();
            cabecera.setText("NOTIFICACION");
            cabecera.setStyle("-fx-fill:red;-fx-font-weight:bold");
            Text mensaje= new Text();
            mensaje.setText("DATOS NO ENCONTRADOS");
            mensaje.setStyle("-fx-fill:black;-fx-font-weight:bold");
            JFXDialogLayout contenido = new JFXDialogLayout();
            contenido.setHeading((cabecera));
            contenido.setBody(mensaje);
            contenido.setStyle(" -fx-background-color:  linear-gradient( from 0.0% 0.0% to 100.0% 100.0%, rgb(153,204,153) 0.0, rgb(153,204,153) 100.0);");
            JFXDialog dialogo = new JFXDialog(stackpaneitems,contenido, JFXDialog.DialogTransition.CENTER);
            JFXButton cerrar = new JFXButton("CERRAR");
            cerrar.setStyle(" -fx-background-color: white;-fx-border-color:  linear-gradient(to bottom, red 14%, red 91%); -fx-border-radius:  15%; -fx-text-fill: red;  -fx-font-family: 'Oswald Regular';-fx-font-weight: bold; -fx-border-width: 5px;-fx-background:none;-fx-border-insets: -5.8;");
            cerrar.setOnAction(e3->{
            dialogo.close();
            });
            contenido.setActions(cerrar);
            dialogo.show(); 
    }
    return items;

}

Upvotes: -1

Dustin
Dustin

Reputation: 733

Since a TableView must be populated from an ObservableList, the simplest way to check if the table is empty, is to check if the ObservableList is empty.

if (yourObservableList.isEmpty(){
    // run code if table is empty.
} 

Upvotes: 0

Danielvgftv
Danielvgftv

Reputation: 635

    //import javafx.beans.binding.Bindings;
    if(Bindings.isEmpty(tableViewFxId.getItems()).get()) {
        //EMPTY


    }else{
        //FILLED


    }

Upvotes: 0

Oumalek Mohamed
Oumalek Mohamed

Reputation: 96

the code bellow work for me try it ! it's very simple

ObservableList<> list = tableview.getItems();

if (list.size() > 0) 
{

} 
else 
{
     //Empty Tableview
}

Upvotes: 0

Thijs Zijdel
Thijs Zijdel

Reputation: 11

ObservableList<> items = table.getItems();

if (items.isEmpty()) {
     //do some
} else {
     //your table wasn't empty
}

Should also work

Upvotes: 0

James_D
James_D

Reputation: 209245

Use

Bindings.isEmpty(table.getItems())

Upvotes: 9

Related Questions