有什么方法可以将数据从“默认构造函数”中取出并以其他方法使用?

时间:2020-11-04 17:12:16

标签: java swing oop constructor

我正在尝试构建一个项目,将从h2db导出数据并将其存储为CSV文件。 我打算从用户那里获取CSV文件(要保存)的目标路径。 类名是Export,默认构造函数摆动事件在这里发生。 我希望 getSelectedFile() 的值(用户选择的路径)能够在另一种方法中使用。 我创建了类变量-尝试使用我所获得的值时字符串类型的路径为null。 在此先感谢:)

这是代码,

public class export extends JFrame{
   private BufferedWriter fileWriter;
   private JPanel contentPane;
   private static JTextField tableName;
   private JButton exportButton;
   private JButton PathButton;
   private String path;
   private String temp;
   
public export() {
         //Some labels and textfields  
  
       JFrame frame = new JFrame("CSV Export");
       PathButton = new JButton("Destination Path");
       PathButton.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               JFileChooser fileChooser = new JFileChooser();
               fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
               int option = fileChooser.showOpenDialog(frame);
               if(option == JFileChooser.APPROVE_OPTION) {
                   File file = fileChooser.getSelectedFile();
                   path = fileChooser.getSelectedFile().toString();
                   label.setText("Folder Selected: " + file.getName());
                   System.out.println(file.getName());
                   System.out.println("getCurrentDirectory(): " 
                           + fileChooser.getCurrentDirectory());
                   System.out.println("getSelectedFile() : " 
                           + fileChooser.getSelectedFile());
                   path = fileChooser.getSelectedFile().toString();
                   exportButton.setEnabled(true);
               }else {
                   label.setText("Open Command canceled");
                   exportButton.setEnabled(false);
               }
           }
       });
       PathButton.setFont(new Font("Tahoma",Font.PLAIN,13));
       PathButton.setBounds(120,230,130,30);
       contentPane.add(PathButton);   
   }
}

0 个答案:

没有答案