Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

attach two buttons on the frame

  Asked By: Diem    Date: Oct 11    Category: Java    Views: 603
  

i tried to attach two buttons on the frame

save1 : To save data into the Test.txt file through I/O operation

open1 : To retreive data from the Test.txt file through I/O operation

The problem is actionPerformed(....) is not allowing to apply throws IOException
it being overridden method of ActionListener class.

if i dont apply throws.....than error is exception has to be applied and no
compilation is executed.

the codes with the errors are being give below

ERRORS:

=====================================

D:\java\I.O\Framet.java:58: unreported exception java.io.IOException; must be
caught or declared to be thrown
saveData(text1.getText());
^
D:\java\I.O\Framet.java:64: unreported exception java.io.IOException; must be
caught or declared to be thrown
openData();
^
2 errors

Tool completed with exit code 1


CODES

============================================================

import java.awt.*;
import java.awt.event.*;
import java.io.*;


public class Framet
{

public static void main(String[]args) throws IOException
{
Button1 f = new Button1();
f.frame();

}
}

class Button1 implements ActionListener
{
Button save1,open1,exit;
TextArea text1;
FlowLayout flow;

public void frame()
{

Frame frame = new Frame("Frame");

flow = new FlowLayout();
frame.setLayout(flow);
frame.setBounds(0,0,500,500);

text1 = new TextArea(5,30);

save1 = new Button("Save");
open1 = new Button("Open");
exit = new Button("Exit");

frame.add(text1);

frame.add(save1);
frame.add(open1);
frame.add(exit);

frame.setVisible(true);

save1.addActionListener(this);
open1.addActionListener(this);
exit.addActionListener(this);

}

public void actionPerformed(ActionEvent ee)
{

if(ee.getSource() == save1)
{
String text2 = text1.getText();
saveData(text1.getText());

}

if(ee.getSource() == open1)
{
openData();
}

if(ee.getSource() == exit)
{
System.exit(0);
}
}

public void saveData(String text2) throws IOException
{
char[] textArray = text2.toCharArray();
FileOutputStream oFile = new FileOutputStream("Test.txt");
for(int i=0;i<textArray.length;i++)
{
oFile.write(textArray[i]);
}

oFile.close();
}

public void openData() throws IOException
{
File file = new File("Test.txt");
System.out.println("Path is "+file.getAbsolutePath());
FileInputStream iFile = new FileInputStream(file);

int data;
String string;
char value = 'q';

Character chart = new Character('a');


while( (data = iFile.read()) != -1)
{
value = (char)data;
string = chart.toString(value);
text1.append(string);
}

iFile.close();

}

}

Share: 

 

No Answers Found. Be the First, To Post Answer.

 
Didn't find what you were looking for? Find more on attach two buttons on the frame Or get search suggestion and latest updates.




Tagged: