2017-06-22 2 views
-1

Ich habe ein Fenster in fxml mit einem TableView und ein paar TableColumns und anderen Sachen geschrieben. Ich versuche, eine Funktion in der Steuerung zu diesem Fenster zu erstellen, die es dem Benutzer ermöglicht, auszuwählen, welche Spalten über eine Reihe von Kontrollkästchen in einem separaten Fenster angezeigt werden sollen. Dieses Fenster wäre ein untergeordnetes Fenster des HealthCheckTab. Es hätte eine Taste, die einmal gedrückt wurde, um den Status aller Checkboxen zu erhalten und eine Funktion in HealtkcheckTab aufzurufen, die die Tabellenansicht aktualisiert und die unerwünschten Spalten nicht sichtbar macht. Verstecke TableColumns von TableView durch CheckBox in einem separaten Fenster

Dies ist das Fenster mit dem Tableview (Auszug)

     </TitledPane> 
         <TitledPane fx:id="classificationNumbers" animated="false" 
          text="HealthCheck Classification Numbers"> 
          <content> 
           <ScrollPane fitToHeight="true" fitToWidth="true" 
            hbarPolicy="ALWAYS" maxHeight="1.7976931348623157E308" 
            maxWidth="1.7976931348623157E308" pannable="true"> 
            <content> 
             <TableView fx:id="HcTable" editable="true" 
              maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" 
              minWidth="-Infinity" tableMenuButtonVisible="true"> 
              <columns> 
               <TableColumn fx:id="colId" editable="false" 
                maxWidth="-Infinity" minWidth="40.0" prefWidth="-1.0" text="ID" /> 
               <TableColumn fx:id="colName" editable="false" 
                maxWidth="1.7976931348623157E308" minWidth="60.0" 
                prefWidth="-1.0" text="Name" /> 
               <TableColumn fx:id="colVal" maxWidth="1.7976931348623157E308" 
                minWidth="55.0" prefWidth="-1.0" text="Value" /> 
               <TableColumn fx:id="colDescrCn" editable="false" 
                maxWidth="1.7976931348623157E308" minWidth="90.0" 
                prefWidth="-1.0" text="Comment (CN)" /> 
               <TableColumn fx:id="colDatatype" editable="false" 
                maxWidth="1.7976931348623157E308" minWidth="80.0" 
                prefWidth="-1.0" text="Datatype" /> 
               <TableColumn fx:id="colTool" maxWidth="1.7976931348623157E308" 
                minWidth="50.0" prefWidth="-1.0" text="Tool" visible="false" /> 
               <TableColumn fx:id="colPrio" editable="false" 
                maxWidth="1.7976931348623157E308" minWidth="60.0" 
                prefWidth="-1.0" text="Priority" /> 
               <TableColumn fx:id="colScope" maxWidth="1.7976931348623157E308" 
                minWidth="50.0" prefWidth="-1.0" text="Scope" visible="false" /> 
               <TableColumn fx:id="colAvg" editable="false" 
                maxWidth="1.7976931348623157E308" minWidth="50.0" 
                prefWidth="-1.0" text="AVG" visible="false" /> 
               <TableColumn fx:id="colMin" maxWidth="1.7976931348623157E308" 
                minWidth="50.0" prefWidth="-1.0" text="MIN" visible="false" /> 
               <TableColumn fx:id="colRelComp" prefWidth="122.0" 
                text="Relative Comparable" /> 
              </columns> 
             </TableView> 
            </content> 
           </ScrollPane> 
          </content> 
         </TitledPane> 

Dies ist das Steuergerät (Extrakt)

@FXML 
    private TreeView<String> HCTree; 
    @FXML 
    private TableView<Map> HcTable; 

    @FXML 
    private TableColumn<Map, String> colId; 
    @FXML 
    private TableColumn<Map, String> colName; 
    @FXML 
    private TableColumn<Map, String> colVal; 
    @FXML 
    private TableColumn<Map, String> colDescrCn; 
    @FXML 
    private TableColumn<Map, String> colDatatype; 
    @FXML 
    private TableColumn<Map, String> colTool; 
    @FXML 
    private TableColumn<Map, String> colPrio; 
    @FXML 
    private TableColumn<Map, String> colScope; 
    @FXML 
    private TableColumn<Map, String> colAvg; 
    @FXML 
    private TableColumn<Map, String> colMin; 
    @FXML 
    private TableColumn<Map, String> colRelComp; 



    // ----------------------------------------------------------------------------------------------- 
    // REFRESH TABLE 
    // ----------------------------------------------------------------------------------------------- 

    public void refreshTable() { 
     System.out.println("apply"); 
     colScope.setVisible(true); 
     //colId.setVisible(false); 


    } 

separates Fenster (Auszug)

@FXML 
    private void handleApplyViewOptions() { 
     try { 
      FXMLLoader loader = new FXMLLoader(getClass().getResource("HealthCheckTab.fxml")); 
      Stage stage = new Stage(StageStyle.DECORATED); 
      stage.setScene(new Scene(loader.load())); 
      HealthCheckTabController controller = loader.<HealthCheckTabController> getController(); 
      controller.refreshTable(); 
     } catch (IOException e) { 
      java.lang.System.err.println(e.getStackTrace()); 
     } 
     Stage stage = (Stage) btnApply.getScene().getWindow(); 
     stage.close(); 
    } 

Die Refreshfunktion Versteckt die Scope-Spalte überhaupt nicht. Ich weiß nicht, warum ...

+0

1. Die "scope" -Spalte scheint den Index 7 zu haben, nicht 8. (Warum rufen Sie nicht einfach 'colScope.setVisible (...)' auf?) 2. Sie erwarten das Aufrufen von 'setVisible (true)' wird etwas verstecken? 3. Wo rufst du 'refreshTable()'? Ich empfehle Ihnen, ein [MCVE] zu erstellen, das das Problem veranschaulicht, und dieses zu veröffentlichen, anstatt einen unvollständigen Teil Ihres Projekts (der viel Code enthält, der für das Problem nicht relevant ist) zu veröffentlichen. –

+0

ja ich habe erwartet, dass setVisible (true) etwas ändert, was vermisse ich/mache ich falsch?Ich habe die unnötigen Teile weggelassen und die Zeilen hinzugefügt, wo die Aktualisierung aufgerufen wird ... –

+0

Sicher, wenn du etwas * verstecken * willst, würdest du 'setVisible (false)' ???? –

Antwort

2

Sie sind definiert für die UI in HealthCheckTab.fxml und ruft refreshTable auf dem Controller eine neue Kopie des UI-Laden (die nie angezeigt wird).

Wahrscheinlich ist es einfacher, einen onHidden Handler mit dem von Ihnen erstellten Dialogfeld zu registrieren und refreshTable aufzurufen, wenn der Dialog ausgeblendet ist. Hier

ist ein SSCCE:

Main.fxml:

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.scene.layout.BorderPane?> 
<?import javafx.scene.control.TableView?> 
<?import javafx.scene.control.TableColumn?> 
<?import javafx.scene.layout.HBox?> 
<?import javafx.scene.control.Button?> 
<?import javafx.geometry.Insets?> 

<BorderPane fx:controller="MainController" xmlns:fx="http://javafx.com/fxml/1"> 
    <center> 
     <TableView > 
      <columns> 
       <TableColumn fx:id="nameColumn" text="Name" /> 
       <TableColumn fx:id="optionalColumn" text="Data" /> 
      </columns> 
     </TableView> 
    </center> 
    <bottom> 
     <HBox alignment="CENTER"> 
      <Button text="Configure View" onAction="#showViewOptions" /> 
      <padding> 
       <Insets top="5" left="5" bottom="5" right="5"/> 
      </padding> 
     </HBox> 
    </bottom> 
</BorderPane> 

MainController.java:

import java.io.IOException; 

import javafx.fxml.FXML; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.scene.control.TableColumn; 
import javafx.stage.Stage; 

public class MainController { 

    @FXML 
    private TableColumn<Void, Void> nameColumn ; 
    @FXML 
    private TableColumn<Void, Void> optionalColumn ; 

    @FXML 
    private void showViewOptions() throws IOException { 
     FXMLLoader loader = new FXMLLoader(ViewOptionsController.class.getResource("ViewOptions.fxml")); 
     Parent root = loader.load(); 
     ViewOptionsController controller = loader.getController(); 
     controller.setOptionalColumnVisible(optionalColumn.isVisible()); 
     Scene scene = new Scene(root); 
     Stage stage = new Stage(); 
     stage.setScene(scene); 
     stage.initOwner(nameColumn.getTableView().getScene().getWindow()); 
     stage.setOnHidden(e -> { 
      refreshTable(controller.isOptionalColumnVisible()); 
     }); 
     stage.show(); 
    } 

    private void refreshTable(boolean showOptionalColumn) { 
     optionalColumn.setVisible(showOptionalColumn); 
    } 
} 

ViewOptions.fxml:

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.scene.layout.VBox?> 
<?import javafx.scene.control.CheckBox?> 
<?import javafx.scene.control.Button?> 
<?import javafx.geometry.Insets?> 

<VBox fx:controller="ViewOptionsController" spacing="5" alignment="center" xmlns:fx="http://javafx.com/fxml/1"> 
    <CheckBox text="Show Data Column" fx:id="optionalColumnVisibleCheckBox" /> 
    <Button text="OK" onAction="#close" /> 
    <padding> 
     <Insets top="5" left="5" right="5" bottom="5"/> 
    </padding> 
</VBox> 

ViewOptionsController.java:

import javafx.beans.property.BooleanProperty; 
import javafx.fxml.FXML; 
import javafx.scene.control.CheckBox; 

public class ViewOptionsController { 

    @FXML 
    private CheckBox optionalColumnVisibleCheckBox ; 

    public BooleanProperty optionalColumnVisibleProperty() { 
     return optionalColumnVisibleCheckBox.selectedProperty(); 
    } 

    public final boolean isOptionalColumnVisible() { 
     return optionalColumnVisibleProperty().get(); 
    } 

    public final void setOptionalColumnVisible(boolean optionalColumnVisible) { 
     optionalColumnVisibleProperty().set(optionalColumnVisible); 
    } 

    @FXML 
    private void close() { 
     optionalColumnVisibleCheckBox.getScene().getWindow().hide(); 
    } 
} 

Anwendungsklasse:

import java.io.IOException; 

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 

public class HideColumnsFromDialog extends Application { 

    @Override 
    public void start(Stage primaryStage) throws IOException { 
     Parent root = FXMLLoader.load(MainController.class.getResource("Main.fxml")); 
     Scene scene = new Scene(root, 600, 600); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

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

Als Nebenwirkung beachten Sie, dass die Funktionen, die Sie hier implementieren, um die Tabellenansicht bereits gebaut: Sie

<TableView fx:id="HcTable" tableMenuButtonVisible="true" ... > 
verwenden können

und die Tabelle zeigt eine Schaltfläche, die ein Dropdown-Menü anzeigt, aus dem der Benutzer auswählen kann, welche Spalten sichtbar sind.

Verwandte Themen