0

I am trying to set the padding for my GridPane, but every time I set the padding it gives me the error message:

incompatible types: java.awt.Insets cannot be converted to javafx.geometry.insets

Every website I go to and every search states to implement it like this:

grid.setPadding(new Insets(10, 10, 10, 10));

And in my code I have it set exactly the same just for testing purposes :

grid.setPadding(new Insets(10, 10, 10, 10));

So I don't understand why it won't allow me to write that, I have also tried to research the error message but nothing comes up.

Here is my code:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package myrectangle2d;

import java.awt.Insets;
import javafx.application.Application;
import javafx.event.ActionEvent;

import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 *
 * @author macuser
 */
public class Exercise18_29 extends Application {

    Stage window;
    Label label1, label2, centerXL, centerXR;
    TextField textCenterXL, textCenterXR;

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


    @Override
    public void start(Stage primaryStage) {

        window = primaryStage;
        window.setTitle("Triangles");



        GridPane grid = new GridPane();
        grid.setPadding(new Insets(10, 10, 10, 10));

       label1 = new Label("Do the Triangles match");
       GridPane.setConstraints(label1, 0, 0);


       label2 = new Label("Yes or no");
       GridPane.setConstraints(label2, 1, 0);


       centerXL = new Label("Center X LEFT");
       GridPane.setConstraints(centerXL, 0, 5);

       textCenterXL = new TextField();
       GridPane.setConstraints(textCenterXL, 1, 5);


       centerXR = new Label("Center X RIGHT");
       GridPane.setConstraints(centerXL, 0, 5);

       textCenterXR = new TextField();
       GridPane.setConstraints(textCenterXR, 3, 5);

       grid.getChildren().addAll(label1, label2, centerXL, textCenterXL, centerXR, textCenterXR);

       Scene scene = new Scene(grid, 400, 600);


       window.setScene(scene);



        window.show();

    }




}

2 Answers2

6

The error message tells you everything you need to know:

incompatible types: java.awt.Insets cannot be converted to javafx.geometry.insets

You have imported java.awt.Insets: (first import line in your code) you need javafx.geometry.Insets.

James_D
  • 201,275
  • 16
  • 291
  • 322
  • @ James_D - Thanks, that did the trick. I am still new to learning Java so I didn't fully understand that error. –  Oct 22 '15 at 19:27
  • i get the same error but for me if i change it to javafx.geometry.Insets it gives an error and asks me to change it to java.awt.Insets, if i use java.awt.Insets the Insets inside the setPadding doesn't work, it i change it to geometry.Insets the code doesn't run... any thoughts? – SebastianZdroana Apr 27 '16 at 22:23
  • Sounds like a different question. Are you using AWT/Swing as well as JavaFX? I.e. do you need both `Insets` classes for some reason? – James_D Apr 27 '16 at 22:25
  • @James_D nah i'm just trying to use GridPane from javafx and Insets to set the padding no swing at all :) – SebastianZdroana Apr 29 '16 at 11:34
-1

The answer above is good. You need

import javafx.geometry.Insets;

instead of

import java.awt.Insets;

I generated the error first time when I allowed my IDE to auto import the necessary libraries. Sometimes the shortcuts bite.

ChristianYami
  • 586
  • 1
  • 9
  • 17