-3

whenever I try to full screen my application, it doesn't scale. I've made multiple copies of this application trying different methods but none seem to work right.

First attempt: Application was a Parent, it would scale the background but the elements inside wouldn't scale to screen size.

As a Parent

As an update: here is the actual Parent that was made. The layout is the original one I wrote and has no issues when it's windowed. It has a preset WIDTH and HEIGHT but when full screened, The first example picture is what it looks like where the WIDTH of the the TextField doesn't update (since it's preset and not updating to the highest WIDTH of the screen it's running on). There are two parts to this that CAN be fixed when only one is fixed. The displayed Text has a set wrapping length of the console, though it is set by using WIDTH.

Here's what the console looks like when it's windowed: enter image description here

If I could find a way to change the WIDTH, I'm thinking this can be fixed for both the TextField and the setWrappingWidth().

package application.console;

import application.areas.startingArea.SA;
import application.areas.vanguardForest.VFCmds;
import application.areas.vanguardForest.VFNavi;
import application.areas.vanguardForest.VFPkups;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Parent;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ScrollPane.ScrollBarPolicy;
import javafx.scene.control.TextField;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

public class Ce extends Region {

    public static boolean fullscreen = false;
    
    public static double WIDTH = 990;
//          990;
//          Screen.getPrimary().getBounds().getMaxX();
    public static double HEIGHT = 525;
//          525;
//          Screen.getPrimary().getBounds().getMaxY();
    
    public static Font Cinzel = (Font.loadFont("file:fonts/static/Cinzel-Medium.ttf", 16));
    
    public static VBox console = new VBox(2);
    public static TextField input = new TextField();
    public static ScrollPane scroll = new ScrollPane();
            
    public static BorderPane root = new BorderPane();

    public static String s;
    
    public static Parent Window() {
        
        root.setMinSize(WIDTH, (HEIGHT - input.getHeight()));
        root.setStyle("-fx-background-color: #232323;");
        
        scroll.setContent(console);
        root.setCenter(scroll);
        scroll.setStyle("-fx-background: #232323;"
                + "-fx-background-color: transparent;"
                + "-fx-border-color: #232323;"
                + "-fx-focus-color: #232323;"
                );
        scroll.setHbarPolicy(ScrollBarPolicy.NEVER);
        scroll.setVbarPolicy(ScrollBarPolicy.NEVER);
        scroll.setBackground(new Background(new BackgroundFill(Color.TRANSPARENT, null, null)));
        
        console.setStyle("-fx-background-color: #232323;"
                + "-fx-focus-color: #232323;");
        
        console.heightProperty().addListener(new ChangeListener<Object>() {
            @Override
            public void changed(ObservableValue<?> observable, Object oldValue, Object newValue) {
                
                scroll.setVvalue((Double)newValue);
            }
            
        });
        
        HBox hbox = new HBox();
        hbox.setPrefSize(WIDTH, 16);
        root.setBottom(hbox);
        
        Text carrot = new Text(" >");
        carrot.setFont(Font.loadFont("file:fonts/static/Cinzel-Medium.ttf", 26));
        carrot.setFill(Color.WHITE);
        
        input.setStyle("-fx-background-color: transparent;"
                + "-fx-text-fill: #FFFFFF;"
                + "-fx-highlight-fill: #FFFFFF;"
                + "-fx-highlight-text-fill: #232323;"
//              + "-fx-border-color: #FFFFFF;"
//              + "-fx-border-width: .5;"
                );
        input.setFont(Cinzel);
        input.setMinWidth(console.getWidth());
        
        input.setOnAction(e -> {
            
            String s = (input.getText()).stripTrailing();

            input.clear();
        });
        
        Pane pane = new Pane();
        
        root.getChildren().add(pane);
        
        hbox.getChildren().addAll(carrot, input);
        
        return root;
    }

This isn't the main issue as I've stated, once getting the scaled width for the TextField the process of for setWrappingWidth() for displaying the text should be the if a solution is found, here's how it goes:

@SuppressWarnings("static-access")
    public void print(String s, Color c) {
        Ce Ce = new Ce();
        HBox text1 = new HBox();
        text1.setMinWidth(Ce.WIDTH);
        text1.setMaxWidth(Ce.WIDTH);
        
        Text tCarrot = new Text(" > ");
        tCarrot.setFont(Ce.Cinzel);
        tCarrot.setFill(c);
        
        Text text2 = new Text();
        final IntegerProperty i = new SimpleIntegerProperty(0);
        Timeline tl = new Timeline();
        KeyFrame kf = new KeyFrame(
                Duration.seconds(textSpeed(fastText)),
                e1 -> {
                    if(i.get() > s.length()) {
                        tl.stop();
                    } else {
                        text2.setText(s.substring(0, i.get()));
                        i.set(i.get() + 1);
                    }
                });
        
        tl.getKeyFrames().add(kf);
        tl.setCycleCount(Animation.INDEFINITE);
        tl.play();
        
        text2.setFill(c);
        text2.setFont(Ce.Cinzel);
        text2.setWrappingWidth(Ce.WIDTH - 40);
        
        text1.getChildren().addAll(tCarrot, text2);
        
        Ce.console.getChildren().add(text1);
        
        Ce.console.setMargin(text1, new Insets(5, 0, 0, 3));

    }   

Lastly, the HEIGHT of the VBox for the displayed Text works just as intended, it's just the setting/updating the WIDTH to set it to the size of the window whether Windowed of Full screened that is the main issue here.

Providence
  • 59
  • 5
  • Before some users say "This code isn't what it's meant for". This is trying to find solutions. or even ways to get it to work. If any suggestions suggest to keep it as a `Parent` then so be it. I'm looking at different ways to get this to work and finding solutions that only partially work with little to no advice onto HOW to make it work. Any help/advice to make this work would be greatly appreciated. – Providence Nov 29 '22 at 06:33
  • 3
    But I don’t know what you are trying to do. You have asked numerous questions but in none of them can I understand what exactly you are trying to achieve. Everything appears to be [xy problems](https://en.m.wikipedia.org/wiki/XY_problem). You can make an app [full screen by setting the property](https://openjfx.io/javadoc/19/javafx.graphics/javafx/stage/Stage.html#fullScreenProperty), but what you display on the screen depends on what you want to display. How is the display while full screen any different than the display while windowed? What does it mean to scale in this context? – jewelsea Nov 29 '22 at 08:00
  • Perhaps providing a UI mock up picture of a layout you want to achieve as an image may help. For example like [this question](https://stackoverflow.com/questions/16094482/best-way-to-create-a-flexible-3-box-layout-in-javafx-fxml/16095034#16095034). – jewelsea Nov 29 '22 at 08:16
  • Maybe try using scene builder with its preview function and try creating your UI in that (removing the default pref width and height it assigns to components and adding appropriate sizing hints like setting the vgrow of hgrow priority). I am guessing the layout portion of what you want to achieve can all be done in FXML and maybe some CSS to fit images, with no Java code, but that is just a guess without understanding exactly what you want. You may want to apply translate and scale transform in Java but I am guessing not. – jewelsea Nov 29 '22 at 08:29
  • 3
    Why hardcode WIDTH and HEIGHT, set layout x and y on items, set negative layout values and negative hbox spacing, use a fixed placement pane like an anchor pane, subclass Region and override layout children? For most tasks, I would not recommend any of those things. Instead, use resizable layout panes with layout hints, letting the layout panes work out the correct sizing and placement for you. – jewelsea Nov 29 '22 at 08:41
  • 2
    1. Stop thinking about “full screen”. It has nothing to do with your problem. 2. Don’t hard code any sizes or layout positions anywhere in your application. (You can add hard-coded padding and spacing later once the layout is working if you want.) 3. Read the [layout tutorial](https://docs.oracle.com/javafx/2/layout/builtin_layouts.htm) and [documentation](https://openjfx.io/javadoc/17/javafx.graphics/javafx/scene/layout/package-summary.html). 4. Ask yourself how you want the layout to behave if there is more space available than it needs. 5. Choose appropriate layout panes and settings. – James_D Nov 29 '22 at 11:29
  • 1
    [This](https://stackoverflow.com/questions/46453780/javafx-how-i-can-bind-a-stage-with-screen-resolution/46455368#46455368) is `FXML`, but the ideas are the same. See if it can help. Also [This](https://stackoverflow.com/questions/74366028/how-to-make-anchorpane-size-responsive-to-the-maximized-screen-view/74367570#74367570) or [this](https://stackoverflow.com/questions/73618261/why-does-adding-another-element-to-a-borderpane-mess-up-my-formatting/73627543#73627543). I suggest you try building the static part of your GUI in FXML. – SedJ601 Nov 29 '22 at 13:50
  • @jewelsea That's the issue. The layout of the application doesn't scale accordingly. There are pictures of what the issue looks like in various of different methods that have been attempted. The layout when windowed has the look that is originally given to it, What you see in the third picture is what the layout looks like when it is windowed minus the white background. If you're talking about "just set full screen to true", it literally doesn't work which is why I've been asking. All of my questions have been about scaling and setting the layout of this application when full screened. – Providence Nov 29 '22 at 22:30
  • @jewelsea The first picture of the full screened application is what it's supposed to look like, if the width of the `VBox` and `TextField` scaled accordingly. That is the closest I've gotten to getting it the way it's intended to be. I've looked everywhere to find a way how to scale it which seems like just a simple fix since the `TextField` X cords goes to where it needs to be. – Providence Nov 29 '22 at 22:32
  • @James_D I would stop worrying about "Full screen" if it wasn't part of the issue. The issue is based on full screening, the gist is on getting it to scale accordingly. If you've looked at what I've said previously. Getting it as a `Parent` was the closest I've gotten it and when trying to do it your way you just said "You completely misunderstood my answer". The questions you're asking doesn't mean anything to what I'm trying to achieve. The simplicity of the question is "How to get the full screened application to scale accordingly". – Providence Nov 29 '22 at 22:39
  • All there's really to it is getting the `Parent` to update the `WIDTH` of the `TextField` and `VBox` when full screened. – Providence Nov 29 '22 at 22:42
  • 2
    at the end of the day there is no way around learning all about layouts.. writing your own or interfering with an existing most probably is wrong (which is what all tag experts - f.i. @James_D - are trying to get over to you since several questions and you insist on ignoring) .. start _learning_, the sooner the better – kleopatra Nov 29 '22 at 23:06
  • 1
    Post a [mre] that reproduces the picture that is closest to what you want. Delete everything else. Explain *clearly* what it is not doing that you want it to do. Use terminology correctly: only use the word “scale” if that is what you mean. (Scaling the text field would mean that the text inside it also increased in size, etc.) – James_D Nov 29 '22 at 23:45
  • 1
    *’I would stop worrying about “Full Screen” if it wasn’t part of the issue’*. So the issue only arises in full screen mode? It doesn’t arise if you just change the size of the size of the window another way??? I do not believe that. If it’s really true, you need to post code we can copy, paste, and run unmodified to reproduce the issue. But again, I do not believe you. There is no point in you posting here if you are simply going to argue with and ignore the experts who are trying to help you. – James_D Nov 29 '22 at 23:54
  • @James_D I'm sorry about my frustrations, I've been getting nowhere with this issue for a whole month. I've updated the question so it's more clear and CAN be reproduced. Showing images of what it looks like in Windowed mode and what it looks like in full screened. – Providence Nov 30 '22 at 00:54
  • @James_D I figured about scaling the `TextField` was going to change the size of the font. I'm just looking for a way to update the `WIDTH` or set the `WIDTH` to the size of the window whether in Windowed mode or Full Screen. Same goes for displaying the `setWrappingWidth()` for the displayed `Text`. – Providence Nov 30 '22 at 00:55
  • 1
    Of course you’re getting nowhere. What do you expect to happen when you completely ignore everything everyone on here has been telling you? Basically your question is “I am hardcoding the width of my components to a fixed value. Why doesn’t the width of my component change when the window changes size”. And over and over again we tell you “don’t hard code any sizes, let the layout panes manage the size for you”. And you continue to ignore us. You are wasting everyone’s time (including your own). – James_D Nov 30 '22 at 02:52

2 Answers2

1

Try this app. It will not be exactly what you want but may provide some useful help for you if you study it, if not just ignore it, tears can keep you blind, and sometimes, that is ok.

cunning love

The implementation follows the suggestions you have received in the comments on your questions which together explain what is being done and why, so I won't provide much commentary on the solution here.

Type text in the input bar, press enter and it will appear in the listview for the console log. Use the Toggle full-screen button to toggle full-screen mode on or off.

Console.java

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Console extends VBox {
    private final ObservableList<String> consoleLog = FXCollections.observableArrayList();
    private final ListView<String> logView = new ListView<>(consoleLog);

    public Console(Stage stage) {
        VBox.setVgrow(logView, Priority.ALWAYS);

        HBox ribbon = createRibbon(
                createFullScreenToggle(stage)
        );
        ribbon.setMinHeight(HBox.USE_PREF_SIZE);

        getChildren().addAll(
                ribbon,
                logView
        );
    }

    private ToggleButton createFullScreenToggle(Stage stage) {
        ToggleButton fullScreenToggle = new ToggleButton("Toggle full screen");
        fullScreenToggle.setOnAction(e ->
                stage.setFullScreen(
                        fullScreenToggle.isSelected()
                )
        );
        
        return fullScreenToggle;
    }

    private HBox createRibbon(ToggleButton fullscreenToggle) {
        Text prompt = new Text(">");

        TextField input = new TextField();
        input.setOnAction(e -> {
            consoleLog.add(0, input.getText());
            logView.scrollTo(0);

            input.clear();
        });
        HBox.setHgrow(input, Priority.ALWAYS);

        HBox ribbon = new HBox(10, 
                prompt, 
                input, 
                fullscreenToggle
        );
        ribbon.setAlignment(Pos.BASELINE_LEFT);

        return ribbon;
    }

    public ObservableList<String> getConsoleLog() {
        return consoleLog;
    }
}

ConsoleApplication.java

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class ConsoleApplication extends Application {
    @Override
    public void start(Stage stage) {
        Console console = new Console(stage);
        console.getConsoleLog().addAll(
                TEXT.lines().toList()
        );

        stage.setScene(
                new Scene(
                        console
                )
        );

        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

    private static final String TEXT = """            
            W. Shakespeare - Sonnet 148
            O me, what eyes hath Love put in my head,
            Which have no correspondence with true sight!
            Or, if the have, where is my judgement fled,
            That censures falsely what they see aright?
            If that be fair whereon my false eyes dote,
            What means the world to say it is not so?
            If it be not, then love doth well denote
            Love’s eye is not so true as all men’s ‘No.’
            How can it? O, how can Love’s eye be true,
            That is so vex’d with watching and with tears?
            No marvel then, though I mistake my view;
            The sun itself sees not till heaven clears.
                O cunning Love! with tears thou keep’st me blind.
                Lest eyes well-seeing thy foul faults should find.
            """;
}
jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • Thank you for the help, I'll look into it and mess around with it to see what can come from it. though this is a good example to study from. It's constructed a little bit differently, but this can work. Again thank you and sorry for the hassle. – Providence Nov 30 '22 at 02:50
-1

If you want to increase the nodes height/width according to the viewport, then this's not the best practice, because every user will have the same font size at the end. What you can do is to make the font resizable by either GUI buttons or keyboard/mouse keys.

Here is a modification on your code, that will allow users to use ctrl + mouse wheel to increase/decrease the font (like any browser or terminal):

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.ObjectBinding;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class ConsoleTest extends Application {
    @Override
    public void start(Stage stage) {
        Scene scene = new Scene(new GameWindow().Console(), 600, 600);
        stage.setTitle("Console");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}

class GameWindow {

    public static Console c = new Console();

    public Parent Console() {
        for (int i = 0; i < 100; i++) c.addText(new Text("Test" + i));
        return c;
    }

}

class Console extends BorderPane {

    private final SimpleDoubleProperty fontSize = new SimpleDoubleProperty(20);
    private final ObjectBinding<Font> fontBinding = Bindings.createObjectBinding(() -> Font.font(fontSize.get()), fontSize);

    private final VBox console;

    public Console() {
        console = new VBox();
        console.setBackground(new Background(new BackgroundFill(Color.BLACK, CornerRadii.EMPTY, Insets.EMPTY)));

        ScrollPane scroll = new ScrollPane(console);
        scroll.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
        scroll.setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
        scroll.setFitToHeight(true);
        scroll.setFitToWidth(true);
        scroll.setPadding(Insets.EMPTY);

        Text caret = new Text("  >");
        caret.fontProperty().bind(fontBinding);
        caret.setFill(Color.WHITE);

        TextField input = new TextField();
        input.setStyle("-fx-background-color: transparent;" + "-fx-text-fill: #FFFFFF;" + "-fx-highlight-fill: #FFFFFF;" + "-fx-highlight-text-fill: #232323;");
        input.fontProperty().bind(fontBinding);

        HBox inputBar = new HBox(2, caret, input);
        inputBar.setStyle("-fx-background-color: #232323;");
        inputBar.setAlignment(Pos.CENTER_LEFT);

        setCenter(scroll);
        setBottom(inputBar);

        EventHandler<ScrollEvent> scrollEvent = e -> {
            if (e.isControlDown()) {
                if (e.getDeltaY() > 0) {
                    fontSize.set(fontSize.doubleValue() + 2);
                } else {
                    double old;
                    fontSize.set((old = fontSize.doubleValue()) < 10 ? old : old - 2);
                }
                e.consume();
            }
        };

        inputBar.setOnScroll(scrollEvent);
        console.setOnScroll(scrollEvent);
    }

    public void addText(Text text) {
        text.fontProperty().bind(fontBinding);
        text.setFill(Color.WHITE);
        console.getChildren().add(text);
    }
}
Al-Anazi
  • 364
  • 1
  • 8
  • The user explicitly said: "it's not the text I'm looking for to scale" – mipa Nov 29 '22 at 11:19
  • as much as this seems helpful, the main elements I'm looking to scale are the `TextField` and `Vbox` for the application, the text isn't really too necessary but I will keep this on the backburner incase if it's needed. Thank you, I really do appreciate it. – Providence Nov 29 '22 at 22:52
  • 1
    I think there is probably confusion in terminology. Scale (to me) means to make something larger or smaller for instance a [Scale](https://openjfx.io/javadoc/19/javafx.graphics/javafx/scene/transform/Scale.html) transform used in a [ScaleTransition](https://openjfx.io/javadoc/19/javafx.graphics/javafx/animation/ScaleTransition.html), but I don't think that is what Providence is looking for, as pointed out my mipa, Providence's issues appear related to layout rather than scale. – jewelsea Nov 29 '22 at 23:34