Created
April 22, 2021 20:44
-
-
Save jananpatel2002/55e58d8fd99066bc8b0565f65b06ddea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Name: Janan Patel | |
* Date: 4/22/2020 | |
* Course Number: CSC 112 | |
* Course Name: Intermediate topics in Java Programming | |
* Problem Number: 11 | |
* Email: jkpatel2001@student.stcc.edu | |
* GUI for Database part 1 | |
* Add JavaFX Library to your project | |
* Add VM Argument to Run Configuration: --module-path %PATH_TO_FX% --add-modules javafx.controls,javafx.fxml | |
*/ | |
import java.sql.Connection; | |
import java.sql.*; | |
import java.sql.DriverManager; | |
import javafx.application.Application; | |
import javafx.geometry.Insets; | |
import javafx.geometry.Pos; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Button; | |
import javafx.scene.control.Label; | |
import javafx.scene.control.Labeled; | |
import javafx.scene.control.TextArea; | |
import javafx.scene.control.TextField; | |
import javafx.scene.layout.BorderPane; | |
import javafx.scene.layout.HBox; | |
import javafx.scene.layout.VBox; | |
import javafx.stage.Stage; | |
public class DatabaseGUI extends Application { | |
private static final int APPWIDTH = 700; | |
private static final int APPHEIGHT = 325; | |
private static final String TITLE = "The Email Program"; | |
// I include the textFields here because they need to be accessed from other | |
// classes. | |
private TextField tfipAddress2 = new TextField(); | |
private TextField tfipAddress3 = new TextField(); | |
private TextField tfipAddress1 = new TextField(); | |
private TextField tfipAddress4 = new TextField(); | |
private TextArea textArea = new TextArea(); | |
// All the HBoxes/VBoxes and extra components that will be needed through the | |
// program. | |
@Override | |
public void start(Stage primaryStage) throws Exception { | |
AppGUI scene = new AppGUI(); | |
Scene scene1 = new Scene(scene, APPWIDTH, APPHEIGHT); | |
primaryStage.setTitle(TITLE); | |
primaryStage.setScene(scene1); | |
primaryStage.show(); | |
primaryStage.setResizable(false); | |
scene.getStylesheets().addAll("Database.css"); | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
private class AppGUI extends BorderPane { | |
public AppGUI() { | |
topBox topBox = new topBox(); | |
this.setTop(topBox); | |
textAreaBox centerBox = new textAreaBox(); | |
this.setCenter(centerBox); | |
} | |
public class topBox extends VBox { | |
public topBox() { | |
HBox ipAddress = new HBox(); | |
Labeled lbipAddress = new Label("Enter IP Address: "); | |
Label period = new Label("."); | |
Label period2 = new Label("."); | |
Label period3 = new Label("."); | |
Button locate = new Button("Locate"); | |
lbipAddress.setStyle("-fx-font-weight: bold; -fx-font-size: 20pt;"); | |
period.setStyle("-fx-font-weight: bold; -fx-font-size: 20pt;"); | |
period2.setStyle("-fx-font-weight: bold; -fx-font-size: 20pt;"); | |
period3.setStyle("-fx-font-weight: bold; -fx-font-size: 20pt;"); | |
tfipAddress1.setPrefColumnCount(5); | |
tfipAddress1.setAlignment(Pos.CENTER); | |
tfipAddress1.setPrefHeight(50); | |
tfipAddress2.setPrefColumnCount(5); | |
tfipAddress2.setAlignment(Pos.CENTER); | |
tfipAddress2.setPrefHeight(50); | |
tfipAddress3.setPrefColumnCount(5); | |
tfipAddress3.setAlignment(Pos.CENTER); | |
tfipAddress3.setPrefHeight(50); | |
tfipAddress4.setPrefColumnCount(5); | |
tfipAddress4.setAlignment(Pos.CENTER); | |
tfipAddress4.setPrefHeight(50); | |
locate.setStyle("-fx-font-weight: bold; -fx-font-size: 17pt; -fx-background-color: grey"); | |
locate.setOnAction(e -> { | |
calculate(); | |
}); | |
ipAddress.setSpacing(3); | |
ipAddress.getChildren().addAll(lbipAddress, tfipAddress1, period, tfipAddress2, period2, tfipAddress3, | |
period3, tfipAddress4, locate); | |
this.setAlignment(Pos.BASELINE_LEFT); | |
this.setPadding(new Insets(20)); | |
this.setSpacing(10); | |
this.getChildren().addAll(ipAddress); | |
} | |
} | |
} | |
private class textAreaBox extends VBox { | |
public textAreaBox() { | |
textArea.setPrefColumnCount(5); | |
textArea.setWrapText(true); | |
textArea.setStyle("-fx-font-size: 12pt;"); | |
textArea.setPadding(new Insets(15)); | |
textArea.setPrefRowCount(10); | |
textArea.setEditable(false); | |
this.setAlignment(Pos.CENTER); | |
this.getChildren().addAll(textArea); | |
} | |
} | |
private void calculate() { | |
final String DATABASE = "silvestri"; | |
final String USERNAME = "readonly"; | |
final String PASSWORD = "readonly"; | |
String driver = "com.mysql.jdbc.Driver"; | |
String url = "jdbc:mysql://localhost:6000/" + DATABASE + "?user=" + USERNAME + "&password=" + PASSWORD; | |
if ((tfipAddress1.getText() != "") && (tfipAddress2.getText() != "") && (tfipAddress3.getText() != "") | |
&& (tfipAddress4.getText() != "")) { | |
if ((Integer.parseInt(tfipAddress1.getText()) > 0 && Integer.parseInt(tfipAddress1.getText()) < 255) | |
&& (Integer.parseInt(tfipAddress2.getText()) > 0 && Integer.parseInt(tfipAddress2.getText()) < 255) | |
&& (Integer.parseInt(tfipAddress3.getText()) > 0 && Integer.parseInt(tfipAddress3.getText()) < 255) | |
&& (Integer.parseInt(tfipAddress4.getText()) > 0 | |
&& Integer.parseInt(tfipAddress4.getText()) < 255)) { | |
textArea.clear(); | |
try { | |
textArea.setText(tfipAddress1.getText() + "." + tfipAddress2.getText() + "." | |
+ tfipAddress3.getText() + "." + tfipAddress4.getText() | |
+ "\nThe location for this IP Address is in Springfield, MA"); | |
Class.forName(driver); | |
System.out.println("Loaded driver"); | |
Connection conn = DriverManager.getConnection(url); | |
System.out.println("Connected to database"); | |
conn.close(); | |
System.out.println("Closed connection"); | |
} catch (Exception e) { | |
textArea.setText(tfipAddress1.getText() + "." + tfipAddress2.getText() + "." | |
+ tfipAddress3.getText() + "." + tfipAddress4.getText() + "\n" + e.getMessage()); | |
} | |
} else { | |
textArea.setText( | |
"ERROR: Looks like you either have a number too big or too small \nPlease make sure each field is between 0 and" | |
+ " 255 and please don't leave either one blank"); | |
} | |
} else { | |
textArea.setText("ERROR: Please enter a value in the blank space. Don't leave it empty "); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment