2017-11-11 23 views
0

, 나는 그런 일이 발생했습니다 예상보다 더 큰 :
가 나는 ScrollPane에 싸서 및 Buttons 가득하는 GridPane을 만들었지 만에 대한 RowConstraintsColumnConstraints 변경 GridPane 사전에.

문제는 가로 스크롤 막대가 부적절하다는 것입니다. 나는 그 지방이 GridPane이라고 생각하지만, 그런 일이 어떻게 일어나는가?
This is what i'm getting when running the code.

그러나 수직 스크롤바는 완벽하게 훌륭합니다.자바 FX GridPane 내가 <code>javafx</code>와 장난 동안

sample.fxml

<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="SampleController"> 
    <children> 
     <ScrollPane AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> 
     <content> 
      <GridPane fx:id="gridPane" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> 
       <columnConstraints> 
       <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> 
       </columnConstraints> 
      </GridPane> 
     </content> 
     </ScrollPane> 
    </children> 
</AnchorPane> 

sampleController.java

public class SampleController { 

    @FXML public GridPane gridPane; 

    @FXML 
    public void initialize(){ 
     RowConstraints rowConstraints = new RowConstraints(10.0, 40, 40); 
     ColumnConstraints columnConstraints = new ColumnConstraints(10.0, 40, 40); 
     int i=0,j=0; 
     for(i=0; i<50; i++){ 
      gridPane.getRowConstraints().add(i, rowConstraints); 
      for(j=0; j<30; j++) { 
       gridPane.getColumnConstraints().add(j, columnConstraints); 
       Button btn = new Button(Integer.toString(j+1)); 
       btn.setPrefSize(40, 40); 
       gridPane.add(btn, j, i); 
      } 
     } 
    } 
} 

Dashboard.java

public class sample extends Application{ 
    public static void main(String[] args){ 
     launch(args); 
    } 

    @Override 
    public void start(Stage stage) throws Exception { 
     Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); 

     Scene scene = new Scene(root, 300, 275); 

     stage.setTitle("FXML Welcome"); 
     stage.setScene(scene); 
     stage.show(); 
    } 
} 
+0

RowConstraints 및 ColumnConstraints를 제거하십시오. 너는 필요 없어. – VGR

답변

1

당신은 당신이 50 * 30 = 1500 제약이 있습니다 결국 의미 모든 행의 열 열 제약 조건을 추가하는대신. 행 제약 조건을 행 루프 외부의 루프에 추가해야합니다. 또한 제약 조건을 추가하기 전에 제약 조건 목록이 비어 있다고 가정하면 addList 끝에 삽입되기 때문에 삽입 할 색인을 지정할 필요가 없습니다. BTW

for (int i = 0; i < 30; i++) { 
    gridPane.getColumnConstraints().add(columnConstraints); 
} 
for(int i = 0; i < 50; i++){ 
    gridPane.getRowConstraints().add(rowConstraints); 
    for(int j = 0; j < 30; j++) { 
     Button btn = new Button(Integer.toString(j + 1)); 
     btn.setPrefSize(40, 40); 
     gridPane.add(btn, j, i); 
    } 
} 

: 당신이 정말로 fxml에서 만든 열 제약을 필요로하는 경우 코드가 조금 간단되기 때문에이 목록의 마지막에서 두 번째 요소로 열 제약을 삽입 할 필요가없는 경우, 확인하시기 바랍니다 : AnchorPane 제약은 AnchorPane의 자식이 아닌 노드에는 영향을 미치지 않습니다. fxml의 GridPane에서 이러한 제약 조건을 안전하게 제거 할 수 있습니다.

+0

> fxml에서 생성 된 열 제한이 정말로 필요한지 확인하십시오.
실제로, 나는 그것을 필요로하지 않습니다. 그것은 SceneBuilder에 의해 만들어졌고 그것이 nessesary라고 생각했습니다. 정말로, 그렇지 않습니다. 나는 모든'col/rowConstraints' 인스턴스를 fxml과 컨트롤러에서 제거했습니다. 이제는 모든 것이 잘 동작합니다. – Quipex