Results 1 to 4 of 4
Like Tree2Likes
  • 1 Post By doWhile
  • 1 Post By doWhile

Thread: Help with GUI - Java NOOB :(

  1. #1
    bdelarosa7 is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default 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!


    Java 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();
                }
            });
        }
    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: Help with GUI - Java NOOB :(

    You need to catch your exception that is thrown from
    Java Code:
    public void ImmuliteGui(String filename) throws IOException
    Suggested reading:
    Lesson: Exceptions (The Java Tutorials > Essential Classes)
    camickr likes this.

  3. #3
    bdelarosa7 is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default Re: Help with GUI - Java NOOB :(

    I actually do throw the IOException.

    Line 34?? Is that right?

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default 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
    Java 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
    Last edited by doWhile; 11-22-2011 at 09:56 PM.
    Fubarable likes this.

Similar Threads

  1. Noob + Java = Questions;
    By Webstorms in forum New To Java
    Replies: 1
    Last Post: 09-24-2011, 02:59 PM
  2. Databse noob - Java SQL questions
    By Onra in forum New To Java
    Replies: 0
    Last Post: 11-27-2010, 02:47 PM
  3. Help im a noob.. a super noob on java..
    By critdevil in forum New To Java
    Replies: 12
    Last Post: 03-07-2009, 03:17 AM
  4. Hello, Java Noob
    By furry in forum Introductions
    Replies: 0
    Last Post: 10-07-2008, 07:39 PM
  5. Ah! Help a Java Noob
    By Snejana in forum New To Java
    Replies: 4
    Last Post: 01-24-2008, 03:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •