2

I was doing an assignment. Basically the assignment is done but I was trying to make it better by adding a GUI to it.

But I was encountering some issue on FileChooser because I don't quite understand how it works.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class CaesarCipherGui extends JFrame implements ActionListener{

private static final long serialVersionUID = 1L;
private static JButton Encrypt  = new JButton("Encrypttion");
private static JButton Decrypt  = new JButton("Decrypttion");
private static JPanel  panel    = new JPanel();

public static void main(String[] args) {
    new CaesarCipherGui();
}
private void addComponent(JPanel p, JComponent c, int x, int y, int width, int height, int align) {
    GridBagConstraints gc = new GridBagConstraints();
    gc.gridx = x;
    gc.gridy = y;
    gc.gridwidth = width;
    gc.gridheight = height;
    gc.weightx = 100.0;
    gc.weighty = 100.0;
    gc.insets = new Insets(5, 5, 5, 5);
    gc.anchor = align;
    gc.fill = GridBagConstraints.NONE;
    p.add(c, gc);
}

public CaesarCipherGui() {
    this.setSize(310, 192);
    this.setTitle("Caesar Cipher");
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setResizable(false);
    panel.setLayout(new GridBagLayout());

    addComponent(panel, Encrypt, 1, 4, 1, 1, GridBagConstraints.WEST);
    Encrypt.addActionListener(this);

    addComponent(panel, Decrypt, 1, 4, 1, 1, GridBagConstraints.EAST);
    Decrypt.addActionListener(this);


    this.add(panel);
    this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == Encrypt){
            Scanner console = new Scanner(System.in);
            System.out.print("Input file: ");
            String inputFileName = console.next();
            System.out.print("Output file: ");
            String outputFileName = console.next();

            try{ 
                FileReader reader = new FileReader("C:/"+inputFileName+".txt");
                Scanner in = new Scanner(reader);
                PrintWriter out = new PrintWriter("C:/"+outputFileName+".txt");

                    while (in.hasNextLine()){
                        String line = in.nextLine();
                        String outPutText = "";
                        int key = 3;

                        for (int i = 0; i < line.length(); i++) {
                            char c = line.charAt(i);
                            if (c >= 'a' && c <= 'z') {
                                c += key % 26;
                                if (c < 'a')
                                    c += 26;
                                if (c > 'z')
                                    c -= 26;
                            }
                            if (c >= 'A' && c <= 'Z') {
                                c += key % 26;
                                if (c < 'A')
                                    c += 26;
                                if (c > 'Z')
                                    c -= 26;
                            }
                            if (c == ' '){
                                c = '#';
                            }
                            outPutText += c;
                        }
                        out.println(outPutText);

                    }
                    out.close();
                    }
            catch (IOException exception){
                System.out.println("Error processing file:" + exception);
            }
    }
    if (e.getSource() == Decrypt){
        Scanner console = new Scanner(System.in);
        System.out.print("Input file: ");
        String inputFileName = console.next();
        System.out.print("Output file: ");
        String outputFileName = console.next();

        try{ 
            FileReader reader = new FileReader("C:/"+inputFileName+".txt");
            Scanner in = new Scanner(reader);
            PrintWriter out = new PrintWriter("C:/"+outputFileName+".txt");

                while (in.hasNextLine()){
                    String line = in.nextLine();
                    String outPutText = "";
                    int key = -3;

                    for (int i = 0; i < line.length(); i++) {
                        char c = line.charAt(i);
                        if (c >= 'a' && c <= 'z') {
                            c += key % 26;
                            if (c < 'a')
                                c += 26;
                            if (c > 'z')
                                c -= 26;
                        }
                        if (c >= 'A' && c <= 'Z') {
                            c += key % 26;
                            if (c < 'A')
                                c += 26;
                            if (c > 'Z')
                                c -= 26;
                        }
                        if (c == '#'){
                            c = ' ';
                        }
                        outPutText += c;
                    }
                    out.println(outPutText);

                }
                out.close();
                }
        catch (IOException exception){
            System.out.println("Error processing file:" + exception);
        }
}
}
}

As the code above is my assignment, but I wish to add the file chooser for the input filename and save file for the output filename, how am I gonna do it? Or you can just modify the part that needs to change and show me.

0

2 Answers 2

1

no, you add an actionlistener to a button and define the filechooser:

cmdSearch = new AbstractAction("Search", null) {
    public void actionPerformed(ActionEvent evt) {
        final JFileChooser fc = new JFileChooser();
        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        txtSearch.setText( (fc.showOpenDialog(YOURCLASSNAME.this) == JFileChooser.APPROVE_OPTION) ? fc.getSelectedFile().toString() : txtSearch.getText() );                                    
    }
};
3
  • ok. i got this one. then how about the savedialog? is it the same way? Nov 22, 2012 at 16:21
  • in principle yes, check the link, posted above already...
    – pyr0
    Nov 22, 2012 at 16:44
  • thanks for the helps. now i am quite clear on the filechooser dialog now. Nov 22, 2012 at 17:19
1

I guess you should start from the official documentation. Using a JFileChooser is quite easy, you just need to:

  • show an open or save dialog according to what you are doing
  • check the return value of the show function to see that the user effectively chose a file
  • retrieve the File through getSelectedFile() method
  • optionally use a custom FileFilter to let the user see just the file types you want
1
  • i saw that link. but what i want is that when i press the encrypt button, it will pop up the filechooser and then right after i select the file, then savedialog will prompt to save the file. so how to do that? is it i need to create another class? Nov 22, 2012 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.