2
package lab7;

import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class cool extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {

        BorderPane bp = new BorderPane();
        bp.getStyleClass().add("boop");

        GridPane gp = new GridPane();
        GridPane gp2 = new GridPane();

        Label label1 = new Label("Reverse Multiplication Table");
        label1.getStyleClass().add("label1");

        HBox counterBox = new HBox();
        counterBox.getStyleClass().add("label1");
        counterBox.getChildren().add(label1);

        Label text = new Label("Enter Answer: ");
        text.getStyleClass().add("text");
        TextField textx = new TextField();
        gp.getStyleClass().add("textx");

        Button b = new Button();
        b.setText("Find Problems");

        Scene sc = new Scene(bp);
        sc.getStylesheets().add("Styles/Styles.css");

        for (int row = 0; row < 11; row++)
            for (int col = 0; col < 11; col++) {
                // This will create the first column
                if (row == 0) {
                    Label columns = new Label(" " + col);
                    gp2.add(columns, 3, col);
                    columns.getStyleClass().add("columnStyle");
                }

                else if (col == 0) {
                    // This will create the first row
                    Label rows = new Label("" + row);
                    gp2.add(rows, row, 0);
                    rows.getStyleClass().add("rowStyle");
                } else {
                    final int a = row;
                    final int z = col;
                    final Label table = new Label(row + " x " + col);
                    table.getStyleClass().add("table");
                    table.setMinWidth(150);

                    gp2.add(table, row, col);
                    b.getStyleClass().add("buttons");
                    b.addEventHandler(MouseEvent.MOUSE_CLICKED,
                            new EventHandler<Event>() {
                                boolean Answer = true;

                                @Override
                                public void handle(Event event) {
                                    if (Answer = true) {
                                        int read = (Integer.parseInt(textx
                                                .getText()));
                                        if (read == (a * z)) {
                                            table.setText(a + " x " + z);
                                            table.getStyleClass().add("ans");

                                        }
                                    }
                                }
                            });
                }
            }

        gp.add(text, 1, 0);
        gp.add(textx, 2, 0);
        gp.add(b, 3, 0);

        bp.setTop(counterBox);
        bp.setCenter(gp);
        bp.setBottom(gp2);

        primaryStage.setScene(sc);
        primaryStage.show();
        primaryStage.setTitle("Lab7");
    }

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

}

Styles.css

.columnStyle{
    -fx-padding: 5px;
    -fx-border-width: 1px;
    -fx-text-fill:gold;
}
.rowStyle{
    -fx-padding: 64px;
    -fx-border-width: 1px;
    -fx-text-fill:gold;
}
.table{
    -fx-padding: 10px;
    -fx-border-width: 1px;
    -fx-border-style: solid;
    -fx-background-color: yellow;
    -fx-text-fill:red;
    }

.boop{
    -fx-padding: 20px;
    -fx-font-size: 100%;
    -fx-background-color:black;

}
.label1{
    -fx-background-color: white;
    -fx-alignment: center;
    -fx-text-fill:red;

}
.textx{
    -fx-alignment: center;
}
.text{
    -fx-text-fill:gold;
}
.ans{
    -fx-background-color:white;
}
.ans2{
    -fx-background-color:gold;
}
.button{
    -fx-text-fill:red;
}

I am getting this error May 25, 2015 8:41:26 PM com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged WARNING: Resource "Styles/Styles.css" not found.

I had trouble at the beginning when running the program. Now I am getting a plain table with nothing from my Styles.css and also when I put the solution it doesn't select the correct selections of multiplications.

Shahzeb
  • 4,745
  • 4
  • 27
  • 40
  • Kindly share the location of `Styles.css` with respect to your project –  May 26 '15 at 04:02
  • shoudn't it be standard, do I have to put the exact path? – Wilson Lino May 26 '15 at 04:06
  • Relative path is recommended, if css is within package then qualified package path is required –  May 26 '15 at 04:08
  • I put the path but I am having the same issue @ Arvind – Wilson Lino May 26 '15 at 04:14
  • What path was given for the last attempt? –  May 26 '15 at 04:19
  • @ Arvind I was able to figure it out but the issues I have is that it whows many errors For Example Access restriction: The constructor 'BorderPane()' is not API (restriction on required library 'C:\Program Files\Java\jre1.8.0_25\lib\ext\jfxrt.jar') – Wilson Lino May 26 '15 at 04:25
  • kindly have a look at this [post](http://stackoverflow.com/questions/9266632/access-restriction-is-not-accessible-due-to-restriction-on-required-library) –  May 26 '15 at 04:28
  • Thanks, I have another question. Every time put in the answer and look for more the previous answers don't erase, how can I fix that @ Arvind – Wilson Lino May 26 '15 at 04:44

1 Answers1

1
sc.getStylesheets().add("Styles/Styles.css");

This is how you should add your stylesheet:

sc.getStylesheets().add(getClass().getResource("Styles/Styles.css").toExternalForm());

In case it doesn't work, please share the exact path of Styles.css. It should be src/Styles/Styles.css.

Note: per convention package names in java should start with lower case character.

Roland
  • 18,114
  • 12
  • 62
  • 93