1

I am using a BorderPane, where the right area is unused. In the center area I have a HBox with a Canvas and another control e.g. a Button. I want the Canvas to have the same width and height with a value of

Canvas width and height = minimum{maximum possible Canvas height, maximum possible Canvas width}

(in other words: Canvas should be a square)

My problem is: How do I determine the maximum width and the maximum height that a Canvas could grow to?

Here is my FXML:

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

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>

<BorderPane id="BorderPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml">
  <bottom>
    <Label text="Bottom area" />
  </bottom>

  <center>
    <HBox>
      <children>
        <Canvas width="300" height="300" />
        <Button mnemonicParsing="false" text="some button next to the Canvas" />
      </children>
    </HBox>
  </center>

  <left>
    <Button mnemonicParsing="false" text="Left area"/>
  </left>

  <top>
    <MenuBar>
      <menus>
        <Menu mnemonicParsing="false" text="File">
          <items>
            <MenuItem mnemonicParsing="false" text="Close" />
          </items>
        </Menu>       

      </menus>
    </MenuBar>
  </top>

</BorderPane>

Thanks for any hint!

stefan.at.kotlin
  • 15,347
  • 38
  • 147
  • 270
  • I have a tiny amount of experience with javafx, but it seems that because you have given the borderpane infinite dimensions to grow into, therefore your center region (and your canvas) can expand infinitely. Are you talking about *visible* size? Then this might help: http://stackoverflow.com/questions/12837592/how-to-scroll-to-make-a-node-within-the-content-of-a-scrollpane-visible – markE Feb 17 '13 at 18:00

2 Answers2

0

The easiest way I can see this being done is: canvas.getwidth() and canvas.getHeight()

jister13
  • 83
  • 1
  • 9
  • 2
    Please correct me if I am wrong, but as far as I know I manually have to set a Canvas size, else it is 0. getWidth() only returns what I have set before, not the maximum possible size ): – stefan.at.kotlin Feb 17 '13 at 16:43
0

You have to add canvas manually, after the GUI is constructed, so you can calculate its size:

public class YourController implements Initializable {

    @FXML HBox   mHBox; 
    @FXML Button mButton;

    @Override
    public void initialize(final URL paramURL, final ResourceBundle paramResourceBundle) {
        Platform.runLater(new Runnable() { public void run() {
            double w0 = mHBox.getWidth();
            double w1 = mButton.getWidth();
            double h0 = mHBox.getHeight();
            double size = Math.min(w0-w1, h0);
            Canvas canvas = new Canvas(size, size);
            mHBox.getChildren().add(0, canvas);
        }});
    }
}

Problem with the Canvas is that is is not resizable :(

Dmitry Mitskevich
  • 4,946
  • 2
  • 15
  • 8