Java - Problemas con eventos y ficheros

 
Vista:

Problemas con eventos y ficheros

Publicado por Juande (2 intervenciones) el 07/03/2005 00:01:36
El problema es el siguiente:

Estoy haciendo un sencillo editor de texto y el problema viene al intentar guardar el texto en un fichero.

Captura los eventos de los botones con un actionListener y el problema esta al llamar a GuardaFitxer(); dentro del actionPerformed(), ya que "GuardaFitxer() throws IOException" y no me deja.

Se supone que al actionPerformed le tendria que poner un "throws IOException", pero entonces me dice que no puede ser.

Entonces creo que lo que necesito es capturar los eventos con alguna interfaz que me permita hacer un throws IOException para poder ejecutar GuardaFitxer().

Alguien me puede ayudar ??

Este es el codigo, por si asi os queda mas claro:

import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

class Fitxers extends JFrame implements ActionListener
{
/* Frame principal */
JTextArea area = new JTextArea();

/* Frame Guardar */
JFrame fGuardar = new JFrame();
JLabel lnom = new JLabel("Nom del fitxer:");
JTextField tnom = new JTextField();


public Fitxers() {
setSize(400,200);

MenuBar menu = new MenuBar();
Menu menuArxiu = new Menu("Arxiu");
MenuItem mAGuardar = new MenuItem("Guardar");
MenuItem mAObrir = new MenuItem("Obrir");
MenuItem mASortir = new MenuItem("Sortir");

menuArxiu.add(mAGuardar);
menuArxiu.add(mAObrir);
menuArxiu.add(mASortir);

mAGuardar.addActionListener(this);
mAObrir.addActionListener(this);
mASortir.addActionListener(this);

menu.add(menuArxiu);
setMenuBar(menu);

getContentPane().setLayout(new BorderLayout());



getContentPane().add(area,BorderLayout.CENTER);

setVisible(true);
}

public static void main( String args[] ) {
Fitxers mainApp = new Fitxers();
}

public void Guarda() {

fGuardar.setTitle("Guardar...");
fGuardar.setSize(300,120);

tnom.setPreferredSize(new Dimension(120,30));
JButton acc = new JButton("Acceptar");
JButton can = new JButton("Cancelar");

acc.addActionListener(this);

can.addActionListener(this);

fGuardar.getContentPane().setLayout(new FlowLayout());
fGuardar.getContentPane().add(lnom);
fGuardar.getContentPane().add(tnom);
fGuardar.getContentPane().add(acc);
fGuardar.getContentPane().add(can);

fGuardar.setVisible(true);


}

public void GuardaFitxer() throws IOException,FileNotFoundException {
/* Flujos */
FileOutputStream salida = new FileOutputStream(tnom.getText()+".txt");
DataOutputStream filtre = new DataOutputStream(salida);
try {
filtre.writeChars(area.getText()+"\n");
}
catch ( IOException e ) {
System.out.println(e.getMessage());
}
finally {
salida.close();
}
}

public void actionPerformed(ActionEvent e) throws IOException {
Object obj = e.getSource();

if (obj instanceof JButton) {
JButton jb = (JButton)obj;

if(jb.getLabel() == "Acceptar")
{
GuardaFitxer();
fGuardar.dispose();
fGuardar.setVisible(false);
}
}

if(obj instanceof MenuItem) {
MenuItem mi = (MenuItem)obj;

if(mi.getLabel() == "Guardar") {
Guarda();

}
if(mi.getLabel() == "Obrir") {

}
if(mi.getLabel() == "Sortir") {
Close();
}
}

}

public void Close() {
dispose();
System.exit(0);
}
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:Problemas con eventos y ficheros

Publicado por ryudoo (30 intervenciones) el 07/03/2005 02:05:18
Al parecer el metodo actionPerformed() no permite lanzar una Exception, en el metodo GuardaFitxer() lanzas dos Exception; IOException, FileNotFoundException lo unico que debes hacer es capturarlas dentro del metodo actionPerformed() es decir:

if(jb.getLabel() == "Acceptar"){
try{
GuardaFitxer();
fGuardar.dispose();
fGuardar.setVisible(false);
}catch(IOException ex){
ex.printStackTrace();
}
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar