JFileChooser - problem z zapisaniem nazwy pliku

0

Mam taki kod:

 
                JFileChooser jFileChooser = new JFileChooser();
                jFileChooser.addChoosableFileFilter(new MyFileFilter());
                jFileChooser.setAcceptAllFileFilterUsed(false);
                int returnVal = jFileChooser.showSaveDialog(GeneralWindow.instance);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = jFileChooser.getSelectedFile();
                    Utils.saveFile(file);
                }

<image>okno zapisu pliku.jpg</image>
Do tego mam filter, który powinien umożliwić zapis i odczyt tylko pliku 'xml'.
I tutaj jest problem, wpisuje plik jak na zrzucie ekranu, a przy pobraniu pliku metodą 'getSelectedFile()' dostaje plik bez rozszerzenia, czyli:
c:\nazwa
zamiast
c:\nazwa.xml

  1. Jak to zmienić, aby zawsze dodawał rozszerzenie prawidłowe?

  2. Jeszcze drugie pytanie, czy idzie jakoś zrobić, aby całe okno JFileChooser było po polsku, czyli 'zapisz', zamiast 'save'?

1

/* Funkcja lokalizująca napisy w okienkach dialogowych typu Otwórz/Zapisz) */
    /* @version 1.0 */
    /* @param komponent - nazwa komponentu z oknem dialogowym
     * @return Metoda nie zwraca żadnej 
     */
    public void LokalizujNapisyPL(javax.swing.JComponent komponent)
    {
      		UIManager.put("FileChooser.openDialogTitleText", "Otwórz");
		UIManager.put("FileChooser.lookInLabelText", "Szukaj w : ");
		UIManager.put("FileChooser.openButtonText", "Otwórz");
		UIManager.put("FileChooser.cancelButtonText", "Anuluj");
		UIManager.put("FileChooser.fileNameLabelText", "Nazwa pliku : ");
		UIManager.put("FileChooser.filesOfTypeLabelText", "Typ plików : ");
		UIManager.put("FileChooser.openButtonToolTipText", "Otwórz wybrany plik");
		UIManager.put("FileChooser.cancelButtonToolTipText","Anuluj");
		UIManager.put("FileChooser.fileNameHeaderText","Nazwa pliku : ");
		UIManager.put("FileChooser.upFolderToolTipText", "Poziom wyżej");
		UIManager.put("FileChooser.homeFolderToolTipText","Pulpit");
		UIManager.put("FileChooser.newFolderToolTipText","Twórz nowy folder");
		UIManager.put("FileChooser.listViewButtonToolTipText","Lista");
		UIManager.put("FileChooser.newFolderButtonText","Nowy folder");
		UIManager.put("FileChooser.renameFileButtonText", "Zmień nazwę pliku");
		UIManager.put("FileChooser.deleteFileButtonText", "Usuń plik");
		UIManager.put("FileChooser.filterLabelText", "Typ pliku : ");
		UIManager.put("FileChooser.detailsViewButtonToolTipText", "Szczegóły");
		UIManager.put("FileChooser.fileSizeHeaderText","Rozmiar");
		UIManager.put("FileChooser.fileDateHeaderText", "Zmodyfkowano");
		SwingUtilities.updateComponentTreeUI(komponent);  
    }
 

gdzieś w kodzie przed użyciem ale po stworzeniu już egzemplarza klasy JFileChoser..

JFileChooser OtworzPlikjFileChooser = new JFileChooser();
        LokalizujNapisyPL(OtworzPlikjFileChooser);
OtworzPlikjFileChooser.showOpenDialog(frame);
 
2

Ja używam czegoś takiego (klawisze skrótów też trzeba zmienić):

        UIManager.put("FileChooser.lookInLabelText","Szukaj w");
        UIManager.put("FileChooser.lookInLabelMnemonic",""+KeyEvent.VK_W);

        UIManager.put("FileChooser.saveInLabelText","Zapisz w");
        UIManager.put("FileChooser.saveInLabelMnemonic",""+KeyEvent.VK_W);

        UIManager.put("FileChooser.fileNameLabelText","Nazwa pliku:");
        UIManager.put("FileChooser.fileNameLabelMnemonic",""+KeyEvent.VK_N);

        UIManager.put("FileChooser.filesOfTypeLabelText","Pliki typu:");
        UIManager.put("FileChooser.filesOfTypeLabelMnemonic",""+KeyEvent.VK_P);

        UIManager.put("FileChooser.upFolderToolTipText","Poziom wyżej");
        UIManager.put("FileChooser.homeFolderToolTipText","Pulpit");
        UIManager.put("FileChooser.newFolderToolTipText","Nowy katalog");
        UIManager.put("FileChooser.listViewButtonToolTipText","Lista");
        UIManager.put("FileChooser.detailsViewButtonToolTipText","Szczegóły");

        UIManager.put("FileChooser.fileNameHeaderText","Nazwa");
        UIManager.put("FileChooser.fileSizeHeaderText","Rozmiar");
        UIManager.put("FileChooser.fileTypeHeaderText","Typ");
        UIManager.put("FileChooser.fileDateHeaderText","Modyfikacja");
        UIManager.put("FileChooser.fileAttrHeaderText","Atrybuty");

        UIManager.put("FileChooser.newFolderErrorText","Błąd podczas tworzenia katalogu");

        UIManager.put("FileChooser.saveButtonText","Zapisz");
        UIManager.put("FileChooser.saveButtonMnemonic",""+KeyEvent.VK_Z);

        UIManager.put("FileChooser.openButtonText","Otwórz");
        UIManager.put("FileChooser.openButtonMnemonic",""+KeyEvent.VK_O);

        UIManager.put("FileChooser.cancelButtonText","Rezygnacja");
        UIManager.put("FileChooser.openButtonMnemonic",""+KeyEvent.VK_R);

        UIManager.put("FileChooser.openDialogTitleText","Otwieranie");
        UIManager.put("FileChooser.saveDialogTitleText","Zapisywanie");

        UIManager.put("FileChooser.saveButtonToolTipText","Zapisanie pliku");
        UIManager.put("FileChooser.openButtonToolTipText","Otwarcie pliku");
        UIManager.put("FileChooser.cancelButtonToolTipText","Rezygnacja");
        UIManager.put("FileChooser.acceptAllFileFilterText","Wszystkie pliki");

i wywołuję metodę zawierającą ten kod pierwszą instrukcją metody main().

0

Bardzo fajne to i działa(pkt.2).

Jednak nadal nie rozwiązuje problemu z pkt.1 ?

Ma ktoś pomysł, na pewno nie jedna osoba zapisywała plik z określonym rozszerzeniem?

0

Ty musisz dodać rozszerzenie, po wpisaniu przez użytkownika nazwy pliku zrób takie coś

     if(!nameOfFile.toLowerCase().endsWith("xml"))
     {
         nameOfFile+=".xml";
     }
0

Ok, pewnie dobra porada.
Ale skąd wziąć pełną ściężke by dodać do pliku(poza tym do obiektu typu 'File' nie mogę dodać ".xml").

File file = jFileChooser.getSelectedFile();

I co z takim plikiem zrobić bez rozszerzenia?

0
String nameOfFile = jFileChooser.getSelectedFile().getAbsolutePath();

1 użytkowników online, w tym zalogowanych: 0, gości: 1