|
I´m trying to make my GUI read from a file, and this code is a part of
my controlPanel class where I control all action in the GUI. I´m not sure
if I´m on the right path here, I get some error [Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
Unhandled exception type FileNotFoundException] in the line where "FileReader reader = new FileReader(file);" is.....
Hope this is enough to get some hints.
thx
public class ControlPanel extends JPanel{
static String file;
static FileWriter fw;
static BufferedWriter bw;
static PrintWriter outFile;
JButton start = new JButton ("Start");
JButton stop = new JButton("Stop");;
JButton step = new JButton("Step");
private ArrayList dataArray = new ArrayList();
private Point bestPlace = new Point();
private int scenario = 0;
private HashMap map = new HashMap();
int[] weights;
private JLabel fileName;
public ControlPanel() throws IOException
{
file = "/forum/results.txt";
fw = new FileWriter(file);
bw = new BufferedWriter(fw);
outFile = new PrintWriter(bw);
public void openCommand(File jonas)
{
dataArray.clear();
try
{
long length = file.length();
System.out.println(length);
FileReader reader = new FileReader(file);
String temp = Long.toString(length);
int size = Integer.parseInt(temp);
char[] cbuf = new char[size];
ArrayList list = new ArrayList();
String numbers = "";
try
{
reader.read(cbuf);
}
catch (IOException e)
{
JOptionPane.showMessageDialog(null,"Can´t read the file", "Error", JOptionPane.ERROR_MESSAGE);
file=null;
}
}
finally
{
System.out.print("oops");
}
}
|
|
|
You had a missing curley brace after the constructor or the "openCommand" method inside of the constructor. Also needed to deal with a FileNotFoundException for the compiler.
import java.awt.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
public class CP extends JPanel
{
static String file;
static FileWriter fw;
static BufferedWriter bw;
static PrintWriter outFile;
JButton start = new JButton ("Start");
JButton stop = new JButton("Stop");;
JButton step = new JButton("Step");
private ArrayList dataArray = new ArrayList();
private Point bestPlace = new Point();
private int scenario = 0;
private HashMap map = new HashMap();
int[] weights;
private JLabel fileName;
public CP() throws IOException
{
file = "/forum/results.txt";
fw = new FileWriter(file);
bw = new BufferedWriter(fw);
outFile = new PrintWriter(bw);
}
public void openCommand(File jonas)
{
dataArray.clear();
try
{
long length = file.length();
System.out.println(length);
FileReader reader = new FileReader(file);
String temp = Long.toString(length);
int size = Integer.parseInt(temp);
char[] cbuf = new char[size];
ArrayList list = new ArrayList();
String numbers = "";
try
{
reader.read(cbuf);
}
catch (IOException e)
{
JOptionPane.showMessageDialog(null,"Can´t read the file", "Error",
JOptionPane.ERROR_MESSAGE);
file=null;
}
}
catch(FileNotFoundException fnfe)
{
System.err.println("file not found: " + fnfe.getMessage());
}
finally
{
System.out.print("oops");
}
}
public static void main(String[] args) throws IOException
{
new CP();
}
}
|
|
|
|
|
|
|
|
|
|