I'm trying to make an application that creates a table from text in a file and then the user can add or delete words. The text file is used else where in my app to populate dropdown boxes so the user can only choose the text in the dropdown boxes.
My problem is I can't delete from the table only add to it and anything I can find on the net are tables that are made from classes. My table is just a very basic one that has 1 column of strings. For some reason when I print the selected items of the table I always get nothing? I can't figure out why.
I only started javaFx a few weeks ago I really just learning from videos and books but they are very simple compared to the application I'm expected to make.
Here is my code:
public class MaintenWindow {
private static Stage window;
private static TableView<String> table = new TableView<String>();
private static TextField input;
private static ObservableList<String> stringList;
private static BorderPane bP;
private static Scene scene;
private static File types = new File("type.txt");
private static File location = new File("location.txt");
private static File status = new File("status.txt");
public static void display() throws FileNotFoundException {
window = new Stage();
BorderPane bP = new BorderPane();
window.setTitle("Mainten Window");
MenuBar menuBar = new MenuBar();
Menu fileMenu = new Menu("_Add/Remove From DropDowns");
MenuItem editLocation = new MenuItem("_Edit Locations");
MenuItem editAtypes = new MenuItem("_Edit Animal Types");
MenuItem editStatus = new MenuItem("_Edit Status's");
MenuItem exit = new MenuItem("Exit");
fileMenu.getItems().addAll(editLocation, editAtypes, editStatus, new SeparatorMenuItem(),exit);
menuBar.getMenus().addAll(fileMenu);
editLocation.setOnAction(e -> {
bP.setTop(menuBar);
bP.setCenter(vBox("Locations", location));
window.setScene(scene);
window.show();
});
editAtypes.setOnAction(e -> {
bP.setTop(menuBar);
bP.setCenter(vBox("Animal Types", types));
window.setScene(scene);
window.show();
});
editStatus.setOnAction(e -> {
bP.setTop(menuBar);
bP.setCenter(vBox("Status", status));
window.setScene(scene);
window.show();
});
bP.setTop(menuBar);
scene = new Scene(bP, 800,800);
Label label = new Label("welcome to the maintenance window");
bP.setCenter(label);
window.setScene(scene);
window.show();
}
/*public static MenuBar menuBar()
{
//file tab menu
Menu fileMenu = new Menu("_Add/Remove From DropDowns");
MenuItem editLocation = new MenuItem("_Edit Locations");
MenuItem editAtypes = new MenuItem("_Edit Animal Types");
MenuItem editStatus = new MenuItem("_Edit Status's");
MenuItem exit = new MenuItem("Exit");
fileMenu.getItems().addAll(editLocation, editAtypes, editStatus, new SeparatorMenuItem(),exit);
editLocation.setOnAction(e -> {
bP.setTop(menuBar());
bP.setCenter(vBox("Locations", location));
window.setScene(scene);
window.show();
});
editAtypes.setOnAction(e -> {
bP.setTop(menuBar());
bP.setCenter(vBox("Animal Types", types));
window.setScene(scene);
window.show();
});
editStatus.setOnAction(e -> {
bP.setTop(menuBar());
bP.setCenter(vBox("Status", status));
window.setScene(scene);
window.show();
});
MenuBar menuBar = new MenuBar();
menuBar.getMenus().addAll(fileMenu);
return menuBar;
}*/
public static ObservableList<String> getString(File file) throws FileNotFoundException{
stringList = FXCollections.observableArrayList();
Scanner kb = new Scanner(file);
String item;
while (kb.hasNext()) {
item = kb.nextLine();
stringList.add(item);
}
kb.close();
return stringList;
}
@SuppressWarnings("unchecked")
public static TableView<String> table(String string,File file)
{
TableColumn<String, String> tColumn = new TableColumn<String, String>(string);
tColumn.setCellValueFactory(cellData ->
new ReadOnlyStringWrapper(cellData.getValue()));
TableView<String> table = new TableView<String>();
try {
table.setItems(getString(file));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
table.getColumns().addAll(tColumn);
table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
return table;
}
public static void addButtonClicked(){
stringList.add(input.getText());
input.clear();
}
//Delete button clicked
public static void deleteButtonClicked(){
ObservableList<String> allStrings, stringSelected;
allStrings = table.getItems();
//printElements(allStrings);
System.out.println(table.getSelectionModel().getSelectedItems());
stringSelected = table.getSelectionModel().getSelectedItems();
System.out.println(stringSelected);
stringSelected.forEach(allStrings::remove);
}
public static HBox textFields() {
input = new TextField();
input.setPromptText("Input");
input.setMinWidth(75);
HBox hBox = new HBox();
hBox.setPadding(new Insets(10,10,10,10));
hBox.setSpacing(10);
hBox.getChildren().addAll(input);
return hBox;
}
public static HBox addRemove(File file){
//Button
Button addButton = new Button("Add");
addButton.setOnAction(e -> addButtonClicked());
Button deleteButton = new Button("Delete");
deleteButton.setOnAction(e -> deleteButtonClicked());
Button writeButton = new Button("Write to file");
try {
try {
writeButton.setOnAction(e -> write(file));
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Button closeButton = new Button("Close Window");
closeButton.setOnAction(e -> window.close());
HBox hBox2 = new HBox();
hBox2.setPadding(new Insets(10,10,10,10));
hBox2.setSpacing(10);
hBox2.getChildren().addAll(addButton, deleteButton, writeButton, closeButton);
return hBox2;
}
private static void printElements(ObservableList<String> list) {
System.out.println("Size: " + list.size());
for (Object o : list) {
System.out.println(o.toString());
}
System.out.println("");
}
public static void write(File file)
{
PrintWriter outFile = null;
try {
outFile = new PrintWriter(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int i = 0; i < stringList.size(); i++)
{
outFile.println(stringList.get(i));
}
outFile.close();
}
public static VBox vBox(String string, File file){
Label label = new Label(string);
label.setFont(new Font("Arial", 20));
VBox vBox = new VBox();
vBox.setSpacing(5);
vBox.setPadding(new Insets(10, 0, 0, 10));
vBox.getChildren().addAll(label, table(string,file), textFields(), addRemove(file));
return vBox;
}
}
I would be greatful for any help. if u have any tips or advice for me please post If you have links to more advanced javafx programs than I'm finding please post them.