您的位置:首页 > 编程语言 > Java开发

javafx

2015-08-06 11:25 501 查看
源码 DialogAPP.zip

环境netbean org.postgresql.Driver version 9

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package dialogapp;

import java.io.IOException;
import java.util.Optional;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar.ButtonData;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Pair;

/**
*
* @author
*/
public class DialogAPP extends Application {

StackPane root;
Scene scene;

@Override
public void start(Stage primaryStage) throws IOException {
//        Button btn = new Button();
//        btn.setText("Say xxxxx'");
//        btn.setOnAction(new EventHandler<ActionEvent>() {
//
//            @Override
//            public void handle(ActionEvent event) {
//                System.out.println("Hello World!");
//            }
//        });

// root = new StackPane();
//  root.getChildren().add(btn);

//        Alert alert = new Alert(AlertType.CONFIRMATION);
//        alert.setTitle("Information Dialog");
//        alert.setHeaderText("Look, an Information Dialog");
//        alert.setContentText("I have a great message for you!");
//
//        //  alert.showAndWait();
//        Optional<ButtonType> result = alert.showAndWait();
//
//        if (result.get() == ButtonType.OK) {
//            System.out.print("ok");
//        } else {
//            System.out.print("cancel");
//        }
//
//        TextInputDialog dialog = new TextInputDialog("walter");
//        // Traditional way to get the response value.
//        Optional<String> result1 = dialog.showAndWait();
//        if (result1.isPresent()) {
//            System.out.println("Your name: " + result1.get());
//        }
//        result1.ifPresent(name -> System.out.println("Your name: " + name));

// List<String> choices = new ArrayList<>();
//choices.add("a");
//choices.add("b");
//choices.add("c");
//
//ChoiceDialog<String> dialog2 = new ChoiceDialog<>("b", choices);
// Optional<String> result2 = dialog2.showAndWait();
//        if (result2.isPresent()) {
//            System.out.println("Your choose: " + result2.get());
//        }
//        result2.ifPresent(name -> System.out.println("Your name: " + name));

//login
// Create the custom dialog.
//Dialog<Pair<String, String>> dialog = new Dialog<>();
//dialog.setTitle("Login Dialog");
//dialog.setHeaderText("Look, a Custom Login Dialog");
//
//// Set the icon (must be included in the project).
//dialog.setGraphic(new ImageView(this.getClass().getResource("login.png").toString()));
//
//// Set the button types.
//ButtonType loginButtonType = new ButtonType("Login", ButtonData.OK_DONE);
//dialog.getDialogPane().getButtonTypes().addAll(loginButtonType, ButtonType.CANCEL);
//
//// Create the username and password labels and fields.
//GridPane grid = new GridPane();
//grid.setHgap(10);
//grid.setVgap(10);
//grid.setPadding(new Insets(20, 150, 10, 10));
//
//TextField username = new TextField();
//username.setPromptText("Username");
//PasswordField password = new PasswordField();
//password.setPromptText("Password");
//
//grid.add(new Label("Username:"), 0, 0);
//grid.add(username, 1, 0);
//grid.add(new Label("Password:"), 0, 1);
//grid.add(password, 1, 1);
//
//// Enable/Disable login button depending on whether a username was entered.
//Node loginButton = dialog.getDialogPane().lookupButton(loginButtonType);
//loginButton.setDisable(true);
//
//// Do some validation (using the Java 8 lambda syntax).
//username.textProperty().addListener((observable, oldValue, newValue) -> {
//    loginButton.setDisable(newValue.trim().isEmpty());
//});
//
//dialog.getDialogPane().setContent(grid);
//
//// Request focus on the username field by default.
//Platform.runLater(() -> username.requestFocus());
//
//// Convert the result to a username-password-pair when the login button is clicked.
//dialog.setResultConverter(dialogButton -> {
//    if (dialogButton == loginButtonType) {
//        return new Pair<>(username.getText(), password.getText());
//    }
//    return null;
//});
//
//
//
//Optional<Pair<String, String>> result = dialog.showAndWait();
//
//result.ifPresent(usernamePassword -> {
//    System.out.println("Username=" + usernamePassword.getKey() + ", Password=" + usernamePassword.getValue());
//});

loadDialogShowView(primaryStage);

}

public void loadDialogShowView(Stage primaryStage) throws IOException{
FXMLLoader loader=new FXMLLoader();
loader.setLocation(DialogAPP.class.getResource("/dialogapp/DialogShowFXML.fxml"));
AnchorPane ap=(AnchorPane)loader.load();

DialogShowFXMLController ds=loader.getController();
ds.ShowOBJ();
scene = new Scene(ap);

// 给 row 添加样式
scene.getStylesheets().addAll(DialogAPP.class.getResource("/dialogapp/rowstyle.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();

}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

}


/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package dialogapp;

import java.net.URL;
import java.sql.SQLException;
import java.util.List;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.beans.property.SimpleStringProperty;

import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellEditEvent;
import javafx.scene.control.TableView;
import javafx.scene.paint.Color;
import javafx.util.Callback;

/**
* FXML Controller class
*
* @author
*/
public class DialogShowFXMLController implements Initializable {

@FXML
private TableView<ShowObject> ObjectTable;
//TableColumn<Person, String>
@FXML
public TableColumn<ShowObject, String> firstOBJ;

@FXML
public TableColumn<ShowObject, String> secondOBJ;

/**
* Initializes the controller class.
*/
@FXML
public void initialize(URL url, ResourceBundle rb) {

ObjectTable.setEditable(true);
firstOBJ.setCellValueFactory(cellData -> cellData.getValue().firstOBJProperty());
secondOBJ.setCellValueFactory(cellData -> cellData.getValue().secondOBJProperty());

// fix or change item s
secondOBJ.setCellFactory(new Callback<TableColumn<ShowObject, String>, TableCell<ShowObject, String>>() {

@Override
public TableCell<ShowObject, String> call(TableColumn<ShowObject, String> param) {

return new TableCell<ShowObject, String>() {

@Override
protected void updateItem(String item, boolean empty) {

//  super.updateItem(item, empty);
if (!empty) {

//  if(item.contains("shoe")) {
if (item.equals("shoe")) {
this.setTextFill(Color.BLUEVIOLET);
} else {
setTextFill(Color.BLACK);
}
setText(item);
//System.out.println("----------"+getText()+"-------"+item+"-----------"+isEmpty());

} else {
setText(null);
}
}

};
}
});

secondOBJ.setOnEditCommit(
(CellEditEvent<ShowObject, String> t) -> {
((ShowObject) t.getTableView().getItems().get(t.getTablePosition().getRow())).setSecondOBJ(t.getNewValue());

});

//        columnValue.setCellFactory(cellFactory);
//      columnValue.setOnEditCommit(
//              new EventHandler<TableColumn.CellEditEvent<Record, Double>>() {
//                  @Override public void handle(TableColumn.CellEditEvent<Record, Double> t) {
//                      ((Record)t.getTableView().getItems().get(
//                              t.getTablePosition().getRow())).setFieldValue(t.getNewValue());
//                  }
//              });

}
private ObservableList<ShowObject> objectData = FXCollections.observableArrayList();

private void setobjectData() {

//        objectData.add(new ShowObject("hands", "glove"));
//        objectData.add(new ShowObject("foot", "shoe"));
//        objectData.add(new ShowObject("head", "hat"));
//        objectData.add(new ShowObject("leg", "trouse"));
//        objectData.add(new ShowObject("body", "clothes"));
//        objectData.add(new ShowObject("", ""));
//        objectData.add(new ShowObject("", ""));
//        objectData.add(new ShowObject("", ""));

DBhelp dbh=new DBhelp();
String sql="select * from tbl_test_object";
try {
List<ShowObject> l= dbh.selectRun(sql);
objectData=FXCollections.observableArrayList(l);

//              for(ShowObject so:l){
//              System.out.println(so.getFirstOBJ()+"  ///// "+so.getSecondOBJ());
//              }
} catch (ClassNotFoundException ex) {
Logger.getLogger(DBhelp.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(DBhelp.class.getName()).log(Level.SEVERE, null, ex);
}
}

public void ShowOBJ() {
setobjectData();
ObjectTable.setItems(objectData);
}

public class ShowObject {

private final StringProperty firstOBJ;
private final StringProperty secondOBJ;

public ShowObject(String S1, String S2) {

this.firstOBJ = new SimpleStringProperty(S1);
this.secondOBJ = new SimpleStringProperty(S2);
}

public StringProperty firstOBJProperty() {
return firstOBJ;
}

public StringProperty secondOBJProperty() {
return secondOBJ;
}

public String getFirstOBJ() {
return firstOBJ.get();
}

//        public void setFirstOBJ(String firstOBJ) {
//            this.firstOBJ.set(firstOBJ);
//        }

public String getSecondOBJ() {
return secondOBJ.get();
}

public void setSecondOBJ(String secondOBJ) {
this.secondOBJ.set(secondOBJ);
}

}

}


/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package dialogapp;

import dialogapp.DialogShowFXMLController.ShowObject;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author
*/

public class DBhelp {

//    //table script
//    CREATE TABLE "public"."tbl_test_object" (
//"firstOBJ" varchar(10),
//"secondOBJ" varchar(10)
//);

public List<ShowObject> selectRun(String sql) throws ClassNotFoundException, SQLException{
List<ShowObject> l=new ArrayList();
String connStr="jdbc:postgresql://spec2.dunham-bush.com:5433/spec7";
DialogShowFXMLController dbase=new DialogShowFXMLController();
ShowObject so=dbase.new ShowObject("","");

Class.forName("org.postgresql.Driver");
Connection con= DriverManager.getConnection(connStr,"ecatalog","yeA");

Statement st=con.createStatement();
st.execute(sql);
ResultSet rs=st.getResultSet();

while(rs.next()){
String  firstOBJ=rs.getString("firstOBJ");
String  secondOBJ= rs.getString("secondOBJ");
l.add(dbase.new ShowObject(firstOBJ,secondOBJ));

}

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

DBhelp dbh=new DBhelp();
String sql="select * from tbl_test_object";
try {
List<ShowObject> l= dbh.selectRun(sql);
for(ShowObject so:l){
System.out.println(so.getFirstOBJ()+"  ///// "+so.getSecondOBJ());
}

} catch (ClassNotFoundException ex) {
Logger.getLogger(DBhelp.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(DBhelp.class.getName()).log(Level.SEVERE, null, ex);
}

}
}


DialogShowFXML.xml

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<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">
<children>
<TableView fx:id="ObjectTable" layoutX="194.0" layoutY="100.0" prefHeight="200.0" prefWidth="200.0">
<columns>
<TableColumn fx:id="firstOBJ" prefWidth="98.0" text="S1" />
<TableColumn fx:id="secondOBJ" minWidth="8.0" prefWidth="101.0" text="S2" />
</columns>
</TableView>
</children>
</AnchorPane>


rowstyle.css

/*
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
*/
/*
Created on : Aug 5, 2015, 10:55:39 AM
Author     :
*/

.table-row-cell:empty {
-fx-background-color: white;
}

.table-row-cell:empty .table-cell {
-fx-border-width: 0px;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: