ListView或ScrollPane滚动条不滚动

时间:2016-06-28 14:36:08

标签: java listview javafx scroll scrollpane

对于大学项目,我必须为桌面游戏的在线版本创建GUI,玩家可以通过扩展JavaFX ImageView创建一些卡片,我想在运行时将它们放在界面中。

因此我在SplitPane中插入了一个JavaFX ListView,但是当我运行我的应用程序时,滚动条不起作用。

卡片完美显示,滚动条可见,但当我尝试使用它时,它看起来像是一个禁用按钮。

如果我使用包含HBox的ScrollPane,也会发生同样的情况。

我试图不使用首选大小,我尝试了ScrollBarPolicies,似乎没有任何效果,最后我得出的结论是问题出现在SplitPane内部。

我甚至认为JavaFX应用程序线程可能已停止,但当滚动条无效时,它实际上正在运行。

FXML

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

<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>

<SplitPane fx:id="mainPlayerRootPane" disable="true" dividerPositions="0.10275689223057644" prefHeight="337.0" styleClass="mainPlayerRootPane" stylesheets="@style.css" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="client.view.GUI.MainPlayerPaneController">
   <items>
      <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
         <children>
            <GridPane prefHeight="335.0" prefWidth="78.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
               <columnConstraints>
                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
               </columnConstraints>
               <rowConstraints>
                  <RowConstraints maxHeight="116.0" minHeight="10.0" prefHeight="80.0" vgrow="SOMETIMES" />
                  <RowConstraints maxHeight="118.0" minHeight="10.0" prefHeight="82.0" vgrow="SOMETIMES" />
                  <RowConstraints maxHeight="138.0" minHeight="10.0" prefHeight="86.0" vgrow="SOMETIMES" />
                  <RowConstraints maxHeight="85.0" minHeight="10.0" prefHeight="85.0" vgrow="SOMETIMES" />
               </rowConstraints>
               <children>
                  <Pane prefHeight="85.0" translateX="4.0">
                     <children>
                        <ImageView fitHeight="77.0" fitWidth="73.0" layoutX="5.0" layoutY="1.0" pickOnBounds="true" preserveRatio="true">
                           <image>
                              <Image url="@../../../../resources/images/VictoryPoints.png" />
                           </image>
                        </ImageView>
                        <Label fx:id="victoryPointsLabel" alignment="TOP_CENTER" contentDisplay="CENTER" opacity="0.9" prefHeight="79.0" prefWidth="70.0" text="99" textAlignment="JUSTIFY" textFill="WHITE">
                           <font>
                              <Font name="Trattatello" size="36.0" />
                           </font>
                        </Label>
                     </children>
                  </Pane>
                  <Pane prefHeight="85.0" translateX="4.0" GridPane.rowIndex="1">
                     <children>
                        <ImageView fitHeight="79.0" fitWidth="71.0" layoutY="4.0" pickOnBounds="true" preserveRatio="true">
                           <image>
                              <Image url="@../../../../resources/images/Coins.png" />
                           </image>
                        </ImageView>
                        <Label fx:id="coinsLabel" alignment="CENTER" contentDisplay="CENTER" opacity="0.9" prefHeight="82.0" prefWidth="77.0" text="20" textFill="#222020">
                           <font>
                              <Font name="Trattatello" size="30.0" />
                           </font>
                        </Label>
                     </children>
                  </Pane>
                  <Pane prefHeight="85.0" prefWidth="81.0" translateX="4.0" GridPane.rowIndex="2">
                     <children>
                        <ImageView fitHeight="79.0" fitWidth="73.0" layoutX="17.0" layoutY="2.0" pickOnBounds="true" preserveRatio="true">
                           <image>
                              <Image url="@../../../../resources/images/Servants.png" />
                           </image>
                        </ImageView>
                        <Label fx:id="servantsLabel" alignment="CENTER" contentDisplay="CENTER" opacity="0.9" prefHeight="101.0" prefWidth="70.0" text="10" textFill="#bcc600">
                           <font>
                              <Font name="Trattatello" size="24.0" />
                           </font>
                        </Label>
                     </children>
                  </Pane>
                  <Pane prefHeight="85.0" translateX="4.0" GridPane.rowIndex="3">
                     <children>
                        <ImageView fitHeight="79.0" fitWidth="71.0" layoutX="1.0" layoutY="3.0" pickOnBounds="true" preserveRatio="true">
                           <image>
                              <Image url="@../../../../resources/images/Emporiums.png" />
                           </image>
                        </ImageView>
                        <Label fx:id="emporiumsLabel" alignment="CENTER" contentDisplay="CENTER" layoutY="8.0" prefHeight="73.0" prefWidth="71.0" text="10">
                           <font>
                              <Font name="Trattatello" size="36.0" />
                           </font>
                        </Label>
                     </children>
                  </Pane>
               </children>
            </GridPane>
         </children>
      </AnchorPane>
      <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0">
         <children>
            <SplitPane dividerPositions="0.5345345345345346" orientation="VERTICAL" prefHeight="335.0" styleClass="mainPlayerRootPane" stylesheets="@style.css" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
              <items>
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0">
                     <children>
                        <SplitPane dividerPositions="0.5" prefHeight="174.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                          <items>
                            <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0">
                                 <children>
                                    <ListView fx:id="cardsListView" editable="true" orientation="HORIZONTAL" prefHeight="172.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
                                 </children>
                              </AnchorPane>
                            <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0">
                                 <children>
                                    <ListView fx:id="permitsListView" orientation="HORIZONTAL" prefHeight="172.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
                                 </children>
                              </AnchorPane>
                          </items>
                        </SplitPane>
                     </children>
                  </AnchorPane>
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="134.0" prefWidth="710.0" />
              </items>
            </SplitPane>
         </children>
      </AnchorPane>
   </items>
</SplitPane>

其控制器类:

package client.view.GUI;


import java.util.List;
import client.model_properties.PlayerProperty;
import javafx.collections.ListChangeListener;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.SplitPane;

public class MainPlayerPaneController {

    @FXML
    private Label victoryPointsLabel;

    @FXML
    private Label coinsLabel;

    @FXML
    private Label servantsLabel;

    @FXML
    private Label emporiumsLabel;

    @FXML
    private SplitPane mainPlayerRootPane;

    @FXML
    private ListView<PoliticalCardView> cardsListView;

    @FXML
    private ListView<PoliticalCardView> permitsListView;

    @FXML
    private void initialize(){

    }

    public void initializeMainPlayerPane(PlayerProperty mainPlayer) {

        this.victoryPointsLabel.textProperty().bind(mainPlayer.getVictoryPoint().asString());
        this.coinsLabel.textProperty().bind(mainPlayer.getCoins().asString());
        this.servantsLabel.textProperty().bind(mainPlayer.getnOfServants().asString());
        this.emporiumsLabel.textProperty().bind(mainPlayer.getAvailableEmporiums().asString());

        this.addToDeckView(mainPlayer.getDeck());
        mainPlayer.getDeckProperty().addListener(new DeckListChangeListener());

    }

    private void addToDeckView(List<? extends String> cards) {
        PoliticalCardView auxCard;
        for(String n : cards) {
            auxCard = new PoliticalCardView(n);
            auxCard.initializeCardView();
            this.cardsListView.getItems().add(auxCard);
        }
    }

    private class DeckListChangeListener implements ListChangeListener<String> {

        @Override
        public void onChanged(javafx.collections.ListChangeListener.Change<? extends String> c) {

            List<? extends String> added = c.getAddedSubList();
            List<? extends String> removed = c.getRemoved();

            for(String n : removed) {
                Node toRemove = cardsListView.getItems().stream()
                .filter( r -> ((PoliticalCardView) r).getColor().equals(n))
                .findFirst()
                .get();

                cardsListView.getItems().remove(toRemove);
            }

            addToDeckView(added);
        }



    }
}

我创建的课程代表卡片。

package client.view.GUI;

import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import utility.UserInterfaceUtilities;

public class PoliticalCardView extends ImageView {

    private Image cardImage;
    private String color;

    public PoliticalCardView(String color) {
        this.color = color;
    }

    public void initializeCardView() {
        this.cardImage = UserInterfaceUtilities.POLITICAL_COLORS_TO_IMAGES.get(this.color);
        this.setImage(cardImage);
        this.setFitHeight(150);
        this.setFitWidth(100);
    }

    public String getColor() {
        return this.color;
    }

}

我想知道问题出在哪里,因为在两天的研究中,我在互联网上找不到类似的东西,而且我还使用了其他工作正常的滚动条。

1 个答案:

答案 0 :(得分:0)

问题在于:

<SplitPane fx:id="mainPlayerRootPane" disable="true"  ..... >

您的SplitPane已停用。

当您通过设置disabledProperty来禁用节点时,其所有子节点都将被禁用。

  

指示是否禁用此节点。节点将成为   如果在其自身或其中一个上将disable设置为true,则禁用   场景图中的祖先。

从FXML声明中移除disable="true",以避免停用ListViewScrollPane