This site requires JavaScript, please enable it in your browser!
Greenfoot back
Rudolf
Rudolf wrote ...

2014/4/16

How to open a path with Greenfoot?

Rudolf Rudolf

2014/4/16

#
Hello programmers, I got a problem, I want to open a file on my Computer with Greenfoot. But how? I try it with this code but it didn´t work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
public class Bibliothek extends Actor
{
    /**
     * Act - do whatever the Bibliothek wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if (Greenfoot.mouseClicked( this))
        {
            if (mouse != null)
            {
                if (mouse.getClickCount() == 1)
                {
                    if (mouse.getButton() == 1)
                    {
  
                            Open();
  
                }
            }
        }
    }
}
        private void Open(){
        
            Desktop desktop = null;
    
    
     if (!Desktop.isDesktopSupported()) {
         
        return;
    }
    desktop = Desktop.getDesktop();
    String path = "Desktop/School ";
     
    try {
        File fPath=new File(path);
        if(!fPath.exists()){
           
            return;
  
        }
        if(!fPath.isDirectory()){
            
            return;
  
        }
        desktop.open(new File(path));
    } catch (IOException e) {
        
        e.printStackTrace();
         
        return;
    }
    }
  
}
bourne bourne

2014/4/16

#
This is how I have read a file before:
1
BufferedReader br = new BufferedReader(new FileReader(filePath));
Rudolf Rudolf

2014/4/17

#
Is there any possibilitiy to open only the Window with the File in? I got a Icon in my Programm and when I click on it I want to open a folder with my dokuments in. With the filereader I could only read the file in the folder.
davmac davmac

2014/4/17

#
I think you're saying you the file to open in a suitable program - you don't want to actually read or write the contents of the file from within your program? In that case your original approach was on the right track, I think. The main problem is that "Desktop/School" is likely not the correct path. The path must be absolute (or otherwise, relative to the current working directory, which in Greenfoot is the scenario directory). See this question on stackoverflow for some ideas.
Rudolf Rudolf

2014/4/21

#
Thanks a lot davmac, but its almost that what I want to do. It works just like a save Button. What I am searching for is an option to open a Folder and then open the files, doc,txt or Internetlinks with Word or Notpad or with my Browser. I want to open only the path, then I want to open the files without Greenfoot, just like i´m workin with my computer and open the folder himselfe step by step :)
You need to login to post a reply.