Snippets
Created by
Terah
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | package application;
import java.util.ArrayList;
import java.util.Random;
import javafx.application.Application;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
import javafx.scene.Scene;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.util.StringConverter;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
TableView<SimpleObjectProperty<RowCont>[]> tv = new TableView<>();
tv.getItems().setAll(getJunkList(40, 8));
for(int i =0 ; i < 8; i++){
final int ii = i;
TableColumn<SimpleObjectProperty<RowCont>[], RowCont> tc = new
TableColumn<SimpleObjectProperty<RowCont>[], RowCont>();
tc.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<
SimpleObjectProperty<RowCont>[],RowCont>, ObservableValue<RowCont>>() {
@Override
public ObservableValue<RowCont> call(CellDataFeatures<SimpleObjectProperty<RowCont>[],
RowCont> param) {
return param.getValue()[ii];
}
});
tc.setCellFactory(new Callback<TableColumn<SimpleObjectProperty<RowCont>[],RowCont>, TableCell<SimpleObjectProperty<RowCont>[],RowCont>>() {
@Override
public TableCell<SimpleObjectProperty<RowCont>[], RowCont> call(
TableColumn<SimpleObjectProperty<RowCont>[], RowCont> param) {
return new EdittableCell();
}
});
tc.setText("COLUMN "+ String.valueOf(ii));
tc.setMinWidth(60);
tv.getColumns().add(tc);
tc.setEditable(true);
}
tv.setEditable(true);
tv.getSelectionModel().setCellSelectionEnabled(true);
tv.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
Scene scene = new Scene(tv,650,580);
scene.getStylesheets().add(getClass().getResource("application.css")
.toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
Random ran;
private class RowCont {
public RowCont(String a){
value = a;
}
String value = "empty";
boolean badMatch = false;
}
private ArrayList<SimpleObjectProperty<RowCont>[]> getJunkList(int size, int row){
ArrayList<SimpleObjectProperty<RowCont>[]> junk = new ArrayList();
ran = new Random();
int pos =0;
while(pos < size){
SimpleObjectProperty<RowCont>[] items = new SimpleObjectProperty[row];
for(int k =0; k < row; k++){
items[k] = new SimpleObjectProperty<RowCont>(new RowCont(String.valueOf(ran.nextInt(pos +1))));
}
junk.add(items);
pos++;
}
return junk;
}
private class EdittableCell extends TextFieldTableCell<SimpleObjectProperty<RowCont>[], RowCont> {
private String normalyStyle = ".table-cell{-fx-border-color: white; -fx-text-fill: black;-fx-border-width: 0;};",
wrongFormatStyle = "-fx-border-color: red; -fx-border-width: 1; -fx-text-fill: tomato;";
public EdittableCell(){
setEditable(true);
this.setConverter(new StringConverter<Main.RowCont>() {
@Override
public String toString(RowCont arg0) {
arg0.badMatch =
arg0.value.matches(".*[a-zA-Z]+.*");//here you add your algorithm
return arg0.value;
}
@Override
public RowCont fromString(String arg0) {
getItem().value = arg0;
return getItem();
}
});
}
@Override
public void startEdit() {
super.startEdit();
}
@Override
public void updateSelected(boolean selected) {
// TODO Auto-generated method stub
super.updateSelected(selected);
if(selected && getTableRow() != null){
getTableRow().setStyle("-fx-background-color: yellow;");
setStyle("-fx-background-color: greenyellow;");
}else{
try{
getTableRow().setStyle(".table-row{}");
if(getItem().badMatch){
setStyle(wrongFormatStyle);
}else{
setStyle(normalyStyle);
}
}catch(NullPointerException npe){}
}
}
@Override
public void updateItem(RowCont item, boolean empty) {
super.updateItem(item, empty);
if(empty){return;}
//updateSelected(isSelected());
setText(item.value);
if(item.badMatch){
setStyle(wrongFormatStyle);
}else{
setStyle(normalyStyle);
}
//commitEdit(item);
}
@Override
public void commitEdit(RowCont arg0) {
super.commitEdit(arg0);
// if(arg0 != null && !arg0.isEmpty()){ could be used though, but boy will called way too often
// setStyle(normalyStyle);
// }else{
// setStyle(wrongFormatStyle);
// }
}
@Override
protected boolean isItemChanged(RowCont arg0, RowCont arg1) {
return super.isItemChanged(arg0, arg1);
}
}
public static void main(String[] args) {
launch(args);
}
}
|
Comments (1)
You can clone a snippet to your computer for local editing. Learn more.
Full code to explanation on this question