• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

The question about JFileChooser

Hi all expert programmers ,
I've written a java applet for chatting . When a user send a file for each other , it will open a JFileChooser to save file . But I didn't use only JFileChooser , i used a JFrame in which i used JFileChooser and some JButton . Since I wanted to rename that file which i reveived , i wrote new file's name and clicked "Save" . But it still saved with old file's name . So i tried again , i wrote new file's name and entered , then clicked "Save" . At this time , it saved with new file' name . I cant understand why i must enter . Is anyone help me ?
0
clbit-org
Asked:
clbit-org
  • 3
  • 2
1 Solution
 
Dejan PažinCommented:

Could you post your code that works with JFileChooser? Its difficult to solve this problem without that.
0
 
clbit-orgAuthor Commented:
     private class SendFile extends Thread{
            String string = " ";
            JFrame receivedFrame;
            JLabel sendLabel1, imageLabel, sendLabel2;
            JFileChooser fileChooser;
            Box box1, box2, box3, box4;
            Color color;
            JPanel panel;
            JButton save, cancel;
            String[] pics = new String[] {"gif", "jpg", "tif"};
            String[] movies = new String[] {"mov", "avi"};
            String[] audios = new String[] {"au", "aiff", "wav"};
            String[] texts = new String[] {"txt"};
            String[] javas = new String[] {"java","class"};

            public void run()
            {
                  receivedFrame = new JFrame( "Received Send File" );
                  receivedFrame.setIconImage( (
                        new ImageIcon("Images18.jpg")).getImage() );
                  vectorReceivedFrame.addElement( receivedFrame );

                  color = new Color( 153, 0, 255 );
                  string = file;
                  save = new JButton( "Save" );
                  cancel = new JButton( "Cancel" );
                  panel = new JPanel();
                  sendLabel1 = new JLabel(
                        userSend + " has sent " + string + " to you." );
                  sendLabel1.setForeground( color );
                  sendLabel2 = new JLabel(
                        "Click Save to save file or Cancel to ignore" );
                  sendLabel2.setForeground( color );
                  imageLabel = new JLabel( new ImageIcon( "Images21.jpg" ) );

                  fileChooser = new JFileChooser();
                  fileChooser.setApproveButtonText( "Save" );
                  fileChooser.setControlButtonsAreShown( false );
                  fileChooser.setMultiSelectionEnabled( false );
                  fileChooser.addChoosableFileFilter(new SimpleFileFilter(pics,
                        "Images (*.gif, *.jpg, *.tif)"));
                  fileChooser.addChoosableFileFilter(new SimpleFileFilter(movies,
                        "Movies (*.mov, *.avi)"));
                  fileChooser.addChoosableFileFilter(new SimpleFileFilter(audios,
                        "Sounds (*.aiff, *.au, *.wav)"));
                  fileChooser.addChoosableFileFilter(new SimpleFileFilter(texts,
                        "Texts (*.txt)"));
                  fileChooser.addChoosableFileFilter(new SimpleFileFilter(javas,
                        "Javas (*.java, *.class)"));
                  fileChooser.setSelectedFile( new File( file ) );
                  
                  box1 = Box.createHorizontalBox();
                  box4 = Box.createHorizontalBox();
                  box2 = Box.createVerticalBox();
                  box3 = Box.createVerticalBox();

                  box4.add( save );
                  box4.add( cancel );
                  box3.add( sendLabel1 );
                  box3.add( sendLabel2 );
                  box1.add( imageLabel );
                  box1.add( box3 );
                  box2.add( box1 );
                  box2.add( fileChooser );
                  box2.add( box4 );

                  receivedFrame.getContentPane().add( panel );
                  panel.add( box2 );

                  receivedFrame.pack();
                  receivedFrame.setVisible( true );
                  receivedFrame.setLocation( 80, 5 );
                  receivedFrame.setResizable( false );
                  
                  CancelListener cancelListener = new CancelListener();
                  cancel.addActionListener( cancelListener );

                  SaveListener saveListener = new SaveListener();
                  save.addActionListener( saveListener );
                  
            }
            
            private class CancelListener implements ActionListener {

                  public void actionPerformed( ActionEvent event )
                  {
                        receivedFrame.dispose();
                  }
            }

            private class SaveListener implements ActionListener {

                  public void actionPerformed( ActionEvent event )
                  {
                        for ( int count = 0; count < vectorSendFile.size(); count++ )
                        {
                              dataSend = new DataSend();
                              dataSend = ( DataSend )vectorSendFile.elementAt( count );
                              if ( dataSend.fileName.trim().compareTo( string.trim() ) == 0 )
                              {
                                    try
                                    {
                                          FileOutputStream fileOutputStream = new FileOutputStream(
                                                fileChooser.getCurrentDirectory().getPath()
                                                + "\\" + fileChooser.getSelectedFile().getName());
                                          fileOutputStream.write( dataSend.arr );
                                          fileOutputStream.close();
                                    }
                                    catch (Exception exception)
                                    {
                                          JOptionPane.showMessageDialog( null,
                                                "Exception",
                                                "Error", JOptionPane.ERROR_MESSAGE);
                                    }
                              }
                        }
                        receivedFrame.dispose();
                        for ( int count = 0; count < vectorSendFile.size(); count++ )
                        {
                              dataSend = new DataSend();
                              dataSend = ( DataSend )vectorSendFile.elementAt( count );
                              if ( dataSend.fileName.trim().compareTo( string.trim() ) == 0 )
                              {
                                    vectorSendFile.removeElementAt( count );
                                    vectorReceivedFrame.removeElementAt( count );                                    
                              }
                        }
                  }
            }
      }

It is a part of my java code .
0
 
Dejan PažinCommented:

Now I got it. I found the problem and here is the solution:

1. You should not remove the buttons from the JFileChooser. So you have to remove this line:

fileChooser.setControlButtonsAreShown( false );

2. Now you have to add the listener to the JFileChooser rather then to your save button. To do that replace this line:

save.addActionListener( saveListener );

with this:

fileChooser.addActionListener( saveListener );

2. Since  you now have the Save and Cancel buttons already on your JFileChooser, you dont need your own, so remove the Box 'box4' and JButton 'save' and 'cancel' from your code

4. And the last  thing you have to do, is modify  SaveListener class, so that you will respond to Save and Cancel appropriately. Like this:

               public void actionPerformed( ActionEvent event )
               {

                  if (event.getActionCommand().equals(JFileChooser.APPROVE_SELECTION))
                  {
                      // Here comes code that needs to be executed if user pressed 'Save'
                  }
                  else
                  {
                     // Here comes code that needs to be executed if user pressed 'Cancel'
                  }


That should do the trick. Let me know if you need more help.




0
 
clbit-orgAuthor Commented:
Thanks for your helping :) . However , I dont want to change my buttons . I just want to know the reason of problem . I want to know why i must "Enter" .
0
 
Dejan PažinCommented:

You must press 'Enter' because the selectedFile property of  JFileChooser is changed either when you press JFileChooser's Save button or when you press 'Enter' in its text field.

Since you are not showing the JFileChoosers Save button (you are using your own for the same purpose), the only way the selectedFile property is going to change is when you press 'Enter'. Unfortunately the proerty doesnt change as you type, but only when you press 'Enter' or click Save button.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now