JavaFX无法在FXML应用程序中显示上下文菜单

时间:2014-06-02 22:53:57

标签: contextmenu tableview javafx-8

我从JavaFX开始。

我正在测试创建一个FXML项目,并在我点击一个tableview的行时尝试显示一个上下文菜单,但我看不到我的上下文菜单。

我的代码是:

package fxmltableview;
import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;
import javafx.beans.binding.Bindings;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;

/**
 *
 * @author aCedano
 */

public class FXMLTableViewController implements Initializable {
    @FXML private TableView<Person> tableView;
    @FXML private TableColumn<Person, String> firstNameColumn;
    @FXML private TableColumn<Person, String> lastNameColumn;
    @FXML private TableColumn<Person, String> testColumn;
    @FXML private ContextMenu resultadoContextMenu;
    Connection con;
    private DBClass objDbClass;
    private ObservableList<Person> data;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
       assert tableView != null : "fx:id=\"tableView\" was not injected: check your FXML file...";
       firstNameColumn.setCellValueFactory(new PropertyValueFactory<>("test"));   
       lastNameColumn.setCellValueFactory(new PropertyValueFactory<>("firstName"));  
       testColumn.setCellValueFactory(new PropertyValueFactory<>("lastName"));   
        objDbClass = new DBClass();
        try{
        con = objDbClass.getConnection();

        buildData();

        tableView.setRowFactory(
        (TableView<Person> tableView1) -> {
            final TableRow<Person> row = new TableRow<>();
            final ContextMenu rowMenu = new ContextMenu();
            MenuItem editItem = new MenuItem("Edit");
            MenuItem removeItem = new MenuItem("Delete");
            removeItem.setOnAction((ActionEvent event) -> {
                tableView1.getItems().remove(row.getItem());
            });
            rowMenu.getItems().addAll(editItem, removeItem);
            row.contextMenuProperty().bind(
                    Bindings.when(Bindings.isNotNull(row.itemProperty()))
                            .then(rowMenu)
                            .otherwise((ContextMenu)null));
            return row;
        });
        tableView.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent e) -> {
            if (e.getButton() == MouseButton.SECONDARY)  
                resultadoContextMenu.show(tableView, e.getScreenX(), e.getScreenY());
        });
        }
    catch(ClassNotFoundException | SQLException ce){
    }
    }

//Populate table
public void buildData(){        
    data = FXCollections.observableArrayList();
    try{      
        String SQL = "Select * from tbltest Order By test";            
        ResultSet rs = con.createStatement().executeQuery(SQL);  
        while(rs.next()){
            Person cm = new Person();
            cm.setFirstName(rs.getString("test"));
            cm.setLastName(rs.getString("nombre"));
            cm.setTest(rs.getString("apellido"));
            data.add(cm);                  
        }
        tableView.setItems(data);
        tableView.getItems();
    }
    catch(SQLException e){
          System.out.println("Error on Building Data");            
    }
}
}

我正在使用“场景”构建器,我的FXML文件是:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.paint.*?>
<?import javafx.geometry.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.cell.*?>
<?import javafx.collections.*?>
<?import fxmltableview.*?>
<!--/*<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" fx:controller="fxmltableview.FXMLDocumentController">
    <children>
        <Button layoutX="126" layoutY="90" text="Click Me!" onAction="#handleButtonAction" fx:id="button" />
        <Label layoutX="126" layoutY="120" minHeight="16" minWidth="69" fx:id="label" />
    </children>
</AnchorPane>*/-->
<GridPane alignment="CENTER" hgap="10.0" prefHeight="689.0" prefWidth="710.0" vgap="10.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="fxmltableview.FXMLTableViewController">
    <padding>
        <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
    </padding>
   <children>
      <Button mnemonicParsing="false" text="Button" GridPane.rowIndex="1">
         <effect>
            <DropShadow />
         </effect>
      </Button> 
       <Label style="-fx-font: NORMAL 20 Tahoma;" text="Address Book" textOverrun="CENTER_ELLIPSIS" GridPane.rowIndex="2">
         <font>
            <Font name="Gill Sans MT Bold" size="13.0" />
         </font>
         <textFill>
            <LinearGradient endX="1.0" endY="1.0" startX="0.5769230769230769">
               <stops>
                  <Stop color="BLACK" />
                  <Stop color="WHITE" offset="1.0" />
               </stops>
            </LinearGradient>
         </textFill></Label>
       <TableView fx:id="tableView" prefHeight="265.0" prefWidth="502.0" tableMenuButtonVisible="true" GridPane.columnIndex="0" GridPane.rowIndex="3">
           <columns>
             <TableColumn fx:id="firstNameColumn" prefWidth="100" text="First Name">
           <cellValueFactory>
                  <PropertyValueFactory property="firstName" />
           </cellValueFactory>
               <contextMenu>
                  <ContextMenu fx:id="resultadoContextMenu">
                    <items>
                      <MenuItem mnemonicParsing="false" text="Unspecified Action" />
                    </items>
                  </ContextMenu>
               </contextMenu>
             </TableColumn>
             <TableColumn fx:id="lastNameColumn" prefWidth="100" text="Last Name">
           <cellValueFactory>
                  <PropertyValueFactory property="lastName" />
           </cellValueFactory>
             </TableColumn>
             <TableColumn fx:id="testColumn" prefWidth="300" text="Email Address">
           <cellValueFactory>
                  <PropertyValueFactory property="test" />
           </cellValueFactory>
             </TableColumn>
        </columns>
        <items>
       <FXCollections fx:factory="observableArrayList">
<!--/*           <Person email="jacob.smith@example.com" firstName="Jacob" lastName="Smith" />
           <Person email="isabella.johnson@example.com" firstName="Isabella" lastName="Johnson" />
           <Person email="ethan.williams@example.com" firstName="Ethan" lastName="Williams" />
           <Person email="emma.jones@example.com" firstName="Emma" lastName="Jones" />
           <Person email="michael.brown@example.com" firstName="Michael" lastName="Brown" />/*-->
       </FXCollections>
   </items>
         <columnResizePolicy>
            <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
         </columnResizePolicy>    
       </TableView>
   </children>
   <columnConstraints>
      <ColumnConstraints />
   </columnConstraints>
   <rowConstraints>
      <RowConstraints />
      <RowConstraints />
      <RowConstraints />
      <RowConstraints />
   </rowConstraints>
</GridPane>

1 个答案:

答案 0 :(得分:0)

您有空条件来显示上下文菜单。将数据添加到tableView并单击行。 (我认为你的ResultSet是空的)

tableView.setItems(data);