Xander
Xander

Reputation: 1715

JButton not working

I recently developed a GUI for my application , that runs shell scripts on Linux Ubuntu 10.04. It was working fine until today. I designed a button so that , clicking the button would open a prompt for selecting a file. The JFileChooser is used in the actions. But from today morning, without any error, clicking on the button does not do anything. It is not simply working all of a sudden. What is this error? Can somebody please help? Is this any error related to the OS/Java installed?

Thanks in advance.

int yorn = JOptionPane.YES_OPTION;
String user = System.getProperty("user.home");
File file = new File(user+"/.afile");
if (file.exists()){
    yorn = JOptionPane.showConfirmDialog(this,"There seem to have a Previous project , Do you want to back up??");

    if (yorn == JOptionPane.YES_OPTION){ // yesoption clicked!

        //choose folder to open!
        JFileChooser jf = new JFileChooser();
        jf.setDialogTitle("Back up location?");
        String pathname = null;
        jf.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        jf.showOpenDialog(null);
        File f = jf.getSelectedFile();
        pathname = f.getAbsolutePath();
        // JOptionPane.showMessageDialog(this,pathname);
        try{
            Process copy = Runtime.getRuntime().exec("sh "+user+"/myprojects/.backup.sh "+pathname);
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(this, e);
        }

        //open file for analysing..
        JOptionPane.showMessageDialog(this,"Backing up complete.. ");

        JFileChooser jf2 = new JFileChooser();
        jf2.setDialogTitle("Open the Codebase");
        String pathname2 = null;
        jf2.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        jf2.showOpenDialog(null);
        File f2 = jf2.getSelectedFile();
        pathname2 = f2.getAbsolutePath();
        String username = System.getProperty("user.home");
        File writefile = new File(username+"/.afile");              

        Writer output = null;
        try {
            output = new BufferedWriter(new FileWriter(writefile));
            output.write(pathname2);
            output.close();

            //    JOptionPane.showMessageDialog(this,username);
        } catch (IOException ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        }

    } else if (yorn == JOptionPane.NO_OPTION){       

        JFileChooser jf = new JFileChooser();
        jf.setDialogTitle("Open the Codebase");
        String pathname = null;
        jf.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        jf.showOpenDialog(null);
        File f = jf.getSelectedFile();          
        pathname = f.getAbsolutePath();
        String username = System.getProperty("user.home");
        File writefile = new File(username+"/.afile");              
        Writer output = null;
        try {
            output = new BufferedWriter(new FileWriter(writefile));
            output.write(pathname);
            output.close();

            //    JOptionPane.showMessageDialog(this,username);
        } catch (IOException ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
} 

This is the code.. What is does, is that , it check if afile exist and if it does, prompt for a backup. If the back up is an yes, then do back up and open a new window for another selection , and if back up is a no , then the file selection is opened. As i mentioned, this was working till the time I asked the question. It stopped working all of a sudden. Thanks for all the replies.

Upvotes: 1

Views: 450

Answers (1)

nathan
nathan

Reputation: 1119

You should as asked before provide an example but here is a piece of code that works for me, tested under linux/MAC OS/Windows:

/* chooser is of type JFileChooser of course */
chooser.setDialogTitle("title of your dialog");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY /* change it to fit your needs */);
chooser.setAcceptAllFileFilterUsed(false);
chooser.showOpenDialog(null);

Upvotes: 2

Related Questions