Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
101 views
in Technique[技术] by (71.8m points)

java - How to call contents of list with streams?

I am using java fx as a gui. When the user selects a file and hits process it reads in and process a jar file, and then prints out and saves a few metrics in string format using EmbeddedStorageManager. I am trying to call the contents of my list in a separate class when I press a button, but it keeps coming back saying the list is empty.

When I run the method directly after populating it(without pressing the button), it seems to print out the contents but when I call it after pressing the show all button it prints the list is empty.

I've been googling for hours but can't seem to find anything that helps.

Please see my code below, and thanks for any help in advance.

Database class This class is where the list is being called from and it contains the method to show the contents of the list.

AppWindow This is the button I am using to call the show method in the database class. When this button is press it says the database is empty

package ie.gmit.sw;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.TitledPane;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class AppWindow extends Application {
    private TextField txtFile; // A control, part of the View and a leaf node.
    //ProcessJar process = new ProcessJar();
    
    Database db = new Database();
    //QueryDB qdb = new QueryDB();
    ProcessJar pj = new ProcessJar();

    @Override
    public void start(Stage stage) throws Exception {
        stage.setTitle("");
        stage.setWidth(800);
        stage.setHeight(400);

        
        stage.setOnCloseRequest((e) -> System.exit(0));

        
        VBox box = new VBox();
        box.setPadding(new Insets(10));
        box.setSpacing(8);

        // **Strategy Pattern**. Configure the Context with a Concrete Strategy
        Scene scene = new Scene(box);
        stage.setScene(scene);

        ToolBar toolBar = new ToolBar(); // A ToolBar is a composite node for Buttons (leaf nodes)

        Button btnQuit = new Button("Quit"); // A Leaf node
        btnQuit.setOnAction(e -> System.exit(0)); // Plant an observer on the button
        toolBar.getItems().add(btnQuit); // Add to the parent node and build the tree

        Button btnAdd = new Button("Show all"); // A Leaf node
        btnAdd.setOnAction(e -> {
            db.show(); <<<<<<-----When this button is pressed its supposed to show the contents of the database, but its coming back empty
        });
        toolBar.getItems().add(btnAdd); // Add to the parent node and build the tree

        Button btnDelete = new Button("Delete"); // A Leaf node
        btnDelete.setOnAction(e -> {
            db.emptyDb();

        });
        toolBar.getItems().add(btnDelete); // Add to the parent node and build the tree

        /*
         * Add all the sub trees of nodes to the parent node and build the tree
         */
        box.getChildren().add(getFileChooserPane(stage)); // Add the sub tree to the main tree
        box.getChildren().add(toolBar); // Add the sub tree to the main tree

        // Display the window
        stage.show();
        stage.centerOnScreen();
    }

    /*
     * This method builds a TitledPane containing the controls for the file chooser
     * part of the application. We could have created a specialised instance of the
     * class TitledPane using inheritance and moved all of the method into its own
     * class (OCP).
     */
    private TitledPane getFileChooserPane(Stage stage) {
        VBox panel = new VBox(); // ** A concrete strategy ***

        txtFile = new TextField(); // A leaf node

        FileChooser fc = new FileChooser(); // A leaf node
        fc.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("JAR Files", "*.jar"));

        Button btnOpen = new Button("Select File"); // A leaf node
        btnOpen.setOnAction(e -> { // Plant an observer on the button
            File f = fc.showOpenDialog(stage);
            // convert f to string
            txtFile.setText(f.getAbsolutePath());
        });

        Button btnProcess = new Button("Process"); // A leaf node
        btnProcess.setOnAction(e -> { // Plant an observer on the button
            File f = new File(txtFile.getText());
            System.out.println("[INFO] Processing file " + f.getName());

            try {
                pj.process(f.toString());
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (ClassNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

        });

        ToolBar tb = new ToolBar(); // A composite node
        tb.getItems().add(btnOpen); // Add to the parent node and build a sub tree
        tb.getItems().add(btnProcess); // Add to the parent node and build a sub tree

        panel.getChildren().add(txtFile); // Add to the parent node and build a sub tree
        panel.getChildren().add(tb); // Add to the parent node and build a sub tree

        TitledPane tp = new TitledPane("Select File to Process", panel); // Add to the parent node and build a sub tree
        tp.setCollapsible(false);
        return tp;
    }


}


Runner

import java.io.IOException;
import javafx.application.Application;

public class Runner {

    public static void main(String[] args) throws IOException {
        System.out.println("[INFO] Launching GUI...");
        Application.launch(AppWindow.class, args);

        
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The issue seems to be with missing call to Database :: addElement in AppWindow class.

The method to add something to the database is called only inside ProcessJar class twice, and it is using its own instance of Database.

You should add another button in AppWindow and set its action to db.addElement("something");


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...