views:

320

answers:

2

i have this issue working but i would like to know if there is a better way of adding the file extension?

what i am doing right now is:

String filePath = chooser.getSelectedFile().getAbsoluteFile() + ".html";

im adding the extension hard coded. and then saving to it.

just wondering if there is a more robust/logical manner this can be implemented?

thank you for your time.

EDIT: i ask this as i would like my app to be portable across platforms. so adding .html manually i may make this a windows only solution.

EDIT: i think ive surfed enough to know that .html hard coded is safe as i havent found any documentation that says dont take this approach (not completely sure).

ISSUE: also if i want to save the file in another format, text, for example how do i detect that the user selected which format?

FileNameExtensionFilter can add filters to the dialog but how do i get the return value for file type selected?

EDIT: i have studied this but still unclear how to retrive user selected file type.

EDIT: this is a rephrase of my issue:

alt text my question is how can i retrieve/find out which one of the two filters the user has selected as the save format. HTML or JPEG? how do i retrieve this info from JFileChooser? thank you.

EDIT: found something out: it has something to do with JFileChooser.getFileFilter() your help still welcome.

EDIT: getFileFilter() and FileNameExtensionFilter comparasion solved this issue.

A: 

I don't understand what it is you're trying to do. Are you trying to save the selected file in some other format than it already is of? The path of the selected file will contain the file extension, so you don't need to add it manually. The following, for example, will print "/Users/banang/Documents/anything.html" to the screen if the file anything.html is selected.

JFileChooser chooser = new JFileChooser();
chooser.showSaveDialog(null);
System.err.println(chooser.getSelectedFile().getCanonicalPath());

Please try to clarify your question a bit.

Banang
A: 

here is the code snippet that solves the issue:

                JFileChooser chooser = new JFileChooser();

            chooser.setMultiSelectionEnabled(false);

            chooser.setAcceptAllFileFilterUsed(false);

            FileNameExtensionFilter filter = new FileNameExtensionFilter("HTML Documents", "htm", "html");

            chooser.setFileFilter(filter);

            int option = chooser.showSaveDialog(ChatGUI.this);

            if (option == JFileChooser.APPROVE_OPTION) {

                // Set up document to be parsed as HTML
                StyledDocument doc = (StyledDocument)textPaneHistory.getDocument();

                HTMLEditorKit kit = new HTMLEditorKit();

                BufferedOutputStream out;

                try {

                    System.out.println(chooser.getFileFilter());

                    if (chooser.getFileFilter() == filter){
                        System.out.println("ha ha");
                    }
iEisenhower