Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

java - JavaFX: How to make my custom component use all available space from parent layout?

I′m trying to make a custom component to javafx, so I make my class extends javafx.scene.control.Control and on constructor it draws it′s contents.

My doubts are: how do I make it "grow" to fill all the space available on its parent layout? Like when I place a TextArea instead...

This is an shrink example of what I′m doing:

@Override
public void start(Stage primarystage) throws Exception {
    BorderPane bpane = new BorderPane();
    mainscene = new Scene(bpane, 800, 600);
    ((BorderPane) this.mainscene.getRoot()).setCenter(t);

    primarystage.setScene(mainscene);
    primarystage.show();
}

On this example the TextArea will get all the space the layout provide for it. I want my component do the same!

All I already tried to do: - Change the properties setMaxWidth/Height, setPrefWidth/Height, setMinWidth/Height; (when I set the MinValues, it always use this values to draw but don′t update the "width" value anymore) - Tried to use Double.MAX_VALUE to force a bigger size then layout privides; Everything didn′t work :( I must be mistaking somehow... - I cried a little too, but even this make nothing work ='(

Maybe a clue is, I put a listener like this below, and everytime I resize my application/stage it updates the TextArea. But when I add this listener do my component it never get update...

TextArea t = new TextArea("teste");
t.widthProperty().addListener(new ChangeListener() {
  public void changed(ObservableValue observable, Object oldValue, Object newValue) {
    System.out.println(oldValue + "|" + newValue);
  }
});

Probably I′m passing by some properties or forgetting something, I don′t know...

I already tried Google, but seems the documentation of JavaFX is too small and superficial, and a lot of them is based on JavaFX script...

I also accepts any tutorial better them the one offered by Oracle...

Thanks in advance of any help... I thing I wrote to much... =O

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I found what I was mistaking...

To make a component grow with it′s parent, all you need to do is define on maxwidth and/or maxheight it max value of double (like below), and let minwidth/height and prefwidth/height with it′s default values.

this.setMaxWidth(Double.MAX_VALUE);
this.setMaxHeight(Double.MAX_VALUE);

With this configuration when the parent changes the available space it will use the final method setWidth()/setHeight() to update the width/height your component can use. So, since these methods are final you can′t override it to "know" when a change occurs. You′ll need to add a changelistener like below to be notified when you need to repaint your component.

t.widthProperty().addListener(new ChangeListener() {
  public void changed(ObservableValue observable, Object oldValue, Object newValue) {
    System.out.println(oldValue + "|" + newValue);
  }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...