Milinda Saranga Milinda Saranga - 1 month ago 8
Java Question

HBox inside GridPane in Javafx

I have to add HBox to GridPane. If I add HBox to GridPane in same class then the system display correctly. But when I try with two classes, only empty window display.I'm new to javafx..How can I do that please help me thanks.

public class IpCamMainWindow extends Application{

private static ArrayList<IpCamViewer> ipCameraList = new ArrayList<IpCamViewer>();
private static ArrayList<String> urls= new ArrayList<String>();
GridPane grid =null;

private ImageView imgWebCamCapturedImage;
private BufferedImage grabbedImage;
private ObjectProperty<Image> imageProperty = new SimpleObjectProperty<Image>();
private Webcam webCam = null;
private boolean stopCamera = false;
IPview ipCamViewer=null;

public static void main(String[] args) {


launch(args);
}

@Override
public void start(Stage stage) throws Exception {
grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(0, 10, 0, 10));

for(int i=0;i<4;i++){
ipCamViewer = new IPview();

grid.add(ipCamViewer, i%2, i/2);
System.out.println("column: " + i%2 + ", row: " + i/2);

}

Scene scene = new Scene(grid);
stage.setScene(scene);
stage.setTitle("IP Camera Solution");
stage.show();

}

}


-

public class IPview extends HBox {

private ImageView imgWebCamCapturedImage;
private BufferedImage grabbedImage;
private ObjectProperty<Image> imageProperty = new SimpleObjectProperty<Image>();
HBox hbox;

public IPview(){

HBox hbox=addHBox();

}

public HBox addHBox() {
hbox = new HBox();
hbox.setPadding(new Insets(15, 12, 15, 12));
hbox.setSpacing(10);
hbox.setStyle("-fx-background-color: #336699;");

Button buttonCurrent = new Button("Current");
buttonCurrent.setPrefSize(100, 20);

Button buttonProjected = new Button("Projected");
buttonProjected.setPrefSize(100, 20);
hbox.getChildren().addAll(buttonCurrent, buttonProjected);

return hbox;
}

}

Answer

If IPView is a subclass of HBox, you need to add the buttons to the IPView instance, and not create another HBox as a member variable of it.

I.e.

public class IPview extends HBox {  

    private ImageView imgWebCamCapturedImage;
    private BufferedImage grabbedImage;
    private ObjectProperty<Image> imageProperty = new SimpleObjectProperty<Image>();

    public IPview(){

        this.setPadding(new Insets(15, 12, 15, 12));
        this.setSpacing(10);
        this.setStyle("-fx-background-color: #336699;");

        Button buttonCurrent = new Button("Current");
        buttonCurrent.setPrefSize(100, 20);

        Button buttonProjected = new Button("Projected");
        buttonProjected.setPrefSize(100, 20);
        this.getChildren().addAll(buttonCurrent, buttonProjected);

    }   

}

If you want the HBox to be a member variable, then you would not make IPView a subclass of HBox, and just provide access to the HBox:

public class IPview {  

    private ImageView imgWebCamCapturedImage;
    private BufferedImage grabbedImage;
    private ObjectProperty<Image> imageProperty = new SimpleObjectProperty<Image>();
    private HBox hbox;

    public IPview(){

        hbox = new HBox();
        hbox.setPadding(new Insets(15, 12, 15, 12));
        hbox.setSpacing(10);
        hbox.setStyle("-fx-background-color: #336699;");

        Button buttonCurrent = new Button("Current");
        buttonCurrent.setPrefSize(100, 20);

        Button buttonProjected = new Button("Projected");
        buttonProjected.setPrefSize(100, 20);
        hbox.getChildren().addAll(buttonCurrent, buttonProjected);

    }   

    public Node getView() {
        return hbox ; 
    }

}

And then in your application class you would do

        ipCamViewer = new IPview();     
        grid.add(ipCamViewer.getView(), i%2, i/2);

In general, I prefer the second approach, but that is just a matter of personal preference.

Comments