源码 DialogAPP.zip
环境netbean org.postgresql.Driver version 9
1 /* 2 * To change this license header, choose License Headers in Project Properties. 3 * To change this template file, choose Tools | Templates 4 * and open the template in the editor. 5 */ 6 package dialogapp; 7 8 9 import java.io.IOException; 10 import java.util.Optional; 11 import javafx.application.Application; 12 import javafx.application.Platform; 13 import javafx.event.ActionEvent; 14 import javafx.event.EventHandler; 15 import javafx.fxml.FXMLLoader; 16 import javafx.geometry.Insets; 17 import javafx.scene.Node; 18 import javafx.scene.Scene; 19 import javafx.scene.control.Button; 20 import javafx.scene.control.ButtonBar.ButtonData; 21 import javafx.scene.control.ButtonType; 22 import javafx.scene.control.Dialog; 23 import javafx.scene.control.Label; 24 import javafx.scene.control.PasswordField; 25 import javafx.scene.control.TableColumn; 26 import javafx.scene.control.TextField; 27 import javafx.scene.image.ImageView; 28 import javafx.scene.layout.AnchorPane; 29 import javafx.scene.layout.GridPane; 30 import javafx.scene.layout.StackPane; 31 import javafx.stage.Stage; 32 import javafx.util.Pair; 33 34 /** 35 * 36 * @author 37 */ 38 public class DialogAPP extends Application { 39 40 StackPane root; 41 Scene scene; 42 43 @Override 44 public void start(Stage primaryStage) throws IOException { 45 // Button btn = new Button(); 46 // btn.setText("Say xxxxx'"); 47 // btn.setOnAction(new EventHandler<ActionEvent>() { 48 // 49 // @Override 50 // public void handle(ActionEvent event) { 51 // System.out.println("Hello World!"); 52 // } 53 // }); 54 55 // root = new StackPane(); 56 // root.getChildren().add(btn); 57 58 // Alert alert = new Alert(AlertType.CONFIRMATION); 59 // alert.setTitle("Information Dialog"); 60 // alert.setHeaderText("Look, an Information Dialog"); 61 // alert.setContentText("I have a great message for you!"); 62 // 63 // // alert.showAndWait(); 64 // Optional<ButtonType> result = alert.showAndWait(); 65 // 66 // if (result.get() == ButtonType.OK) { 67 // System.out.print("ok"); 68 // } else { 69 // System.out.print("cancel"); 70 // } 71 // 72 // TextInputDialog dialog = new TextInputDialog("walter"); 73 // // Traditional way to get the response value. 74 // Optional<String> result1 = dialog.showAndWait(); 75 // if (result1.isPresent()) { 76 // System.out.println("Your name: " + result1.get()); 77 // } 78 // result1.ifPresent(name -> System.out.println("Your name: " + name)); 79 80 81 82 83 // List<String> choices = new ArrayList<>(); 84 //choices.add("a"); 85 //choices.add("b"); 86 //choices.add("c"); 87 // 88 //ChoiceDialog<String> dialog2 = new ChoiceDialog<>("b", choices); 89 // Optional<String> result2 = dialog2.showAndWait(); 90 // if (result2.isPresent()) { 91 // System.out.println("Your choose: " + result2.get()); 92 // } 93 // result2.ifPresent(name -> System.out.println("Your name: " + name)); 94 95 96 97 98 99 //login 100 // Create the custom dialog. 101 //Dialog<Pair<String, String>> dialog = new Dialog<>(); 102 //dialog.setTitle("Login Dialog"); 103 //dialog.setHeaderText("Look, a Custom Login Dialog"); 104 // 105 //// Set the icon (must be included in the project). 106 //dialog.setGraphic(new ImageView(this.getClass().getResource("login.png").toString())); 107 // 108 //// Set the button types. 109 //ButtonType loginButtonType = new ButtonType("Login", ButtonData.OK_DONE); 110 //dialog.getDialogPane().getButtonTypes().addAll(loginButtonType, ButtonType.CANCEL); 111 // 112 //// Create the username and password labels and fields. 113 //GridPane grid = new GridPane(); 114 //grid.setHgap(10); 115 //grid.setVgap(10); 116 //grid.setPadding(new Insets(20, 150, 10, 10)); 117 // 118 //TextField username = new TextField(); 119 //username.setPromptText("Username"); 120 //PasswordField password = new PasswordField(); 121 //password.setPromptText("Password"); 122 // 123 //grid.add(new Label("Username:"), 0, 0); 124 //grid.add(username, 1, 0); 125 //grid.add(new Label("Password:"), 0, 1); 126 //grid.add(password, 1, 1); 127 // 128 //// Enable/Disable login button depending on whether a username was entered. 129 //Node loginButton = dialog.getDialogPane().lookupButton(loginButtonType); 130 //loginButton.setDisable(true); 131 // 132 //// Do some validation (using the Java 8 lambda syntax). 133 //username.textProperty().addListener((observable, oldValue, newValue) -> { 134 // loginButton.setDisable(newValue.trim().isEmpty()); 135 //}); 136 // 137 //dialog.getDialogPane().setContent(grid); 138 // 139 //// Request focus on the username field by default. 140 //Platform.runLater(() -> username.requestFocus()); 141 // 142 //// Convert the result to a username-password-pair when the login button is clicked. 143 //dialog.setResultConverter(dialogButton -> { 144 // if (dialogButton == loginButtonType) { 145 // return new Pair<>(username.getText(), password.getText()); 146 // } 147 // return null; 148 //}); 149 // 150 // 151 // 152 //Optional<Pair<String, String>> result = dialog.showAndWait(); 153 // 154 //result.ifPresent(usernamePassword -> { 155 // System.out.println("Username=" + usernamePassword.getKey() + ", Password=" + usernamePassword.getValue()); 156 //}); 157 158 159 160 161 loadDialogShowView(primaryStage); 162 163 } 164 165 166 public void loadDialogShowView(Stage primaryStage) throws IOException{ 167 FXMLLoader loader=new FXMLLoader(); 168 loader.setLocation(DialogAPP.class.getResource("/dialogapp/DialogShowFXML.fxml")); 169 AnchorPane ap=(AnchorPane)loader.load(); 170 171 DialogShowFXMLController ds=loader.getController(); 172 ds.ShowOBJ(); 173 scene = new Scene(ap); 174 175 // 给 row 添加样式 176 scene.getStylesheets().addAll(DialogAPP.class.getResource("/dialogapp/rowstyle.css").toExternalForm()); 177 primaryStage.setScene(scene); 178 primaryStage.show(); 179 180 } 181 /** 182 * @param args the command line arguments 183 */ 184 public static void main(String[] args) { 185 launch(args); 186 } 187 188 }
1 /* 2 * To change this license header, choose License Headers in Project Properties. 3 * To change this template file, choose Tools | Templates 4 * and open the template in the editor. 5 */ 6 package dialogapp; 7 8 import java.net.URL; 9 import java.sql.SQLException; 10 import java.util.List; 11 import java.util.ResourceBundle; 12 import java.util.logging.Level; 13 import java.util.logging.Logger; 14 import javafx.beans.property.SimpleStringProperty; 15 16 import javafx.beans.property.StringProperty; 17 import javafx.collections.FXCollections; 18 import javafx.collections.ObservableList; 19 20 import javafx.fxml.FXML; 21 import javafx.fxml.Initializable; 22 import javafx.scene.control.TableCell; 23 import javafx.scene.control.TableColumn; 24 import javafx.scene.control.TableColumn.CellEditEvent; 25 import javafx.scene.control.TableView; 26 import javafx.scene.paint.Color; 27 import javafx.util.Callback; 28 29 /** 30 * FXML Controller class 31 * 32 * @author 33 */ 34 public class DialogShowFXMLController implements Initializable { 35 36 @FXML 37 private TableView<ShowObject> ObjectTable; 38 //TableColumn<Person, String> 39 @FXML 40 public TableColumn<ShowObject, String> firstOBJ; 41 42 @FXML 43 public TableColumn<ShowObject, String> secondOBJ; 44 45 /** 46 * Initializes the controller class. 47 */ 48 @FXML 49 public void initialize(URL url, ResourceBundle rb) { 50 51 52 ObjectTable.setEditable(true); 53 firstOBJ.setCellValueFactory(cellData -> cellData.getValue().firstOBJProperty()); 54 secondOBJ.setCellValueFactory(cellData -> cellData.getValue().secondOBJProperty()); 55 56 // fix or change item s 57 secondOBJ.setCellFactory(new Callback<TableColumn<ShowObject, String>, TableCell<ShowObject, String>>() { 58 59 @Override 60 public TableCell<ShowObject, String> call(TableColumn<ShowObject, String> param) { 61 62 return new TableCell<ShowObject, String>() { 63 64 @Override 65 protected void updateItem(String item, boolean empty) { 66 67 // super.updateItem(item, empty); 68 if (!empty) { 69 70 // if(item.contains("shoe")) { 71 if (item.equals("shoe")) { 72 this.setTextFill(Color.BLUEVIOLET); 73 } else { 74 setTextFill(Color.BLACK); 75 } 76 setText(item); 77 //System.out.println("----------"+getText()+"-------"+item+"-----------"+isEmpty()); 78 79 } else { 80 setText(null); 81 } 82 } 83 84 }; 85 } 86 }); 87 88 89 secondOBJ.setOnEditCommit( 90 (CellEditEvent<ShowObject, String> t) -> { 91 ((ShowObject) t.getTableView().getItems().get(t.getTablePosition().getRow())).setSecondOBJ(t.getNewValue()); 92 93 }); 94 95 96 // columnValue.setCellFactory(cellFactory); 97 // columnValue.setOnEditCommit( 98 // new EventHandler<TableColumn.CellEditEvent<Record, Double>>() { 99 // @Override public void handle(TableColumn.CellEditEvent<Record, Double> t) { 100 // ((Record)t.getTableView().getItems().get( 101 // t.getTablePosition().getRow())).setFieldValue(t.getNewValue()); 102 // } 103 // }); 104 105 } 106 private ObservableList<ShowObject> objectData = FXCollections.observableArrayList(); 107 108 private void setobjectData() { 109 110 // objectData.add(new ShowObject("hands", "glove")); 111 // objectData.add(new ShowObject("foot", "shoe")); 112 // objectData.add(new ShowObject("head", "hat")); 113 // objectData.add(new ShowObject("leg", "trouse")); 114 // objectData.add(new ShowObject("body", "clothes")); 115 // objectData.add(new ShowObject("", "")); 116 // objectData.add(new ShowObject("", "")); 117 // objectData.add(new ShowObject("", "")); 118 119 DBhelp dbh=new DBhelp(); 120 String sql="select * from tbl_test_object"; 121 try { 122 List<ShowObject> l= dbh.selectRun(sql); 123 objectData=FXCollections.observableArrayList(l); 124 125 // for(ShowObject so:l){ 126 // System.out.println(so.getFirstOBJ()+" ///// "+so.getSecondOBJ()); 127 // } 128 } catch (ClassNotFoundException ex) { 129 Logger.getLogger(DBhelp.class.getName()).log(Level.SEVERE, null, ex); 130 } catch (SQLException ex) { 131 Logger.getLogger(DBhelp.class.getName()).log(Level.SEVERE, null, ex); 132 } 133 } 134 135 public void ShowOBJ() { 136 setobjectData(); 137 ObjectTable.setItems(objectData); 138 } 139 140 public class ShowObject { 141 142 private final StringProperty firstOBJ; 143 private final StringProperty secondOBJ; 144 145 public ShowObject(String S1, String S2) { 146 147 this.firstOBJ = new SimpleStringProperty(S1); 148 this.secondOBJ = new SimpleStringProperty(S2); 149 } 150 151 public StringProperty firstOBJProperty() { 152 return firstOBJ; 153 } 154 155 public StringProperty secondOBJProperty() { 156 return secondOBJ; 157 } 158 159 public String getFirstOBJ() { 160 return firstOBJ.get(); 161 } 162 163 // public void setFirstOBJ(String firstOBJ) { 164 // this.firstOBJ.set(firstOBJ); 165 // } 166 167 public String getSecondOBJ() { 168 return secondOBJ.get(); 169 } 170 171 public void setSecondOBJ(String secondOBJ) { 172 this.secondOBJ.set(secondOBJ); 173 } 174 175 } 176 177 }
1 /* 2 * To change this license header, choose License Headers in Project Properties. 3 * To change this template file, choose Tools | Templates 4 * and open the template in the editor. 5 */ 6 package dialogapp; 7 8 import dialogapp.DialogShowFXMLController.ShowObject; 9 import java.sql.Connection; 10 import java.sql.DriverManager; 11 import java.sql.ResultSet; 12 import java.sql.SQLException; 13 import java.sql.Statement; 14 import java.util.ArrayList; 15 import java.util.List; 16 import java.util.logging.Level; 17 import java.util.logging.Logger; 18 19 20 21 22 /** 23 * 24 * @author 25 */ 26 27 28 public class DBhelp { 29 30 // //table script 31 // CREATE TABLE "public"."tbl_test_object" ( 32 //"firstOBJ" varchar(10), 33 //"secondOBJ" varchar(10) 34 //); 35 36 37 38 39 public List<ShowObject> selectRun(String sql) throws ClassNotFoundException, SQLException{ 40 List<ShowObject> l=new ArrayList(); 41 String connStr="jdbc:postgresql://spec2.dunham-bush.com:5433/spec7"; 42 DialogShowFXMLController dbase=new DialogShowFXMLController(); 43 ShowObject so=dbase.new ShowObject("",""); 44 45 Class.forName("org.postgresql.Driver"); 46 Connection con= DriverManager.getConnection(connStr,"ecatalog","yeA"); 47 48 Statement st=con.createStatement(); 49 st.execute(sql); 50 ResultSet rs=st.getResultSet(); 51 52 53 while(rs.next()){ 54 String firstOBJ=rs.getString("firstOBJ"); 55 String secondOBJ= rs.getString("secondOBJ"); 56 l.add(dbase.new ShowObject(firstOBJ,secondOBJ)); 57 58 } 59 60 return l; 61 } 62 public static void main(String args[]) { 63 64 DBhelp dbh=new DBhelp(); 65 String sql="select * from tbl_test_object"; 66 try { 67 List<ShowObject> l= dbh.selectRun(sql); 68 for(ShowObject so:l){ 69 System.out.println(so.getFirstOBJ()+" ///// "+so.getSecondOBJ()); 70 } 71 72 73 } catch (ClassNotFoundException ex) { 74 Logger.getLogger(DBhelp.class.getName()).log(Level.SEVERE, null, ex); 75 } catch (SQLException ex) { 76 Logger.getLogger(DBhelp.class.getName()).log(Level.SEVERE, null, ex); 77 } 78 79 80 } 81 }
DialogShowFXML.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <?import java.lang.*?> 4 <?import java.util.*?> 5 <?import javafx.scene.*?> 6 <?import javafx.scene.control.*?> 7 <?import javafx.scene.layout.*?> 8 9 <AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="dialogapp.DialogShowFXMLController"> 10 <children> 11 <TableView fx:id="ObjectTable" layoutX="194.0" layoutY="100.0" prefHeight="200.0" prefWidth="200.0"> 12 <columns> 13 <TableColumn fx:id="firstOBJ" prefWidth="98.0" text="S1" /> 14 <TableColumn fx:id="secondOBJ" minWidth="8.0" prefWidth="101.0" text="S2" /> 15 </columns> 16 </TableView> 17 </children> 18 </AnchorPane>
rowstyle.css
1 /* 2 To change this license header, choose License Headers in Project Properties. 3 To change this template file, choose Tools | Templates 4 and open the template in the editor. 5 */ 6 /* 7 Created on : Aug 5, 2015, 10:55:39 AM 8 Author : 9 */ 10 11 .table-row-cell:empty { 12 -fx-background-color: white; 13 } 14 15 .table-row-cell:empty .table-cell { 16 -fx-border- 0px; 17 }