Results 1 to 16 of 16
  1. #1
    Kenan_89 is offline Member
    Join Date
    Jan 2012
    Posts
    25
    Rep Power
    0

    Default Put a container into a scrollpane, but position centered

    Hi,

    my problem is, I want to put a centered Container into a scrollpane in JavaFX.

    I tried it this way:
    1. create a scrollpane
    2. create a gridpane, with column- and rowconstraints to 100% (setPercentHeight, setPercentWidth)
    3. create a last container
    4. added this last container into the gridpane(so it should be showed centered, because of the row-column-constraints)
    5. added the gridpane as content of scrollpane

    The point is, that the gridpane doesn`t take the free area of the scrollpane, although i set the row and column up to 100%.

    The result should be look like this:


    Thanks for help.
    Last edited by Kenan_89; 12-12-2012 at 08:59 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Put a container into a scrollpane, but position centered

    I'm sure I must be missing something, but I don't understand the need for a scroll pane to display a container that's not large enough to require scrolling. To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem, and that we can play with and try to tweak to meet your requirement.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Kenan_89 is offline Member
    Join Date
    Jan 2012
    Posts
    25
    Rep Power
    0

    Default Re: Put a container into a scrollpane, but position centered

    Quote Originally Posted by DarrylBurke View Post
    I'm sure I must be missing something, but I don't understand the need for a scroll pane to display a container that's not large enough to require scrolling. To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem, and that we can play with and try to tweak to meet your requirement.

    db
    Hello,

    thank you for your post.
    The container in center has a variable size. Because of that I need the scrollpane. But if it is smaller
    it should display centered.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Put a container into a scrollpane, but position centered

    So, where's that SSCCE?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Kenan_89 is offline Member
    Join Date
    Jan 2012
    Posts
    25
    Rep Power
    0

    Default Re: Put a container into a scrollpane, but position centered

    Quote Originally Posted by DarrylBurke View Post
    So, where's that SSCCE?

    db
    Sorry, what do you mean with SSCCE?
    Do you mean an example code? If so, I can
    upload in some hours, because I am not at home now.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Put a container into a scrollpane, but position centered

    Quote Originally Posted by Kenan_89 View Post
    Sorry, what do you mean with SSCCE?
    Since it appears that you're not reading my responses, I might as well have left this topic alone.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    Kenan_89 is offline Member
    Join Date
    Jan 2012
    Posts
    25
    Rep Power
    0

    Default Re: Put a container into a scrollpane, but position centered

    Quote Originally Posted by DarrylBurke View Post
    Since it appears that you're not reading my responses, I might as well have left this topic alone.

    db
    Sorry, my fault.

    Here is the example code:

    PHP Code:
    public class App extends Application{
    	
    	public static void main(String[] args){
    		Application.launch(args);
    	}
    		
    	public void start(Stage rootStage){
    		GridPane rootPane = new GridPane();
    		Scene scene = new Scene(rootPane, 500, 500);
    		rootStage.setScene(scene);
    		rootStage.show();
    		
    		RowConstraints row = new RowConstraints();
    		ColumnConstraints col = new ColumnConstraints();
    		
    		row.setPercentHeight(100);
    		col.setPercentWidth(100);
    		
    		rootPane.getColumnConstraints().add(col);
    		rootPane.getRowConstraints().add(row);
    		
    		ScrollPane scroll = new ScrollPane();
    		StackPane centeredPane = new StackPane();
    		centeredPane.setStyle("-fx-border-color: red");
    		centeredPane.setPrefSize(200, 200);
    		centeredPane.getChildren().add(new Button("TEST"));
    		
    		scroll.setHbarPolicy(ScrollBarPolicy.ALWAYS);
    		scroll.setVbarPolicy(ScrollBarPolicy.ALWAYS);
    		scroll.setContent(centeredPane);
    		
    		rootPane.add(scroll, 0, 0);
    	}
    
    }
    The StackPane should be shown in center.

    Currently it looks like:
    Last edited by Kenan_89; 12-13-2012 at 01:37 PM.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Put a container into a scrollpane, but position centered

    First, the order of nesting is wrong for what you are trying to achieve. Second, there are other properties of the constraints that can be leveraged.

    Is this what you wanted to see?
    Java FX Code:
    import javafx.application.Application;
    import javafx.geometry.HPos;
    import javafx.geometry.VPos;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.ScrollPane;
    import javafx.scene.control.ScrollPane.ScrollBarPolicy;
    import javafx.scene.layout.ColumnConstraints;
    import javafx.scene.layout.GridPane;
    import javafx.scene.layout.RowConstraints;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    
    public class App extends Application {
    
      public static void main(String[] args) {
        Application.launch(args);
      }
    
      public void start(Stage rootStage) {
        StackPane centeredPane = new StackPane();
        centeredPane.setStyle("-fx-border-color: red");
        centeredPane.setPrefSize(200, 200);
        centeredPane.getChildren().add(new Button("TEST"));
        
        GridPane outerPane = new GridPane();
        RowConstraints row = new RowConstraints();
        row.setPercentHeight(100);
        row.setFillHeight(false);
        row.setValignment(VPos.CENTER);
        outerPane.getRowConstraints().add(row);
        
        ColumnConstraints col = new ColumnConstraints();
        col.setPercentWidth(100);
        col.setFillWidth(false);
        col.setHalignment(HPos.CENTER);
        outerPane.getColumnConstraints().add(col);
    
        outerPane.add(centeredPane, 0, 0);
        
        ScrollPane rootPane = new ScrollPane();
        rootPane.setHbarPolicy(ScrollBarPolicy.ALWAYS);
        rootPane.setVbarPolicy(ScrollBarPolicy.ALWAYS);
        rootPane.setContent(outerPane);
    
        Scene scene = new Scene(rootPane, 500, 500);
        rootStage.setScene(scene);
        rootStage.show();
      }
    }
    For your future reference, a SSCCE is incomplete without imports.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    Kenan_89 is offline Member
    Join Date
    Jan 2012
    Posts
    25
    Rep Power
    0

    Default Re: Put a container into a scrollpane, but position centered

    Hmm, on my PC the result of your code looks like this:

    My problem is to get the pane with red border horizontal and vertical centered in my scrollpane.
    But thanks for your try and the time :)

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Put a container into a scrollpane, but position centered

    This is what I get:
    Put a container into a scrollpane, but position centered-app.png

    Delete App.class and recompile.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  11. #11
    Kenan_89 is offline Member
    Join Date
    Jan 2012
    Posts
    25
    Rep Power
    0

    Default Re: Put a container into a scrollpane, but position centered

    Sorry but what do you mean with App.class? I cant see it anywhere in my code.

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Put a container into a scrollpane, but position centered

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  13. #13
    Kenan_89 is offline Member
    Join Date
    Jan 2012
    Posts
    25
    Rep Power
    0

    Default Re: Put a container into a scrollpane, but position centered

    I created a completly new project with your code but the result is the same. I am working with IDE eclipse.
    Which FX version do you use?

  14. #14
    Kenan_89 is offline Member
    Join Date
    Jan 2012
    Posts
    25
    Rep Power
    0

    Default Re: Put a container into a scrollpane, but position centered

    I found a way to realize it. I don`t think it is nice, but I currently don`t see another
    way:

    1.execute the method "setFitToHeight(true)" and "setFitToWidth(true)" on the scrollPane
    2. now, you have to resize the outerPane AND the pane in the center concurrently because if you
    just give the centered pane a size the scrollpane doesn`t react. For the outerPane you have to
    give a size with method: "setMinHeight() setMinWidth()"

    For now it works but I will look for the correct solution further.

  15. #15
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Put a container into a scrollpane, but position centered

    Quote Originally Posted by Kenan_89 View Post
    I created a completly new project with your code but the result is the same. I am working with IDE eclipse.
    Which FX version do you use?
    What's your JavaFX version? Mine is 2.2.3.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  16. #16
    Kenan_89 is offline Member
    Join Date
    Jan 2012
    Posts
    25
    Rep Power
    0

    Default Re: Put a container into a scrollpane, but position centered

    I hava Java 7 Update 9 installed. I think it must be 2.2.4. But I have to look. I give you the answer later.

Similar Threads

  1. drawing a centered grid with JFrame
    By adonaldson in forum AWT / Swing
    Replies: 3
    Last Post: 02-19-2012, 10:01 PM
  2. Replies: 3
    Last Post: 02-05-2012, 07:10 AM
  3. scrollpane does not resize based on the container
    By preeta in forum AWT / Swing
    Replies: 3
    Last Post: 04-08-2011, 07:24 PM
  4. Replies: 3
    Last Post: 04-22-2009, 12:01 AM
  5. Replies: 2
    Last Post: 10-30-2008, 12:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •