Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » e(fx)clipse » Fitting a dialog to the size of its content
Fitting a dialog to the size of its content [message #1229535] Thu, 09 January 2014 16:53 Go to next message
Joseph Gagnon is currently offline Joseph Gagnon
Messages: 58
Registered: June 2013
Member
I have a Dialog extension (an extension of org.eclipse.fx.ui.dialogs.Dialog) that contains various subpanels containing categories of search criteria a user may select from. Each panel is implemented as a TitledPane with the ability to collapse set to false. Initially I hardcoded the dialog size, but now I'd like to have the dialog size itself based upon its content. So, I tried the following:

...
import org.eclipse.fx.ui.dialogs.Dialog;

public class MessageSearchDialog extends Dialog {

  public MessageSearchDialog(Window parent, String title) {
    super(parent, title);
  }

  @Override
  protected Node createDialogArea() {
    BorderPane pane = new BorderPane();
    pane.setPadding(new Insets(5));
    Node searchPane = createSearchPane();
    pane.setCenter(searchPane);
    Bounds bounds = searchPane.getLayoutBounds();
    pane.setPrefSize(bounds.getWidth(), bounds.getHeight());
//    pane.setPrefSize(800, 450);
    return pane;
  }

  ...
}


Unfortunately, when the dialog is opened, it is sized as though all of the TitledPane panels were collapsed (i.e. only the title portion of the pane is visible, the content is not). I can resize the window with the mouse and everything then appears as the dialog window is enlarged. I also tried .getBoundsInParent(), but that made no difference.

Is there a way to make this work without having to hardcode the size?
Re: Fitting a dialog to the size of its content [message #1229589 is a reply to message #1229535] Thu, 09 January 2014 19:33 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5422
Registered: July 2009
Senior Member
To get the preferred size you need to:

pane.impl_reapplyCSS(); // none public API
double w = pane.prefWidth(-1);
double h = pane.prefHeight(-1);
s.setWidth(w);
s.setHeight(h);

Tom

On 09.01.14 17:53, Joseph Gagnon wrote:
> I have a Dialog extension (an extension of
> org.eclipse.fx.ui.dialogs.Dialog) that contains various subpanels
> containing categories of search criteria a user may select from. Each
> panel is implemented as a TitledPane with the ability to collapse set to
> false. Initially I hardcoded the dialog size, but now I'd like to have
> the dialog size itself based upon its content. So, I tried the following:
>
>
> ..
> import org.eclipse.fx.ui.dialogs.Dialog;
>
> public class MessageSearchDialog extends Dialog {
>
> public MessageSearchDialog(Window parent, String title) {
> super(parent, title);
> }
>
> @Override
> protected Node createDialogArea() {
> BorderPane pane = new BorderPane();
> pane.setPadding(new Insets(5));
> Node searchPane = createSearchPane();
> pane.setCenter(searchPane);
> Bounds bounds = searchPane.getLayoutBounds();
> pane.setPrefSize(bounds.getWidth(), bounds.getHeight());
> // pane.setPrefSize(800, 450);
> return pane;
> }
>
> ...
> }
>
>
> Unfortunately, when the dialog is opened, it is sized as though all of
> the TitledPane panels were collapsed (i.e. only the title portion of the
> pane is visible, the content is not). I can resize the window with the
> mouse and everything then appears as the dialog window is enlarged. I
> also tried .getBoundsInParent(), but that made no difference.
>
> Is there a way to make this work without having to hardcode the size?
Re: Fitting a dialog to the size of its content [message #1229603 is a reply to message #1229589] Thu, 09 January 2014 20:01 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5422
Registered: July 2009
Senior Member
Well I looked at the code and I don't understand why you need to do this.

If you set nothing the Dialog will open in its preferred size. Anyways
I've filed a bug and added 2 more methods:

* getInitialSize
* getInitialLocation

which are consulted before the Stage is opened. Please note these are
the stage values. You need to add the window decorations height!

Tom

> /*******************************************************************************
> * Copyright (c) 2014 __COMPANY/CONTRIBUTOR__ and others.
> * All rights reserved. This program and the accompanying materials
> * are made available under the terms of the Eclipse Public License v1.0
> * which accompanies this distribution, and is available at
> * http://www.eclipse.org/legal/epl-v10.html
> *
> * Contributors:
> * tomschindl<__EMAIL__> - initial API and implementation
> *******************************************************************************/
> package org.eclipse.fx.ui.dialogs;
>
> import javafx.application.Application;
> import javafx.event.ActionEvent;
> import javafx.event.EventHandler;
> import javafx.scene.Node;
> import javafx.scene.Scene;
> import javafx.scene.control.Button;
> import javafx.scene.control.TextField;
> import javafx.scene.layout.HBox;
> import javafx.stage.Stage;
>
> /**
> * @author tomschindl
> *
> */
> public class TestIt extends Application {
>
> /**
> * @param args
> */
> public static void main(String[] args) {
> Application.launch(args);
> }
>
> /* (non-Javadoc)
> * @see javafx.application.Application#start(javafx.stage.Stage)
> */
> @Override
> public void start(Stage primaryStage) throws Exception {
> Button b = new Button("Hello");
> b.setOnAction(new EventHandler<ActionEvent>() {
>
> @Override
> public void handle(ActionEvent event) {
> TitleAreaDialog d = new TitleAreaDialog(primaryStage,"Hello World","Hello World 2","Message",(String)null) {
>
> @Override
> protected Node createDialogContent() {
> HBox b = new HBox();
> b.getChildren().addAll(new TextField(),new TextField(),new TextField(),new TextField(),new TextField());
> return b;
> }
> };
> d.open();
> }
> });
> Scene s = new Scene(b);
> primaryStage.setScene(s);
> primaryStage.show();
> }
>
> }


On 09.01.14 20:33, Tom Schindl wrote:
> To get the preferred size you need to:
>
> pane.impl_reapplyCSS(); // none public API
> double w = pane.prefWidth(-1);
> double h = pane.prefHeight(-1);
> s.setWidth(w);
> s.setHeight(h);
>
> Tom
>
> On 09.01.14 17:53, Joseph Gagnon wrote:
>> I have a Dialog extension (an extension of
>> org.eclipse.fx.ui.dialogs.Dialog) that contains various subpanels
>> containing categories of search criteria a user may select from. Each
>> panel is implemented as a TitledPane with the ability to collapse set to
>> false. Initially I hardcoded the dialog size, but now I'd like to have
>> the dialog size itself based upon its content. So, I tried the following:
>>
>>
>> ..
>> import org.eclipse.fx.ui.dialogs.Dialog;
>>
>> public class MessageSearchDialog extends Dialog {
>>
>> public MessageSearchDialog(Window parent, String title) {
>> super(parent, title);
>> }
>>
>> @Override
>> protected Node createDialogArea() {
>> BorderPane pane = new BorderPane();
>> pane.setPadding(new Insets(5));
>> Node searchPane = createSearchPane();
>> pane.setCenter(searchPane);
>> Bounds bounds = searchPane.getLayoutBounds();
>> pane.setPrefSize(bounds.getWidth(), bounds.getHeight());
>> // pane.setPrefSize(800, 450);
>> return pane;
>> }
>>
>> ...
>> }
>>
>>
>> Unfortunately, when the dialog is opened, it is sized as though all of
>> the TitledPane panels were collapsed (i.e. only the title portion of the
>> pane is visible, the content is not). I can resize the window with the
>> mouse and everything then appears as the dialog window is enlarged. I
>> also tried .getBoundsInParent(), but that made no difference.
>>
>> Is there a way to make this work without having to hardcode the size?
>
Re: Fitting a dialog to the size of its content [message #1229605 is a reply to message #1229589] Thu, 09 January 2014 20:06 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5422
Registered: July 2009
Senior Member
The correct none API method is "impl_processCSS(true);"

Tom

On 09.01.14 20:33, Tom Schindl wrote:
> To get the preferred size you need to:
>
> pane.impl_reapplyCSS(); // none public API
> double w = pane.prefWidth(-1);
> double h = pane.prefHeight(-1);
> s.setWidth(w);
> s.setHeight(h);
>
> Tom
>
> On 09.01.14 17:53, Joseph Gagnon wrote:
>> I have a Dialog extension (an extension of
>> org.eclipse.fx.ui.dialogs.Dialog) that contains various subpanels
>> containing categories of search criteria a user may select from. Each
>> panel is implemented as a TitledPane with the ability to collapse set to
>> false. Initially I hardcoded the dialog size, but now I'd like to have
>> the dialog size itself based upon its content. So, I tried the following:
>>
>>
>> ..
>> import org.eclipse.fx.ui.dialogs.Dialog;
>>
>> public class MessageSearchDialog extends Dialog {
>>
>> public MessageSearchDialog(Window parent, String title) {
>> super(parent, title);
>> }
>>
>> @Override
>> protected Node createDialogArea() {
>> BorderPane pane = new BorderPane();
>> pane.setPadding(new Insets(5));
>> Node searchPane = createSearchPane();
>> pane.setCenter(searchPane);
>> Bounds bounds = searchPane.getLayoutBounds();
>> pane.setPrefSize(bounds.getWidth(), bounds.getHeight());
>> // pane.setPrefSize(800, 450);
>> return pane;
>> }
>>
>> ...
>> }
>>
>>
>> Unfortunately, when the dialog is opened, it is sized as though all of
>> the TitledPane panels were collapsed (i.e. only the title portion of the
>> pane is visible, the content is not). I can resize the window with the
>> mouse and everything then appears as the dialog window is enlarged. I
>> also tried .getBoundsInParent(), but that made no difference.
>>
>> Is there a way to make this work without having to hardcode the size?
>
Re: Fitting a dialog to the size of its content [message #1229988 is a reply to message #1229603] Fri, 10 January 2014 18:43 Go to previous messageGo to next message
Joseph Gagnon is currently offline Joseph Gagnon
Messages: 58
Registered: June 2013
Member
You're quite correct, I don't need to specifically size the dialog window (not sure why I originally thought I did). If I remove the code that sets the dialog size, it sizes itself to the content, which is what I wanted. Thanks.

[Updated on: Fri, 10 January 2014 18:44]

Report message to a moderator

Re: Fitting a dialog to the size of its content [message #1229996 is a reply to message #1229605] Fri, 10 January 2014 19:03 Go to previous messageGo to next message
Joseph Gagnon is currently offline Joseph Gagnon
Messages: 58
Registered: June 2013
Member
Now I have a different twist to the story. I'd like to have one of the search category panels (implemented as a TitledPane) be collapsible and have the dialog window resize itself to the current bounds of the content when the collapsible pane is expanded or collapsed.

I experimented a little with your suggestion:

pane.impl_processCSS(true); // none public API
double w = pane.prefWidth(-1);
double h = pane.prefHeight(-1);
s.setWidth(w);
s.setHeight(h);


but I cannot figure out how to affect the dialog size - I can't find accessor methods to set width or height. Your exmaple looks to be setting the width and height of the scene object, but I don't know how to get a handle to that in my dialog.

Another thing that I noticed when I did some tests was that it seems that the width and height returned from the impl_processCSS() call is the "old" size. Basically what I did was attach a ChangeListener to the expanded property of the TitledPane referred to earlier and printed out the width and height returned. The values printed always seemed to be the "old" size of the dialog, the reverse of what I need.

  private Node createSearchPane() {
    final GridPane pane = new GridPane();

    ...
    code to set up other panels
    ...

    TitledPane msgTypePane = new TitledPane("Type", createMsgTypePane());
    msgTypePane.setCollapsible(true);
    msgTypePane.setExpanded(false);
    msgTypePane.expandedProperty().addListener(new ChangeListener<Boolean>() {
      @Override
      public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
        System.out.println(newValue);
        pane.impl_processCSS(true);
        double width = pane.prefWidth(-1);
        double height = pane.prefHeight(-1);
        System.out.println(width + "," + height);
      }
    });
    pane.add(msgTypePane, 0, 3, 4, 1);

    return pane;
}


So, with all of that said, what is a good way to dynamically resize the dialog window to handle the changes to the content bounds due to the collapse/expand of an item in the dialog?
Re: Fitting a dialog to the size of its content [message #1230092 is a reply to message #1229996] Sat, 11 January 2014 00:39 Go to previous messageGo to next message
Dennis Lee is currently offline Dennis Lee
Messages: 32
Registered: September 2013
Member
Well maybe it is different story and may not work your cases but I needed to re-size the Dialog to its (dynamic) contents panel. If it is different from your case, sorry for distraction but just in case:

Stage has sizeToScene() method, but the stage in Dialog is private.
private Stage stage;


So I overridden create method in subclass of Dialog and keep the reference to stage like this,
//This is another one from sub class of Dialog
private Stage stage; 

@Override
protected Stage create() {
    return (stage = super.create());
}


And whenever the contents changed, I called
stage.sizeToScene()
method to make the Dialog fit to the contents node.

[Updated on: Sat, 11 January 2014 04:46]

Report message to a moderator

Re: Fitting a dialog to the size of its content [message #1230206 is a reply to message #1230092] Sat, 11 January 2014 09:28 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5422
Registered: July 2009
Senior Member
I'll add API to resize the dialog later on.

Tom

On 11.01.14 01:39, Dennis Lee wrote:
> Well maybe it is different story and may not work your cases but I
> needed to re-size the Dialog to its (dynamic) contents panel. If it is
> different from your case, sorry for distraction but just in case:
>
> Stage has sizeToScene() method, but the stage in Dialog is private.
> private Stage stage;
>
> So I overridden create method in subclass of Dialog and keep the
> reference to stage like this,
> @Override
> protected Stage create() {
> return (stage = super.create());
> }
>
>
> And whenever the contents changed, I called stage.sizeToScene() method
> to make the Dialog fit to the contents node.
>
>
Re: Fitting a dialog to the size of its content [message #1230287 is a reply to message #1230206] Sat, 11 January 2014 15:17 Go to previous messageGo to next message
Dennis Lee is currently offline Dennis Lee
Messages: 32
Registered: September 2013
Member
That's great. Thanks.
Re: Fitting a dialog to the size of its content [message #1230306 is a reply to message #1230287] Sat, 11 January 2014 16:12 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5422
Registered: July 2009
Senior Member
Hi,

The API is in [1] and will be published with the next nightly builds.

Tom

[1]https://bugs.eclipse.org/bugs/show_bug.cgi?id=425480

On 11.01.14 16:17, Dennis Lee wrote:
> That's great. Thanks.
>
Re: Fitting a dialog to the size of its content [message #1270881 is a reply to message #1230206] Thu, 13 March 2014 18:28 Go to previous messageGo to next message
Dennis Lee is currently offline Dennis Lee
Messages: 32
Registered: September 2013
Member
Hi Tom,

It look like the API you added is missing in the latest package. I download pre-built SDK from http://downloads.efxclipse.bestsolution.at/downloads/nightly/sdk/ today and noticed that pack() is missing.

Thanks.
Re: Fitting a dialog to the size of its content [message #1270885 is a reply to message #1270881] Thu, 13 March 2014 18:37 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5422
Registered: July 2009
Senior Member
The SDK is not responsible for the target - you need to point your
target to the correct update-site.

Tom

On 13.03.14 19:28, Dennis Lee wrote:
> Hi Tom,
>
> It look like the API you added is missing in the latest package. I
> download pre-built SDK from
> http://downloads.efxclipse.bestsolution.at/downloads/nightly/sdk/ today
> and noticed that pack() is missing.
>
> Thanks.
>
Re: Fitting a dialog to the size of its content [message #1270916 is a reply to message #1270885] Thu, 13 March 2014 20:40 Go to previous message
Dennis Lee is currently offline Dennis Lee
Messages: 32
Registered: September 2013
Member
Sorry and Thank you. I thought the SDK has all. I'll setup and try.

Dennis

Thomas Schindl wrote on Thu, 13 March 2014 14:37
The SDK is not responsible for the target - you need to point your
target to the correct update-site.

Tom

On 13.03.14 19:28, Dennis Lee wrote:
> Hi Tom,
>
> It look like the API you added is missing in the latest package. I
> download pre-built SDK from
> http://downloads.efxclipse.bestsolution.at/downloads/nightly/sdk/ today
> and noticed that pack() is missing.
>
> Thanks.
>

Previous Topic:Cleaning up controller instances attached to MParts in an E4 application
Next Topic:Can't get any e(fx)eclipse 0.9.0 or 1.0 version to work with 3.x RCP
Goto Forum:
  


Current Time: Sat Oct 25 17:32:11 GMT 2014

Powered by FUDForum. Page generated in 0.02694 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software