2017-10-04 3 views
0

그래서 텍스트 파일을 연 다음 textArea로 정의한 JtextArea에 내용을 붙여 넣는 파일 선택기를 만들려고했습니다. 하지만 showOpenDialog에 인수 (this)가있는 동안 오류를 표시하지 못하고 조사를했는데 답변이 null로 채워져서 파일 선택기가 작동하지만 파일 내용을 인쇄하려고 할 때 그것도 null을 반환합니다. 나는 이클립스 (Eclipse) 프로그램을 사용하여 자동 채워진 코드를 사용하고있다. 나는 Java에 상당히 익숙하지 않으며 잘못되었다는 단서가 없다. 이것이 여기에 물건을 게시하는 방법이 아니라면 정말로 미안.showopenDialog가 actionlistener 아래에서 작동하지 않습니다.

JButton btnNewButton = new JButton("Bladeren"); 
btnNewButton.addActionListener(
    new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      JFileChooser fileChooser = new JFileChooser(); 
      fileChooser.setCurrentDirectory(new File(System.getProperty("user.home"))); 
      int result = fileChooser.showOpenDialog(this); 
      if (result == JFileChooser.APPROVE_OPTION) { 
       File selectedFile = fileChooser.getSelectedFile(); 
       System.out.println("Selected file: " + selectedFile.getAbsolutePath()); 
       String content = readFile(selectedFile, StandardCharsets.UTF_8); 
       System.out.println(content); 
       textArea.setText(content); 
      } 

     } 

     private String readFile(File selectedFile, Charset utf8) { 
      // TODO Auto-generated method stub 
      return null; 
     } 
    } 
); 

답변

0

당신은 매개 변수 유형 Component에서 수있는 API로 볼 수있다. 귀하의 예에서 this의 의미는 무엇입니까? 다른 클래스/인터페이스가 extends이거나 implements입니까?

readFile 메서드의 코드를 붙여 넣지 않았지만 null을 반환하는 이유를 묻습니다. 이렇게하면 우리는 당신을 도울 수 없으므로 코드를 게시하십시오.

+0

나는 매우 어리 석며 내 readfile 방법으로 너무 많이 보지 않았다. 이것을 지적 해 주셔서 감사 드리며 올바른 방향으로 이끌어 주었고 이제는 고맙다. – 221flo221

관련 문제