Results 1 to 14 of 14
  1. #1
    Indegon is offline Member
    Join Date
    Nov 2010
    Location
    My own world
    Posts
    15
    Rep Power
    0

    Default Using Scanner class in GUI

    Is there a way to implement the Scanner class into a GUI class? Or if not is there a GUI equivalent? I am trying to use a scanner class in combination with a file reader to read in a file to an array but it's just not working. everything except for the scanner declarations work fine. but the scanners throw a wierd java.io.FileNotFound exception. does anyone have any ideas on this?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Indegon View Post
    Is there a way to implement the Scanner class into a GUI class? Or if not is there a GUI equivalent? I am trying to use a scanner class in combination with a file reader to read in a file to an array but it's just not working. everything except for the scanner declarations work fine. but the scanners throw a wierd java.io.FileNotFound exception. does anyone have any ideas on this?
    You've got a but in your code, but of course without seeing your code, what it is is anybody's guess. If you still need help, then you may wish to show this code, show the actual error, and indicate in the code which line is causing the error.

  3. #3
    Indegon is offline Member
    Join Date
    Nov 2010
    Location
    My own world
    Posts
    15
    Rep Power
    0

    Default

    Java Code:
    package projectb;
    import javax.swing.*;
    import java.io.File;
    import java.io.FileReader;
    import java.util.Scanner;
    import java.util.Arrays;
    import java.awt.*;
    import java.awt.event.*;
    /********************************************************
     ********************************************************
     **  Pokedex V1.0 Pokemon 1 - 151 original.            **
     **  Completed on: 11/02/2010                          **
     **  Info: This program acts as a rudimentary pokedex  **
     **  including basic search and sort functions, more   **
     **  be added later.                                   **
     ********************************************************
     *******************************************************/
    public class Main {
    
        public static class mainMenu extends JFrame
        {
               
            // Array data types declared here
            String[][] dexData = new String[152][3];
            String[] sortName = new String[151];
            String[][] dexData2 = new String[152][3];
            
            // Primitive data types declared here.
            int searchType = 0;
            int sortType = 0;
            int searchNum = 0;
            boolean mainLoop = false;
            String myStop = "";
            String myName = ""; // This is used to search pokedex by name
            String myNumber = ""; // This is used to search pokedex by number.
            String compareName = ""; // This is used to compare sortName to dexData
            int countNum = 0; // this is used to keep track of index number
            
            // counter variables for loops declared here
            int counter = 0;
            int counter1 = 0;
            int counter2 = 0;
            int counter3 = 0;
            int counter4 = 0;
            int counter5 = 0;
            int counter6 = 0;
            int counter7 = 0;
         
    // Possibly these lines causing the error
            // File instances created here. 
            File pNum = new File("pokeNum.txt");
            File pName = new File("pokeName.txt");
            File pInfo = new File("pokeInfo.txt");
    
            // Scanner instances created here
    
    // The lines below here are throwing errors
            Scanner nums = new Scanner(pNum);
            Scanner names = new Scanner(pName);
            Scanner infos = new Scanner(pInfo);
    // The lines above here are throwing errors
    
            /* Need to add in user prompt for files here eventually */
            
            // dexData array filled here using for loop
                JPanel frame = new JPanel();
                    ImageIcon myImg = new ImageIcon("images/152.jpg");
                    ImageIcon spaceImg = new ImageIcon("spacer.JPG");
                    JPanel centerImage = new JPanel();
                         JLabel imgImage = new JLabel(myImg);
                         JPanel spacer1 = new JPanel();
                                JLabel space1 = new JLabel(spaceImg);
                                JLabel space2 = new JLabel(spaceImg);
                                JLabel space3 = new JLabel(spaceImg);
                                JButton backBtn1 = new JButton("Back To Main");
                    JPanel main = new JPanel();
                        JButton sbName = new JButton("Search By Name");
                        JButton sbNum = new JButton("Search By Number");
                        JButton soName = new JButton("Sort By Name");
                        JButton soNum = new JButton("Sort By Num");
                    JPanel search = new JPanel();
                        JPanel srcPnl = new JPanel();
                            JTextField fldSearch = new JTextField(25);
                            JButton btnSearch = new JButton("Search");
                        JPanel srcNum = new JPanel();
                            JLabel lblNum = new JLabel("Number: ");
                            JTextField fldNum = new JTextField(3);
                        JPanel srcName = new JPanel();
                            JLabel lblName = new JLabel("Name: ");
                            JTextField fldName = new JTextField(20);
                        JPanel srcInfo = new JPanel();
                            JLabel lblInfo = new JLabel("Info: ");
                            JTextArea txtInfo = new JTextArea(3,30);
                    JPanel sort = new JPanel();
                            JTextArea txtList = new JTextArea(5,20);
                            JScrollPane comboBox = new JScrollPane(txtList);
                            JPanel spacer2 = new JPanel();
                            JPanel spacer3 = new JPanel();
                                JLabel space4 = new JLabel(spaceImg);
                                JLabel space5 = new JLabel(spaceImg);
                                JLabel space6 = new JLabel(spaceImg);
                                JLabel space7 = new JLabel(spaceImg);
                                JLabel space8 = new JLabel(spaceImg);
                                JLabel space9 = new JLabel(spaceImg);
                                JButton backBtn2 = new JButton ("Back To Main");
                public mainMenu()
                {   
                    while(counter<152)
                    {
                        dexData[counter][0]=nums.nextLine();
                        dexData[counter][1]=names.nextLine();
                        dexData[counter][2]=infos.nextLine();
                        counter++;
                    }
                    frame.setLayout(new BorderLayout(5,5));
                          frame.add(centerImage,BorderLayout.CENTER);
                                centerImage.setLayout(new GridLayout(2,1,5,5));
                                            centerImage.add(imgImage);
                                            centerImage.add(spacer1);
                                                        spacer1.setLayout(new BorderLayout(100,110));
                                                                spacer1.add(space1, BorderLayout.SOUTH);
                                                                spacer1.add(space2, BorderLayout.WEST);
                                                                spacer1.add(space3, BorderLayout.EAST);
                                                                spacer1.add(backBtn1,BorderLayout.CENTER);
                                                                        backBtn1.addActionListener(new ActionListener()
                                                                        {
                                                                            public void actionPerformed(ActionEvent e)
                                                                            {
                                                                                main.setVisible(true);
                                                                                search.setVisible(false);
                                                                                centerImage.setVisible(false);
                                                                            }
                                                                        });
                                            centerImage.setVisible(false);
                          frame.add(main,BorderLayout.NORTH);
                                main.setLayout(new GridLayout(2,2,5,5));
                                main.setVisible(true);
                                     main.add(sbName);
                                          sbName.addActionListener(new ActionListener()
                                          {
                                              public void actionPerformed(ActionEvent e)
                                              {
                                                  main.setVisible(false);
                                                  search.setVisible(true);
                                                  centerImage.setVisible(true);
                                                  searchType=1;
                                              }
                                          });
                                     main.add(sbNum);
                                          sbNum.addActionListener(new ActionListener()
                                          {
                                              public void actionPerformed(ActionEvent e)
                                              {
                                                  main.setVisible(false);
                                                  search.setVisible(true);
                                                  centerImage.setVisible(true);
                                                  searchType=2;
                                              }
                                          });
                                                  
                                     main.add(soName);
                                          soName.addActionListener(new ActionListener()
                                          {
                                              public void actionPerformed(ActionEvent e)
                                              {
                                                  main.setVisible(false);
                                                  sort.setVisible(true);
                                                  sortType = 1;
                                              }
                                          });
                                     main.add(soNum);
                                          soNum.addActionListener(new ActionListener()
                                          {
                                              public void actionPerformed(ActionEvent e)
                                              {
                                                  main.setVisible(false);
                                                  sort.setVisible(true);
                                                  sortType = 2;
                                              }
                                          });
                                                
                          frame.add(search,BorderLayout.SOUTH);
                                search.setLayout(new GridLayout(4,1,0,0));
                                search.setVisible(false);
                                       search.add(srcPnl);
                                       srcPnl.setLayout(new FlowLayout(FlowLayout.LEFT,5,5));
                                              srcPnl.add(fldSearch);
                                                     fldSearch.setEditable(true);
                                              srcPnl.add(btnSearch);
                                                     btnSearch.addActionListener(new ActionListener() 
                                                     {
                                                         public void actionPerformed(ActionEvent e)
                                                         {
                                                             // use imgImage.setIcon(pokeImg(int Count)); here
                                                         }
                                                     });
                                                             
                                       search.add(srcNum);
                                       srcNum.setLayout(new FlowLayout(FlowLayout.LEFT,5,5));
                                              srcNum.add(lblNum);
                                              srcNum.add(fldNum);
                                                     fldNum.setEditable(false);
                                       search.add(srcName);
                                       srcName.setLayout(new FlowLayout(FlowLayout.LEFT,5,5));
                                              srcName.add(lblName);
                                              srcName.add(fldName);
                                                      fldName.setEditable(false);
                                       search.add(srcInfo);
                                       srcInfo.setLayout(new FlowLayout(FlowLayout.LEFT,5,5));
                                              srcInfo.add(lblInfo);
                                              srcInfo.add(txtInfo);
                                                      txtInfo.setEditable(false);
                                                      txtInfo.setLineWrap(true);
                           frame.add(sort, BorderLayout.EAST);
                                 sort.setLayout(new GridLayout(2,1,5,5));
                                 sort.setVisible(false);
                                 sort.add(spacer3);
                                 spacer3.setLayout(new BorderLayout(5,5));
                                      spacer3.add(comboBox, BorderLayout.CENTER);
                                      spacer3.add(space7,BorderLayout.NORTH);
                                      spacer3.add(space8,BorderLayout.EAST);
                                      spacer3.add(space9,BorderLayout.WEST);
                                 sort.add(spacer2);
                                      spacer2.setLayout(new BorderLayout(5,200));
                                              spacer2.add(backBtn2,BorderLayout.CENTER);
                                                      backBtn2.addActionListener(new ActionListener()
                                                      {
                                                          public void actionPerformed(ActionEvent e)
                                                          {
                                                              sort.setVisible(false);
                                                              main.setVisible(true);
                                                          }
                                                      });
                                              spacer2.add(space4,BorderLayout.SOUTH);
                                              spacer2.add(space5,BorderLayout.EAST);
                                              spacer2.add(space6,BorderLayout.WEST);
                      add(frame);
                }  
            }
        public static void main(String[] args) throws Exception
        {
            JFrame pokedex = new mainMenu();
            pokedex.setTitle("Pokedex");
            pokedex.setLocation(325,100);
            pokedex.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            pokedex.setSize(400,600);
            pokedex.setResizable(false);
            pokedex.setVisible(true);
        }
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Please also post your error messages as requested in the first post.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You've got an unhandled exception -- nothing to do with this being a GUI. What you need to do is either throw the exception or enclose the code that throws the exception in a try/catch block. If this term confuses you, then please have a look here: Exceptions

    Again, if you run into similar problems, please post the error message. OK?

  6. #6
    Indegon is offline Member
    Join Date
    Nov 2010
    Location
    My own world
    Posts
    15
    Rep Power
    0

    Default

    sorry, the error for all three is the same. "Unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown"

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Indegon View Post
    sorry, the error for all three is the same. "Unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown"
    See my post above.

  8. #8
    Indegon is offline Member
    Join Date
    Nov 2010
    Location
    My own world
    Posts
    15
    Rep Power
    0

    Default

    noted, and thank you. I will try to post as much information as possible initially in the future.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Indegon View Post
    noted, and thank you. I will try to post as much information as possible initially in the future.
    Great and thanks! Please let us know if you do/don't fix your error. :)

  10. #10
    Indegon is offline Member
    Join Date
    Nov 2010
    Location
    My own world
    Posts
    15
    Rep Power
    0

    Default

    I got it :) it was just like you said, all I had to do is add throws Exception after mainMenu() and it worked. Thanks so much for all your help over the last two days.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I've heard that one should try to be as specific as possible with your exceptions and this means avoid throwing Exception, and instead throwing or catching specific exceptions. In your situation what would you want to have happen if the files above are not found? If I were your user, I'd love to see an understandable error message and then see a window or something that would allow me to possibly correct the mistake, say a JOptionPane that allowed me to enter correct paths to the files. If that weren't possible, then I'd like to see an understandable error message, and the program should gracefully exit. Both these can be accomplished by using a try/catch block.

    Luck!

  12. #12
    Indegon is offline Member
    Join Date
    Nov 2010
    Location
    My own world
    Posts
    15
    Rep Power
    0

    Default

    I agree with you, so I changed throws Exception to throws AWTException i tried IOException but it didn't like that. I'm guessing you have to use AWT for all GUI components?

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Indegon View Post
    I agree with you, so I changed throws Exception to throws AWTException i tried IOException but it didn't like that. I'm guessing you have to use AWT for all GUI components?
    Hm, not sure what you meant. The error states that you have an unhandled FileNotFoundException, so that should work. But regardless, I was suggesting something more on these lines:

    Java Code:
       public static class mainMenu extends JFrame {
    
          //.........
    
          Scanner nums = null;
          Scanner names = null;
          Scanner infos = null;
    
    
          //.........
          
          JButton backBtn2 = new JButton("Back To Main");
    
          public mainMenu() {
             
             try {
                nums = new Scanner(pNum);
                names = new Scanner(pName);
                infos = new Scanner(pInfo);
             } catch (FileNotFoundException e1) {
                // e1.printStackTrace();
                // TODO: 
                // either get other file paths from user or give a nice error message 
                // and exit gracefully.  
             }
    
             
             while (counter < 152) {
                dexData[counter][0] = nums.nextLine();
                dexData[counter][1] = names.nextLine();
                dexData[counter][2] = infos.nextLine();
                counter++;
             }
             // .... etc.....
    Also, you may wish to avoid using a static inner class, but rather place the class in its own file.

  14. #14
    Indegon is offline Member
    Join Date
    Nov 2010
    Location
    My own world
    Posts
    15
    Rep Power
    0

    Default

    oh ok, I see what you mean now. You're right that does look better. Thank you so much once again :)

Similar Threads

  1. Using the scanner class
    By danielwestjr in forum New To Java
    Replies: 1
    Last Post: 03-13-2009, 10:49 AM
  2. Using Scanner class to read int value
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 02-07-2009, 02:47 AM
  3. Scanner Class...
    By TheRocket in forum New To Java
    Replies: 5
    Last Post: 12-05-2008, 09:48 AM
  4. Scanner class
    By ajaymenon.k in forum Advanced Java
    Replies: 1
    Last Post: 11-26-2007, 07:01 AM
  5. JDK 5.0 Scanner Class
    By Sircedric88 in forum New To Java
    Replies: 3
    Last Post: 07-27-2007, 06:55 PM

Posting Permissions

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