943,888 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2074
  • Java RSS
Jul 30th, 2006
0

Runtime Error help needed

Expand Post »
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 . . .


Java Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
korbynlehr is offline Offline
18 posts
since May 2006
Jul 30th, 2006
0

Re: Runtime Error help needed

this.setLayout(null);

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

It says that in the error code btw.

Reputation Points: 11
Solved Threads: 8
Posting Whiz in Training
hooknc is offline Offline
216 posts
since Aug 2005
Jul 30th, 2006
0

Re: Runtime Error help needed

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 . . .
Reputation Points: 10
Solved Threads: 0
Newbie Poster
korbynlehr is offline Offline
18 posts
since May 2006
Jul 31st, 2006
0

Re: Runtime Error help needed

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.
Reputation Points: 11
Solved Threads: 8
Posting Whiz in Training
hooknc is offline Offline
216 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Uploading
Next Thread in Java Forum Timeline: Registering a data source with Windows ODBC manager





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC