TomJ
TomJ

Reputation: 1813

New window not opening in specified size in JavaFX

Am trying to open a new window when clicked on a button in javafx. When clicked on the button, new Window is opening, but its size not as I expected. I will provide my codes. The following code is of the button -

b2.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent e) {
            setEffect(new BoxBlur(5, 10, 10));
            Stage usrpagestage = new Stage();
            usrpagestage.setMaxHeight(300);
            usrpagestage.setMaxWidth(210);
            usrpagestage.setResizable(false);
            usrpagestage.initStyle(StageStyle.UTILITY);
            try {
                Class.forName("com.mysql.jdbc.Driver");
                connect = DriverManager
                        .getConnection("jdbc:mysql://localhost:3306/project?"
                                + "user=root&password=virus");
                statement = connect.createStatement();
                preparedStatement = connect
                        .prepareStatement("SELECT count(*)FROM information_schema.tables\n" +
                                "WHERE table_schema = 'project' AND table_name = 'subject'");
                rs=preparedStatement.executeQuery();
                rs.next();
                int chk ;
                chk = rs.getInt(1);

              if(chk!=1)
              {
                  usrpagestage.setScene(new Scene(new SubWarning()));
                  usrpagestage.show();
              }

              else
              {
                  usrpagestage.setScene(new Scene(new AddStaff()));
                  usrpagestage.show();
              }
            }
            catch (ClassNotFoundException | SQLException e1) {
                try {
                    throw e1;
                } catch ( ClassNotFoundException | SQLException ex) {
                    Logger.getLogger(TutorPage.class.getName()).log(Level.SEVERE, null, ex);
                }
            } 
            finally {
               close2();
            }

            usrpagestage.setOnCloseRequest(new EventHandler<WindowEvent>() {
                @Override
                public void handle(WindowEvent t) {
                    setEffect(new BoxBlur(0, 0, 0));
                }
            });
        }
    });

The following code is of the new window to be opened -

package subwarning;

import javafx.geometry.Insets;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;

public class SubWarning extends BorderPane{

public SubWarning() {
    setCenter(addVBox());        
}

private VBox addVBox() {

    VBox vb1 = new VBox();
    vb1.setPadding(new Insets(15, 20, 25, 20));
    vb1.setSpacing(15);
    vb1.setStyle("-fx-background-color: #333333;");

    Text t1 = new Text("Add Subjects first.\n Then add Staff.");
    t1.setFont(Font.font("Arial", FontWeight.BOLD, 20));
    t1.setFill(Color.RED);

    return vb1;
}

}

When clicked on the button, a small is opening. It is too small that I can't even see the contents in it. How can I open the window in the specified size ?

Upvotes: 0

Views: 979

Answers (2)

Georgian
Georgian

Reputation: 8960

Of couse your window is small. You're only specifying it maximum dimensions.

Possible solutions:

  1. Use the setWidth and setHeight APIs from Window (Stage in your case)

  2. Call setMaxHeight and setMaxWidth, but also call setMinHeight and setMinWidth.


EDIT 1:

  • Use solution #1 after setting the Scene
  • Use solution #2 and setResizable(true)

Upvotes: 2

TomJ
TomJ

Reputation: 1813

I found the answer. I just forget to add the Text to VBox. When I added it, the window appeared in specified size.

Upvotes: 0

Related Questions