Ver Mensaje Individual
  #1 (permalink)  
Antiguo 24/01/2014, 13:50
santunvel
 
Fecha de Ingreso: enero-2013
Mensajes: 54
Antigüedad: 11 años, 3 meses
Puntos: 0
JFileChooser no se oculta al ejecutar mi aplicación

Es una aplicación que una de sus funciones es guardar archivos, el JFileChooser se queda allí sin hacer nada, el boton guardar nisiquiera tiene funcion ni nada, estorba, pero al presionar el boton guradar que es un JButton con evento de llamar a otro JFileChooser y ya me lo guarda bien que si funciona y ya guardo el archivo que quiero

Código Java:
Ver original
  1. package ventana;
  2.  
  3.  
  4.  
  5.  
  6. public class ClaseFrame extends javax.swing.JFrame implements ActionListener {
  7.     String texto;
  8.     private Container contenedor;
  9.  
  10.     public ClaseFrame() {
  11.         initComponents();
  12.         contenedor=getContentPane();
  13.             contenedor.setLayout(null);
  14.            
  15.             /*Creamos el objeto*/
  16.             fileChooser=new JFileChooser();
  17.                         //new JFileChooser().setVisible(false);
  18.            
  19.             /*Propiedades del Label, lo instanciamos, posicionamos y
  20.              * activamos los eventos*/
  21.             labelTitulo= new JLabel();
  22.             labelTitulo.setText("COMPONENTE JFILECHOOSER");
  23.             labelTitulo.setBounds(110, 20, 180, 23);
  24.            
  25.             areaDeTexto = new JTextArea();
  26.             areaDeTexto.setLineWrap(true);
  27.             areaDeTexto.setWrapStyleWord(true);
  28.             scrollPaneArea = new JScrollPane();
  29.             scrollPaneArea.setBounds(20, 50, 350, 270);
  30.             scrollPaneArea.setViewportView(areaDeTexto);
  31.                
  32.             botonAbrir= new JButton();
  33.             botonAbrir.setText("Abrir");
  34.             botonAbrir.setBounds(100, 330, 80, 23);
  35.             botonAbrir.addActionListener(this);
  36.            
  37.             botonGuardar= new JButton();
  38.             botonGuardar.setText("Guardar");
  39.             botonGuardar.setBounds(220, 330, 80, 23);
  40.             botonGuardar.addActionListener(this);
  41.            
  42.             /*Agregamos los componentes al Contenedor*/
  43.             contenedor.add(labelTitulo);
  44.             contenedor.add(scrollPaneArea);
  45.             contenedor.add(botonAbrir);
  46.             contenedor.add(botonGuardar);
  47.             //Asigna un titulo a la barra de titulo
  48.             setTitle("CoDejaVu : Ventana JFileChooser");
  49.             //tama�o de la ventana
  50.             setSize(400,400);
  51.             //pone la ventana en el Centro de la pantalla
  52.             setLocationRelativeTo(null);
  53.            
  54.             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  55.     }
  56.  
  57.  
  58.     @SuppressWarnings("unchecked")
  59.                        
  60.  
  61.     private void botonAbrirActionPerformed(java.awt.event.ActionEvent evt) {                                          
  62.         // TODO add your handling code here:
  63.         String archivo=abrirArchivo();
  64.                 areaDeTexto.setText(archivo);
  65.     }                                          
  66.  
  67.     private void botonGuardarActionPerformed(java.awt.event.ActionEvent evt) {                                            
  68.         // TODO add your handling code here:
  69.         guardarArchivo();
  70.     }                                            
  71.  
  72.    
  73.     public static void main(String args[]) {
  74.      
  75.         try {
  76.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  77.                 if ("Nimbus".equals(info.getName())) {
  78.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  79.                     break;
  80.                 }
  81.             }
  82.         } catch (ClassNotFoundException ex) {
  83.             java.util.logging.Logger.getLogger(ClaseFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  84.         } catch (InstantiationException ex) {
  85.             java.util.logging.Logger.getLogger(ClaseFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  86.         } catch (IllegalAccessException ex) {
  87.             java.util.logging.Logger.getLogger(ClaseFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  88.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  89.             java.util.logging.Logger.getLogger(ClaseFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  90.         }
  91.         //</editor-fold>
  92.  
  93.         java.awt.EventQueue.invokeLater(new Runnable() {
  94.             public void run() {
  95.                 new ClaseFrame().setVisible(true);
  96.             }
  97.         });
  98.     }
  99.                  
  100.  
  101.     @Override
  102.     public void actionPerformed(ActionEvent evento) {
  103.         if (evento.getSource()==botonAbrir)
  104.             {
  105.                 String archivo=abrirArchivo();
  106.                 areaDeTexto.setText(archivo);
  107.             }
  108.            
  109.             if (evento.getSource()==botonGuardar)
  110.             {
  111.                 guardarArchivo();
  112.             }
  113.     }
  114.  
  115.     private String abrirArchivo() {
  116.         String aux="";     
  117.             texto="";
  118.        
  119.             try
  120.             {
  121.                 fileChooser.showOpenDialog(this);
  122.                 File abre=fileChooser.getSelectedFile();
  123.  
  124.                 /*recorremos el archivo, lo leemos para plasmarlo
  125.                  *en el area de texto*/
  126.                 if(abre!=null)
  127.                 {              
  128.                     FileReader archivos=new FileReader(abre);
  129.                     BufferedReader lee=new BufferedReader(archivos);
  130.                     while((aux=lee.readLine())!=null)
  131.                         {
  132.                          texto+= aux+ "\n";
  133.                         }
  134.  
  135.                     lee.close();
  136.                 }          
  137.             }
  138.             catch(IOException ex)
  139.             {
  140.               JOptionPane.showMessageDialog(null,ex+"" +
  141.                     "\nNo se ha encontrado el archivo",
  142.                     "ADVERTENCIA!!!",JOptionPane.WARNING_MESSAGE);
  143.             }
  144.                 return texto;
  145.     }
  146.  
  147.     private void guardarArchivo() {
  148.         try
  149.             {
  150.                 String nombre="";
  151.                 JFileChooser file=new JFileChooser();
  152.                 file.showSaveDialog(this);
  153.                 File guarda =file.getSelectedFile();
  154.        
  155.                 if(guarda !=null)
  156.                 {
  157.                     nombre=file.getSelectedFile().getName();
  158.                    
  159.                     FileWriter  save=new FileWriter(guarda+".txt");
  160.                     save.write(areaDeTexto.getText());
  161.                     save.close();
  162.                     JOptionPane.showMessageDialog(null,
  163.                             "El archivo se a guardado Exitosamente",
  164.                             "Informaci�n",JOptionPane.INFORMATION_MESSAGE);
  165.                 }
  166.              }
  167.            catch(IOException ex)
  168.            {
  169.              JOptionPane.showMessageDialog(null,
  170.                      "Su archivo no se ha guardado",
  171.                      "Advertencia",JOptionPane.WARNING_MESSAGE);
  172.            }
  173.        
  174.     }
  175. }