0

I want to use the variable data's value from the following method in another class and this method handles the run button in one window and the other class handles the buttons which are located in another window. I have tried a few ways to do that but none of them worked, so I would be grateful if anyone could help me with it.

public class Test{

@FXML
private TextField sqlTextField;
@FXML
private TableView table;
@FXML
private Button save;
@FXML
private Button run;
@FXML
private Button delete;

public ObservableList<ObservableList> data = FXCollections.observableArrayList();

 public void handleRunButton() throws SQLException {

        run.setDisable(true);
    try {
        ConnectionClass connectionClass = new ConnectionClass();
        Connection connection = connectionClass.getConnection();
        String sql = sqlTextField.getText() + ";";
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);

        for (int i = 0; i < resultSet.getMetaData().getColumnCount(); i++) {
            final int j = i;
            TableColumn col = new TableColumn(resultSet.getMetaData().getColumnName(i + 1));
            col.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList, String>, ObservableValue<String>>() {
                public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList, String> param) {
                    return new SimpleStringProperty(param.getValue().get(j).toString());
                }
            });

            table.getColumns().addAll(col);
            System.out.println("Column [" + i + "] ");
        }

        /********************************
         * Data added to ObservableList *
         ********************************/
        while (resultSet.next()) {
            //Iterate Row
            ObservableList<String> row = FXCollections.observableArrayList();
            for (int i = 1; i <= resultSet.getMetaData().getColumnCount(); i++) {
                row.add(resultSet.getString(i));
            }
            data.add(row);
            System.out.println("Row [1] added " + row);
        }
        System.out.println(data);
        //FINALLY ADDED TO TableView
        table.setItems(data);

        System.out.println("data =" + data);
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Error on Building Data");
    }
    ObservableList tableText = table.getItems();
    System.out.println("tableText=" + tableText);
    boolean disableButton2;
    if (tableText.size() > 0) {
        disableButton2 = false;
    } else {
        disableButton2 = true;
    }
    save.setDisable(disableButton2);
    System.out.println(disableButton2);
    delete.setDisable(disableButton2);
    System.out.println("data==>" + data);
}
Azad
  • 9
  • 4
  • 6
    Method-local variables are - as the name suggests - only accessible within the method. In order to use them outside that method, they have to either be passed along as a parameter to another method or be returned by the method. --- This sounds like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What are you actually trying to achieve from a business-logic point of view? – Turing85 Oct 24 '21 at 12:29
  • Ok, so I have a table that gets the Data from a database when the run button is pressed, now we have a save button too which opens a new window and asks the user if he wants to save the data( which I have created another class for the save window ), now I want to have access to the ObservableList data in handleRunButton method in order to be able to use it in the save window to save the data in the system. – Azad Oct 24 '21 at 12:43
  • 1
    @MReza you either need to change the scope of the variable and expose it, or pass it into the methods/classes that need access to it. – OH GOD SPIDERS Oct 24 '21 at 12:44
  • 1
    You should study [mvc in JavaFX](https://stackoverflow.com/questions/32342864/applying-mvc-with-javafx) – jewelsea Oct 24 '21 at 13:22
  • See also https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml – James_D Oct 24 '21 at 14:37

0 Answers0