2017-03-01 14 views
2

Wie kann ich ListView mit Löschen-Taste auf jeder Zeile erstellen und Schaltfläche Aktion in JavaFX löschen?ListView mit Delete Button auf jeder Zeile in JavaFX

enter image description here

+0

Wie kann ich eine Aktion zum Löschen einer Zeile festlegen? –

+0

Wer hier kann mir helfen? –

+0

Erstellen Sie benutzerdefinierte 'ListCell' und' setCellFactory() 'Ihrer' ListView'. – MBec

Antwort

0

Hier ist ein SSCE, die auf this anwer hauptsächlich beruhte.

import javafx.application.Application; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.Label; 
import javafx.scene.control.ListCell; 
import javafx.scene.control.ListView; 
import javafx.scene.layout.HBox; 
import javafx.scene.layout.Pane; 
import javafx.scene.layout.Priority; 
import javafx.scene.layout.StackPane; 
import javafx.stage.Stage; 

public class SO extends Application { 
    static class XCell extends ListCell<String> { 
     HBox hbox = new HBox(); 
     Label label = new Label(""); 
     Pane pane = new Pane(); 
     Button button = new Button("Del"); 

     public XCell() { 
      super(); 

      hbox.getChildren().addAll(label, pane, button); 
      HBox.setHgrow(pane, Priority.ALWAYS); 
      button.setOnAction(event -> getListView().getItems().remove(getItem())); 
     } 

     @Override 
     protected void updateItem(String item, boolean empty) { 
      super.updateItem(item, empty); 
      setText(null); 
      setGraphic(null); 

      if (item != null && !empty) { 
       label.setText(item); 
       setGraphic(hbox); 
      } 
     } 
    } 

    @Override 
    public void start(Stage primaryStage) throws Exception { 
     StackPane pane = new StackPane(); 
     Scene scene = new Scene(pane, 300, 150); 
     primaryStage.setScene(scene); 
     ObservableList<String> list = FXCollections.observableArrayList(
       "Item 1", "Item 2", "Item 3", "Item 4"); 
     ListView<String> lv = new ListView<>(list); 
     lv.setCellFactory(param -> new XCell()); 
     pane.getChildren().add(lv); 
     primaryStage.show(); 
    } 

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

Die wichtigste Änderung ist diese Linie

button.setOnAction(event -> getListView().getItems().remove(getItem())); 

, wo das Element, dass diese Zelle aus dem Artikel Liste der ListView entfernt darstellt.

+0

Danke, Vielen Dank –

+0

Danke, Vielen Dank, –

+0

Es funktioniert ....... –

Verwandte Themen