Help with GUI - Java NOOB :(
Hey guys,
So I'm trying to create a very simple program that would sort a massive amount of data for us here at work. I have already gotten that aspect of the program to function properly and print to a file whilst reading the data-to-sort from a file as you can see in line 37. I have not included that aspect of the program as it is rather lengthy and not relevant to the error I'm getting, I believe.
The problem arises in that I would like to create a really simple GUI so that a window pops up and I'm able to type in the name of the file in the textfield and simply press enter and have the program print to a specified file. The error I'm getting is:
C:\Users\Gilab\Desktop\ImmuliteGui\src\ImmuliteGui .java:130: error: unreported exception IOException; must be caught or declared to be thrown
ImmuliteGui(text);
^
1 error
Line 130 being 47 in the example below.
I am completely new to creating GUI's so my coding may be all over the place :=(:.
Any help would be appreciated!
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class ImmuliteGui extends JPanel implements ActionListener {
protected JTextField textField;
protected JTextArea textArea;
private final static String newline = "\n";
public ImmuliteGui(){
super(new GridBagLayout());
textField = new JTextField(40);
textField.addActionListener(this);
textArea = new JTextArea(30, 50);
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
add(textField, c);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
add(scrollPane, c);
}
public void ImmuliteGui(String filename) throws IOException
{
PrintWriter out = new PrintWriter(new FileWriter("ImmuliteResults.txt"));
BufferedReader in=new BufferedReader(new FileReader(filename));
int datasize=0;
String titleline=in.readLine();
while(in.readLine()!=null)
datasize++;
in.close();
}
public void actionPerformed(ActionEvent evt){
String text = textField.getText();
ImmuliteGui(text);
textArea.append(text + newline);
textField.selectAll();
textArea.setCaretPosition(textArea.getDocument().getLength());
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("Immulite Lot Tracker");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new ImmuliteGui());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args){
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Re: Help with GUI - Java NOOB :(
You need to catch your exception that is thrown from
Code:
public void ImmuliteGui(String filename) throws IOException
Suggested reading:
Lesson: Exceptions (The Java™ Tutorials > Essential Classes)
Re: Help with GUI - Java NOOB :(
I actually do throw the IOException.
Line 34?? Is that right?
Re: Help with GUI - Java NOOB :(
A thrown exception has to be caught by something...actionPerformed is not declared to throw the exception, so you must catch the exception within that method when you call code which throws that type of exception. The above link explains this all, and it is simple syntax
Code:
try{
//do IO operations here
}catch(IOException io){
//do something, often io.printStackTrace(); is enough to track when something is wrong.
}
Although, I might advise a more advanced approach and catch the exception in the IO method - this allows you to use a finally clause so you can ensure you close the IO stream