2017-01-23 67 views
0

这是我在这个伟大的社区的第一个问题。我在这个博客http://code.makery.ch/library/javafx-8-tutorial/上开始了一个Java FX教程。但我正在使用IntelliJ IDEA而不是Eclipse。该程序编译成功,但左侧的TableView和右侧的标签都看起来像禁用了(字体颜色为灰色且不是黑色,并且该表无法在鼠标悬停或点击时突出显示)。所以我的问题是:为什么我不能选择表格视图?JavaFX TableView,标签和按钮选择不起作用,看起来和表看起来被禁用

TableView, Labels and Buttons looks disabled

package com.melkojji.controller; 

import com.melkojji.model.Person; 
import com.melkojji.view.PersonOverviewController; 
import javafx.application.Application; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Scene; 
import javafx.scene.layout.AnchorPane; 
import javafx.scene.layout.BorderPane; 
import javafx.stage.Stage; 

import java.io.IOException; 

public class Main extends Application { 

    private Stage primaryStage; 
    private BorderPane rootLayout; 
    private ObservableList<Person> personData = FXCollections.observableArrayList(); 

    public Main() { 
     this.personData.add(new Person("Mustapha", "HUSAIN")); 
     this.personData.add(new Person("Mustapha", "EL KOJJI")); 
    } 

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

    @Override 
    public void start(Stage primaryStage) { 
     this.primaryStage = primaryStage; 
     this.primaryStage.setTitle("AdressApp"); 
     this.primaryStage.setMinWidth(615); 
     this.primaryStage.setMinHeight(365); 
     initRootLayout(); 
     showPersonOverview(); 
    } 

    public void initRootLayout() { 
     try { 
      // Load root layout from fxml file. 
      FXMLLoader loader = new FXMLLoader(); 
      loader.setLocation(Main.class.getResource("../view/RootLayout.fxml")); 
      rootLayout = (BorderPane) loader.load(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     // Show the scene containing the root layout. 
     Scene scene = new Scene(rootLayout); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    public void showPersonOverview() { 
     try { 
      // Load person overview. 
      FXMLLoader loader = new FXMLLoader(); 
      loader.setLocation(Main.class.getResource("../view/PersonOverview.fxml")); 
      AnchorPane personOverview = (AnchorPane) loader.load(); 
      // Set person overview into the center of root layout. 
      rootLayout.setCenter(personOverview); 
      // Give the controller access to the main app. 
      PersonOverviewController controller = loader.getController(); 
      controller.setMain(this); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    public Stage getPrimaryStage() { 
     return primaryStage; 
    } 

    public ObservableList<Person> getPersonData() { 
     return personData; 
    } 
} 

package com.melkojji.view; 

import com.melkojji.controller.Main; 
import com.melkojji.model.Person; 
import com.melkojji.util.DateUtil; 
import javafx.fxml.FXML; 
import javafx.scene.control.Label; 
import javafx.scene.control.TableColumn; 
import javafx.scene.control.TableView; 

/** 
* Created by melkojji on 1/14/2017. 
*/ 
public class PersonOverviewController { 
    @FXML 
    private TableView<Person> personTableView; 
    @FXML 
    private TableColumn<Person, String> personFirstNameTableColumn; 
    @FXML 
    private TableColumn<Person, String> personLastNameTableColumn; 
    @FXML 
    private Label firstNameLabel; 
    @FXML 
    private Label lastNameLabel; 
    @FXML 
    private Label streetLabel; 
    @FXML 
    private Label postalCodeLabel; 
    @FXML 
    private Label cityLabel; 
    @FXML 
    private Label birthdayLabel; 
    // Reference to the main application. 
    private Main main; 

    /** 
    * The constructor. 
    * The constructor is called before the initialize() method. 
    */ 
    public PersonOverviewController() { 
    } 

    /** 
    * Initializes the controller class. This method is automatically called 
    * after the fxml file has been loaded. 
    */ 
    @FXML 
    public void initialize() { 
     // Initialize the person table with the two columns. 
     this.personFirstNameTableColumn.setCellValueFactory(cellData -> cellData.getValue().firstNameProperty()); 
     this.personLastNameTableColumn.setCellValueFactory(cellData -> cellData.getValue().lastNameProperty()); 
     // Clear person details. 
     showPersonDetails(null); 
     // Listen for selection changes and show the person details when changed. 
     personTableView.getSelectionModel().selectedItemProperty().addListener(((observable, oldValue, newValue) -> showPersonDetails(newValue))); 
    } 

    /** 
    * Is called by the main application to give a reference back to itself. 
    * 
    * @param main 
    */ 
    public void setMain(Main main) { 
     this.main = main; 
     // Add observable list data to the table. 
     this.personTableView.setItems(main.getPersonData()); 
    } 

    /** 
    * Fills all text fields to show details about the person. 
    * If the specified person is null, all text fields are cleared. 
    * 
    * @param person the person or null 
    */ 
    public void showPersonDetails(Person person) { 
     if (person != null) { 
      // Fill the labels with info from the person object. 
      firstNameLabel.setText(person.getFirstName()); 
      lastNameLabel.setText(person.getLastName()); 
      streetLabel.setText(person.getStreet()); 
      postalCodeLabel.setText(Integer.toString(person.getPostalCode())); 
      cityLabel.setText(person.getCity()); 
      birthdayLabel.setText(DateUtil.format(person.getBirthday())); 
      // birthdayLabel.setText(...); 
     } else { 
      // Person is null, remove all the text. 
      firstNameLabel.setText(""); 
      lastNameLabel.setText(""); 
      streetLabel.setText(""); 
      postalCodeLabel.setText(""); 
      cityLabel.setText(""); 
      birthdayLabel.setText(""); 
     } 
    } 
} 

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

<?import javafx.scene.control.Menu?> 
<?import javafx.scene.control.MenuBar?> 
<?import javafx.scene.control.MenuItem?> 
<?import javafx.scene.layout.BorderPane?> 


<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="325.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1"> 
    <top> 
     <MenuBar BorderPane.alignment="CENTER"> 
     <menus> 
      <Menu mnemonicParsing="false" text="File"> 
      <items> 
       <MenuItem mnemonicParsing="false" text="Close" /> 
      </items> 
      </Menu> 
      <Menu mnemonicParsing="false" text="Edit"> 
      <items> 
       <MenuItem mnemonicParsing="false" text="Delete" /> 
      </items> 
      </Menu> 
      <Menu mnemonicParsing="false" text="Help"> 
      <items> 
       <MenuItem mnemonicParsing="false" text="About" /> 
      </items> 
      </Menu> 
     </menus> 
     </MenuBar> 
    </top> 
</BorderPane> 

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

<?import javafx.geometry.Insets?> 
<?import javafx.scene.control.Button?> 
<?import javafx.scene.control.ButtonBar?> 
<?import javafx.scene.control.Label?> 
<?import javafx.scene.control.SplitPane?> 
<?import javafx.scene.control.TableColumn?> 
<?import javafx.scene.control.TableView?> 
<?import javafx.scene.layout.AnchorPane?> 
<?import javafx.scene.layout.ColumnConstraints?> 
<?import javafx.scene.layout.GridPane?> 
<?import javafx.scene.layout.RowConstraints?> 

<AnchorPane disable="true" minHeight="-Infinity" minWidth="-Infinity" prefHeight="300.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.melkojji.view.PersonOverviewController"> 
    <children> 
     <SplitPane dividerPositions="0.29797979797979796" prefHeight="300.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> 
     <items> 
      <AnchorPane minHeight="-Infinity" minWidth="-Infinity" prefHeight="160.0" prefWidth="100.0"> 
       <children> 
        <TableView fx:id="personTableView" editable="true" onSort="#initialize" prefHeight="298.0" prefWidth="175.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> 
        <columns> 
         <TableColumn fx:id="personFirstNameTableColumn" prefWidth="75.0" text="First name" /> 
         <TableColumn fx:id="personLastNameTableColumn" prefWidth="75.0" text="Last name" /> 
        </columns> 
        <columnResizePolicy> 
         <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" /> 
        </columnResizePolicy> 
        </TableView> 
       </children></AnchorPane> 
      <AnchorPane minHeight="-Infinity" minWidth="-Infinity" prefHeight="160.0" prefWidth="100.0"> 
       <children> 
        <Label layoutX="14.0" layoutY="14.0" text="Person details :" AnchorPane.leftAnchor="5.0" AnchorPane.topAnchor="5.0" /> 
        <GridPane AnchorPane.leftAnchor="5.0" AnchorPane.rightAnchor="5.0" AnchorPane.topAnchor="30.0"> 
        <columnConstraints> 
         <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> 
         <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> 
        </columnConstraints> 
        <rowConstraints> 
         <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> 
         <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> 
         <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> 
         <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> 
         <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> 
         <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> 
        </rowConstraints> 
        <children> 
         <Label text="First name" /> 
         <Label text="Last name" GridPane.rowIndex="1" /> 
         <Label text="Street" GridPane.rowIndex="2" /> 
         <Label text="City" GridPane.rowIndex="3" /> 
         <Label text="Postal code" GridPane.rowIndex="4" /> 
         <Label text="Birthday" GridPane.rowIndex="5" /> 
         <Label fx:id="firstNameLabel" text="Label" GridPane.columnIndex="1" /> 
         <Label fx:id="lastNameLabel" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="1" /> 
         <Label fx:id="streetLabel" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="2" /> 
         <Label fx:id="cityLabel" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="3" /> 
         <Label fx:id="postalCodeLabel" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="4" /> 
         <Label fx:id="birthdayLabel" text="Label" GridPane.columnIndex="1" GridPane.rowIndex="5" /> 
        </children> 
        </GridPane> 
        <ButtonBar prefHeight="40.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.rightAnchor="0.0"> 
        <buttons> 
         <Button mnemonicParsing="false" text="New" /> 
         <Button mnemonicParsing="false" text="Edit" /> 
         <Button mnemonicParsing="false" text="Delete" /> 
        </buttons> 
        <padding> 
         <Insets right="5.0" /> 
        </padding> 
        </ButtonBar> 
       </children></AnchorPane> 
     </items> 
     </SplitPane> 
    </children> 
</AnchorPane> 

回答

0

disable property将禁用一个节点和所有子节点设置。所以,因为你有

<AnchorPane disable="true" ..> 

锚窗格及其所有子节点,包括表,被禁用。

+0

非常感谢!它正在使用您的解决方案。 –