没有显示FXML Tableview的JavaFX

时间:2017-02-20 23:01:54

标签: javafx fxml observablelist

我一直在寻找我做错的事,但不知怎的,我错过了什么。我已经注释掉并重写了如此多的代码,以至于它开始变得一团糟(完全是我的错),所以希望我能找到解决方案。

我使用FXML表单制作攻击类对象列表。表单拉出,我键入所需的条目,点击添加攻击按钮,表格不填充。以下是相关代码:

涉及的课程

 public class Attack {

       import javafx.beans.property.SimpleStringProperty;

public class Attack {

    private final SimpleStringProperty weapon = new SimpleStringProperty("");
    private final SimpleStringProperty range= new SimpleStringProperty("");
    private final SimpleStringProperty damage= new SimpleStringProperty("");
    private int attackBonus;

    public String getRange(){return range.get();}
    public String getName(){return weapon.get();}
    public String getDamage(){return damage.get();}
    public int getAttackBonus(){return attackBonus;}

    public void setRange(String newRange){range.set(newRange);}
    public void setWeapon(String newName){weapon.set(newName);}
    public void setAttackBonus(int atkBonus){this.attackBonus = atkBonus;}
    public void setDamage(String dmg){damage.set(dmg);}




    Attack(String nameIn, int AB, String rangeIn, String damageIn){

      setWeapon(nameIn);
      setDamage(damageIn);
      setRange(rangeIn);
      setAttackBonus(AB);

    };

    Attack(){
       this("", 0, "", "");
    }
}

控制器类

package sample;


import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.stage.Stage;

public class AddNPC{

    @FXML
    private Button save, addAttack;

    @FXML
    private TextField ac, hp, init, initBonus, fort, will, ref, weapon, attackBonus, damage;
    @FXML
    private RadioButton melee, touch, ranged, rangedTouch;
    @FXML
    private ToggleGroup range;

    @FXML
    private NPC monster;

    @FXML
    private Stage dialogStage;

   @FXML
    private final TableView<Attack> attackTableView = new TableView<>();

    @FXML
    private TableColumn<Attack, String> weaponColumn;
    @FXML
    private TableColumn<Attack, String> attackColumn;
    @FXML
    private TableColumn<Attack, String> rangeColumn;
    @FXML
    private TableColumn<Attack, String> damageColumn;

    Dice d = new Dice(1, 3, 1);

   /* @FXML
    private final ObservableList<Attack> atk = FXCollections.observableArrayList();

    public ObservableList<Attack> getAtk(){
        return atk;
    }*/


    //public AddNPC(){};

    @FXML
    public void initialize(){


        getAttack();



    }


    public ObservableList<Attack> getAttack(){
        ObservableList<Attack> attacks = FXCollections.observableArrayList();

        attacks.add(new Attack("Unarmed", 0, "melee", "1d3"));
        return attacks;
    }

    @FXML
    private void handleButtonAction(ActionEvent event){


        //event for save here but not shown


        if(event.getSource() == addAttack){

            String weaponIn = weapon.getText();
            int bonusIn = Integer.parseInt(attackBonus.getText());
            String rangeIn = range.getSelectedToggle().getUserData().toString();
            String damageIn = damage.getText();

            Attack attack = new Attack(weaponIn, bonusIn, rangeIn, damageIn);



            attackTableView.getItems().add(attack);

            attackBonus.clear();
            weapon.clear();
            damage.clear();





        }

    }



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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.ToggleGroup?>
<?import javafx.scene.control.cell.PropertyValueFactory?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.shape.Line?>
<?import javafx.scene.text.Font?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="541.0" prefWidth="453.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.AddNPC">
   <children>
      <TextField fx:id="name" layoutX="70.0" layoutY="74.0" prefHeight="25.0" prefWidth="127.0" promptText="Name" />
      <TextField fx:id="hp" layoutX="71.0" layoutY="114.0" prefHeight="25.0" prefWidth="38.0" promptText="HP" />
      <TextField fx:id="ac" layoutX="125.0" layoutY="114.0" prefHeight="25.0" prefWidth="38.0" promptText="AC" />
      <TextField fx:id="init" layoutX="179.0" layoutY="114.0" prefHeight="25.0" prefWidth="68.0" promptText="Initiative" />
      <TextField fx:id="initBonus" layoutX="261.0" layoutY="114.0" prefHeight="25.0" prefWidth="98.0" promptText="Initiative Bonus" />
      <TextField fx:id="fort" layoutX="71.0" layoutY="153.0" prefHeight="25.0" prefWidth="38.0" promptText="Fort" />
      <TextField fx:id="will" layoutX="125.0" layoutY="153.0" prefHeight="25.0" prefWidth="38.0" promptText="Will" />
      <TextField fx:id="ref" layoutX="181.0" layoutY="153.0" prefHeight="25.0" prefWidth="57.0" promptText="Reflex" />
      <Button fx:id="save" layoutX="202.0" layoutY="502.0" mnemonicParsing="false" onAction="#handleButtonAction" text="Save" />
      <Button fx:id="addAttack" layoutX="185.0" layoutY="293.0" mnemonicParsing="false" onAction="#handleButtonAction" text="Add Attack" />
      <Label layoutX="159.0" layoutY="23.0" prefHeight="35.0" prefWidth="127.0" text="NPC Editor">
         <font>
            <Font size="24.0" />
         </font>
      </Label>

      <Label layoutX="222.0" layoutY="228.0" text="+" />
      <TextField fx:id="weapon" layoutX="65.0" layoutY="224.0" promptText="Weapon Name" />
       <TextField fx:id="attackBonus" layoutX="232.0" layoutY="224.0" prefHeight="25.0" prefWidth="47.0" promptText="Bonus" />
      <TextField fx:id="damage" layoutX="292.0" layoutY="224.0" prefHeight="25.0" prefWidth="83.0" promptText="damage" />
      <RadioButton fx:id="melee" contentDisplay="TOP" layoutX="69.0" layoutY="253.0" mnemonicParsing="false" prefHeight="35.0" prefWidth="75.0" selected="true" text="Melee" textAlignment="CENTER" userData="melee">
         <toggleGroup>
            <ToggleGroup fx:id="range" />
         </toggleGroup></RadioButton>

      <RadioButton fx:id="touch" layoutX="144.0" layoutY="262.0" mnemonicParsing="false" text="Touch" toggleGroup="$range" userData="touch" />
      <RadioButton fx:id="ranged" layoutX="214.0" layoutY="262.0" mnemonicParsing="false" text="Range" toggleGroup="$range" userData="ranged" />
      <RadioButton fx:id="rangedTouch" layoutX="279.0" layoutY="262.0" mnemonicParsing="false" text="Ranged Touch" toggleGroup="$range" userData="rangedTouch" />
      <Line endX="100.0" layoutX="222.0" layoutY="204.0" startX="-100.0" />
      <TableView fx:id="attackTableView" layoutX="18.0" layoutY="332.0" prefHeight="154.0" prefWidth="418.0">
        <columns>
          <TableColumn fx:id="weaponColumn" prefWidth="107.0" text="Weapon">
            <cellValueFactory>
               <PropertyValueFactory property="weapon" /></cellValueFactory>
          </TableColumn>
          <TableColumn fx:id="attackColumn" prefWidth="69.0" text="Atk Bonus">
            <cellValueFactory>
              <PropertyValueFactory property="attackBonus" /></cellValueFactory>
            </TableColumn>
           <TableColumn fx:id="rangeColumn" prefWidth="59.0" text="Range">
            <cellValueFactory>
              <PropertyValueFactory property="range" /></cellValueFactory>
            </TableColumn>
           <TableColumn fx:id="damageColumn" prefWidth="181.0" text="Damage">
            <cellValueFactory>
              <PropertyValueFactory property="damage" /></cellValueFactory>
            </TableColumn>
        </columns>

      </TableView>
   </children>
</AnchorPane>

希望我没有遗漏任何东西。

0 个答案:

没有答案