Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 5th, 2008, 9:43 PM   #1
JD-Salinger
Unknown
 
JD-Salinger's Avatar
 
Join Date: Apr 2008
Location: unknown
Posts: 79
Rep Power: 1 JD-Salinger is on a distinguished road
my program is correct but it acts weird(exception handling)

i run my program and it is acting weird even i inputted correct values, i reviewed it and i thnk my code is correct, please check it.. and debug itif you have some time... thanks

here is the data frame class
java Syntax (Toggle Plain Text)
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class DataFrame extends JFrame
  6. {
  7. protected DataPanel panel = new DataPanel();
  8. public static void main(String[] args)
  9. {
  10. DataFrame df = new DataFrame();
  11. df.setVisible(true);
  12. }
  13.  
  14. public DataFrame()
  15. {
  16. super("Enter Data");
  17. buildLayout();
  18. pack();
  19. }
  20.  
  21. protected void buildLayout()
  22. {
  23. Container pane = getContentPane();
  24. pane.setLayout(new BorderLayout());
  25. pane.add(new DataPanel(),BorderLayout.CENTER);
  26. JButton button = new JButton("Ok");
  27. button.addActionListener(new ActionListener()
  28. {
  29. public void actionPerformed(ActionEvent event)
  30. {
  31. onOk();
  32. }
  33. });
  34.  
  35. JPanel panel = new JPanel();
  36. panel.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
  37. panel.add(button);
  38. pane.add(panel,BorderLayout.SOUTH);
  39. }
  40.  
  41. protected void onOk()
  42. {
  43. try
  44. {
  45. panel.validateInput();
  46. System.exit(0);
  47. }
  48. catch(InputValidationException ive)
  49. {
  50. ive.errorSource.requestFocus();
  51. JOptionPane.showMessageDialog(this,ive.getMessage(),
  52. "Validation Error",
  53. JOptionPane.ERROR_MESSAGE);
  54. }
  55. }
  56.  
  57. }

and here is the datapanel class
java Syntax (Toggle Plain Text)
  1. import java.awt.GridLayout;
  2. import javax.swing.*;
  3.  
  4. public class DataPanel extends JPanel
  5. {
  6. protected JTextField nameField;
  7. protected JTextField ageField;
  8.  
  9. public DataPanel()
  10. {
  11. buildDisplay();
  12. }
  13.  
  14. public void validateInput() throws InputValidationException
  15. {
  16. String name = nameField.getText();
  17. if(name.length() == 0)
  18. {
  19. throw new InputValidationException("No name was spefified",nameField);
  20. }
  21. String age=ageField.getText();
  22. try
  23. {
  24. int value=Integer.parseInt(age);
  25. if(value <= 0)
  26. {
  27. throw new InputValidationException("Age value must be " + "a positive integer",ageField);
  28. }
  29. }
  30. catch(NumberFormatException e)
  31. {
  32. throw new InputValidationException("Age value is missing " +
  33. "or invalid",ageField);
  34. }
  35. }
  36. protected void buildDisplay()
  37. {
  38. setLayout(new GridLayout(2,2,10,5));
  39. JLabel label = new JLabel("Name: ");
  40. add(label);
  41. nameField = new JTextField(10);
  42. add(nameField);
  43. label = new JLabel("Age:");
  44. add(label);
  45. ageField=new JTextField(10);
  46. add(ageField);
  47. }
  48. }

and the exception class
java Syntax (Toggle Plain Text)
  1. import java.awt.Component;
  2.  
  3. public class InputValidationException extends Exception
  4. {
  5. protected Component errorSource;
  6.  
  7. public InputValidationException(String message,Component source)
  8. {
  9. super(message);
  10. errorSource = source;
  11. }
  12. }
__________________
-------------------------------------------------------------------------
I thought what I'd Do was, I'd pretend to be one of those deaf mutes
------------------------------------------------------------------------
JD-Salinger is offline   Reply With Quote
Old Jul 6th, 2008, 1:20 AM   #2
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 294
Rep Power: 3 andro is on a distinguished road
Send a message via AIM to andro
Re: my program is correct but it acts weird(exception handling)

protected DataPanel panel = new DataPanel();

pane.add(new DataPanel(),BorderLayout.CENTER);


You aren't actually adding the DataPanel you are validating input on to your DataFrame. Are you using an IDE? Eclipse maybe? It has a debugger you can use to step through your code. You would've spotted the problem pretty quickly.
__________________
http://www.kevinherron.com/
andro is offline   Reply With Quote
Old Jul 6th, 2008, 3:55 AM   #3
JD-Salinger
Unknown
 
JD-Salinger's Avatar
 
Join Date: Apr 2008
Location: unknown
Posts: 79
Rep Power: 1 JD-Salinger is on a distinguished road
Re: my program is correct but it acts weird(exception handling)

but that's what it suppose to do... cause that was in the book... i'm not using eclipse, i am using blueJ, it runs slow in my computer...got 256mb or ram, 80g of harddisk drive, 3 gigahertz of processor speed, it runs slow for me... my program works but the exception handling is wrong... ill check my program tomorow... ill be doing some other exercise for now... thanks anyway...
__________________
-------------------------------------------------------------------------
I thought what I'd Do was, I'd pretend to be one of those deaf mutes
------------------------------------------------------------------------
JD-Salinger is offline   Reply With Quote
Old Jul 6th, 2008, 12:01 PM   #4
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 294
Rep Power: 3 andro is on a distinguished road
Send a message via AIM to andro
Re: my program is correct but it acts weird(exception handling)

I told you exactly how to fix it. You are calling panel.validateInput() on a different DataPanel than the one you add to your DataFrame.
__________________
http://www.kevinherron.com/
andro is offline   Reply With Quote
Old Jul 6th, 2008, 5:50 PM   #5
JD-Salinger
Unknown
 
JD-Salinger's Avatar
 
Join Date: Apr 2008
Location: unknown
Posts: 79
Rep Power: 1 JD-Salinger is on a distinguished road
Re: my program is correct but it acts weird(exception handling)

thanks dude... i haven't figured it out.... im stupid in not understanding that... thanks again
__________________
-------------------------------------------------------------------------
I thought what I'd Do was, I'd pretend to be one of those deaf mutes
------------------------------------------------------------------------
JD-Salinger is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
hello, I'd like to write a program for my work. blake_jl Community Introductions 13 Nov 23rd, 2007 4:31 PM
Sudoku program info & description Adak Software Design and Algorithms 4 Jul 2nd, 2006 12:49 PM
Language display in program Prm753 C++ 3 May 30th, 2006 5:45 PM
Creating a program to test a program sixstringartist C 8 Jan 21st, 2006 1:15 PM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 4:12 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:14 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC