0

I need to add this code to an action listener. I need it to work with my Jbutton (Reverse) When trying to add the action to reverse it does not work at all sends back multiple errors. The code works perfect by itself just not inside the action listener I used:

import java.io.FileReader;
import java.io.BufferedReader
import java.io.FileNotFoundException;
import java.util.*;
class FileReaderReverse
 {
 public static void main(String[] args) throws IOException,FileNotFoundException
 {
FileReader fr=new FileReader("input.txt");
BufferedReader br=new BufferedReader(fr);
String s;
List<String>
tmp = new ArrayList<String>();
do{
s = br.readLine();
tmp.add(s);
}while(s!=null);
for(int i=tmp.size()-1;i>=0;i--) {
System.out.println(tmp.get(i));
  }
 }
}

I tried to do it like this and it didn't work. This is how I did the rest of my code while inputting a file. Doesn't seem to work. I have buttons added. I am trying to add it to my jButton (Reverse).

public void actionPerformed(ActionEvent e) {
try {
FileReader fr=new FileReader("input.txt");
BufferedReader br=new BufferedReader(fr);
String s;

List<String> tmp = new ArrayList<String>();
do{
s = br.readLine();
tmp.add(s);
}while(s!=null);
for(int i=tmp.size()-1;i>=0;i--) {
System.out.println(tmp.get(i));
} catch (Exception exp) {   exp.printStackTrace();
} finally {
 try {
reader.close();
 } catch (Exception exp) {
  }
 }
}
Mirza Sisic
  • 2,401
  • 4
  • 24
  • 38
  • 3
    do you add the actionListener to the button? – SomeJavaGuy Mar 01 '16 at 13:30
  • yes. JButton Write = new JButton("Write"); JButton Read = new JButton("Read"); JButton Reverse = new JButton("Reverse"); add(Read); add(Reverse); add(Write); – user5992786 Mar 01 '16 at 13:36
  • 2
    Please edit your question instead of a comment (and while you're at it fix the formatting as well). Besides that, please be more specific than "Doesn't seem to work." - _In which way_ doesn't it work? What does or does not happen when you click the button? – Thomas Mar 01 '16 at 13:44
  • 1
    Your comment also states that you create buttons and add them to the ui but you don't add any listener to the buttons themselves. – Thomas Mar 01 '16 at 13:46
  • to be honest it wont even let me put it in like i tried. the reason i tried it that way is because i my other auction buttons worked that way. I had them doing different things though. – user5992786 Mar 01 '16 at 14:00
  • @Override public void actionPerformed(ActionEvent e) { Reader reader = null; try { reader = new FileReader(new File("input.txt")); a.read(reader, ""); } catch (Exception exp) { exp.printStackTrace(); } finally { try { reader.close(); } catch (Exception exp) { } } } }); – user5992786 Mar 01 '16 at 14:01
  • Please stop using the comments for code and find the appropriate edit link between your question and the first comment – OneCricketeer Mar 01 '16 at 14:03
  • Please see [How do you add an ActionListener onto a JButton in Java](http://stackoverflow.com/questions/284899/how-do-you-add-an-actionlistener-onto-a-jbutton-in-java) and let us know what doesn't work with that – OneCricketeer Mar 01 '16 at 14:06
  • I get the errors of cannot find variable reader. unreported exception filenotfound should be declared or thrown. unreported exception IO should be declared or thrown. – user5992786 Mar 01 '16 at 14:39

0 Answers0