Runtime Error help needed

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2006
Posts: 18
Reputation: korbynlehr is an unknown quantity at this point 
Solved Threads: 0
korbynlehr's Avatar
korbynlehr korbynlehr is offline Offline
Newbie Poster

Runtime Error help needed

 
0
  #1
Jul 30th, 2006
I have some code that I need help with. The code compiles but I get a runtime error. Here is the error and the code:


ERROR:

Exception in thread "main" java.lang.Error: Do not use MessageLog.setLayout() us
e MessageLog.getContentPane().setLayout() instead
at javax.swing.JFrame.createRootPaneException(JFrame.java:465)
at javax.swing.JFrame.setLayout(JFrame.java:531)
at MessageLog.<init>(MessageLog.java:23)
at MessageLog.main(MessageLog.java:90)
Press any key to continue . . .


  1.  
  2. import java.io.*;
  3. import java.awt.*;
  4. import javax.swing.*;
  5. import java.awt.event.*;
  6. public class MessageLog extends JFrame implements ActionListener
  7. {
  8. JLabel lblMessage = new JLabel();
  9. JTextField txtMessage = new JTextField();
  10. JLabel lblTitle = new JLabel();
  11. JTextField txtTitle = new JTextField();
  12. JButton btnSave = new JButton();
  13. JButton btnReset = new JButton();
  14. JLabel lblHeader = new JLabel();
  15. String fileName = "messages.log";
  16. PrintWriter writer;
  17. //Constructor
  18. //it will create and place the controls on frame
  19. public MessageLog()
  20. {
  21. this.setLayout(null);
  22. lblMessage.setText("Message");
  23. lblMessage.setBounds(new Rectangle(54, 99, 55, 16));
  24. txtMessage.setText("");
  25. txtMessage.setBounds(new Rectangle(116, 98, 169, 20));
  26. txtTitle.setBounds(new Rectangle(116, 74, 169, 21));
  27. lblTitle.setText("Title");
  28. lblTitle.setBounds(new Rectangle(53, 74, 54, 16));
  29. btnSave.setBounds(new Rectangle(179, 141, 65, 22));
  30. btnSave.setText("Save");
  31. btnSave.addActionListener(this);
  32. btnReset.setBounds(new Rectangle(108, 141, 69, 22));
  33. btnReset.setText("Reset");
  34. btnReset.addActionListener(this);
  35. this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  36. lblHeader.setFont(new java.awt.Font("Verdana", Font.BOLD, 22));
  37. this.add(txtTitle);
  38. lblHeader.setText("Message Log");
  39. lblHeader.setBounds(new Rectangle(85, 10, 172, 46));
  40. this.add(txtMessage);
  41. this.add(lblTitle);
  42. this.add(lblMessage, null);
  43. this.add(btnReset);
  44. this.add(btnSave);
  45. this.add(lblHeader);
  46. this.setSize(340,220);
  47. this.setTitle("Message Log");
  48. this.setVisible(true);
  49. this.setLocation(400,300);
  50. }
  51. //To handle the action event of the buttons
  52. public void actionPerformed(ActionEvent actionEvent)
  53. {
  54. if(actionEvent.getSource() == btnReset)
  55. {
  56. txtTitle.setText("");
  57. txtMessage.setText("");
  58. }
  59. else if(actionEvent.getSource() == btnSave)
  60. {
  61. try
  62. {
  63. File file = new File(fileName);
  64. //Check if file exists
  65. if(file.exists())
  66. {
  67. //append the message at the end of file
  68. writer = new PrintWriter(new FileWriter(fileName, true));
  69. writer.println(txtTitle.getText() + "," + txtMessage.getText());
  70. writer.close();
  71. JOptionPane.showMessageDialog(null,"Message logged to file.");
  72. }
  73. else
  74. {
  75. //create new file and save message (if file not exist)
  76. writer = new PrintWriter(new FileWriter(fileName));
  77. writer.println(txtTitle.getText() + "," + txtMessage.getText());
  78. writer.close();
  79. JOptionPane.showMessageDialog(null,"Message logged to file.");
  80. }
  81. }catch(Exception e){}
  82. }
  83. }
  84. public static void main (String[] args)
  85. {
  86. MessageLog mLog = new MessageLog();
  87. }
  88. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 216
Reputation: hooknc is an unknown quantity at this point 
Solved Threads: 8
hooknc hooknc is offline Offline
Posting Whiz in Training

Re: Runtime Error help needed

 
0
  #2
Jul 30th, 2006
this.setLayout(null);

needs to be changed to this.getContentPane().setLayout(null);

It says that in the error code btw.

Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 18
Reputation: korbynlehr is an unknown quantity at this point 
Solved Threads: 0
korbynlehr's Avatar
korbynlehr korbynlehr is offline Offline
Newbie Poster

Re: Runtime Error help needed

 
0
  #3
Jul 30th, 2006
Actually I changed that but then I get another runtime error as follows and cannot see where it is asking me to make the change:

ERROR:
Exception in thread "main" java.lang.Error: Do not use MessageLog.add() use Mess
ageLog.getContentPane().add() instead
at javax.swing.JFrame.createRootPaneException(JFrame.java:465)
at javax.swing.JFrame.addImpl(JFrame.java:491)
at java.awt.Container.add(Container.java:307)
at MessageLog.<init>(MessageLog.java:39)
at MessageLog.main(MessageLog.java:90)
Press any key to continue . . .
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 216
Reputation: hooknc is an unknown quantity at this point 
Solved Threads: 8
hooknc hooknc is offline Offline
Posting Whiz in Training

Re: Runtime Error help needed

 
0
  #4
Jul 31st, 2006
this.add(txtTitle);
this.add(txtMessage);
this.add(lblTitle);
this.add(lblMessage, null);
this.add(btnReset);
this.add(btnSave);
this.add(lblHeader);

All these need to be changed to:
this.getContentPane().add(xxx);
Last edited by hooknc; Jul 31st, 2006 at 4:03 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC