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 . . .

import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MessageLog extends JFrame implements ActionListener
{
    JLabel lblMessage = new JLabel();
    JTextField txtMessage = new JTextField();
    JLabel lblTitle = new JLabel();
    JTextField txtTitle = new JTextField();
    JButton btnSave = new JButton();
    JButton btnReset = new JButton();
    JLabel lblHeader = new JLabel();
    String fileName = "messages.log";
    PrintWriter writer;
    //Constructor
    //it will create and place the controls on frame
    public MessageLog()
    {
  this.setLayout(null);
  lblMessage.setText("Message");
  lblMessage.setBounds(new Rectangle(54, 99, 55, 16));
  txtMessage.setText("");
  txtMessage.setBounds(new Rectangle(116, 98, 169, 20));
  txtTitle.setBounds(new Rectangle(116, 74, 169, 21));
  lblTitle.setText("Title");
  lblTitle.setBounds(new Rectangle(53, 74, 54, 16));
  btnSave.setBounds(new Rectangle(179, 141, 65, 22));
  btnSave.setText("Save");
  btnSave.addActionListener(this);
  btnReset.setBounds(new Rectangle(108, 141, 69, 22));
  btnReset.setText("Reset");
  btnReset.addActionListener(this);
  this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  lblHeader.setFont(new java.awt.Font("Verdana", Font.BOLD, 22));
  this.add(txtTitle);
  lblHeader.setText("Message Log");
  lblHeader.setBounds(new Rectangle(85, 10, 172, 46));
  this.add(txtMessage);
  this.add(lblTitle);
  this.add(lblMessage, null);
  this.add(btnReset);
  this.add(btnSave);
  this.add(lblHeader);
  this.setSize(340,220);
  this.setTitle("Message Log");
  this.setVisible(true);
  this.setLocation(400,300);
    }
    //To handle the action event of the buttons
    public void actionPerformed(ActionEvent actionEvent)
    {
        if(actionEvent.getSource() == btnReset)
        {
   txtTitle.setText("");
   txtMessage.setText("");
        }
        else if(actionEvent.getSource() == btnSave)
        {
   try
   {
    File file = new File(fileName);
    //Check if file exists
    if(file.exists())
    {
     //append the message at the end of file
     writer  = new PrintWriter(new FileWriter(fileName, true));
     writer.println(txtTitle.getText() + "," + txtMessage.getText());
     writer.close();
     JOptionPane.showMessageDialog(null,"Message logged to file.");
    }
    else
    {
     //create new file and save message (if file not exist)
     writer = new PrintWriter(new FileWriter(fileName));
     writer.println(txtTitle.getText() + "," + txtMessage.getText());
     writer.close();
     JOptionPane.showMessageDialog(null,"Message logged to file.");
    }
   }catch(Exception e){}
        }
    }
    public static void main (String[] args)
    {
        MessageLog mLog = new MessageLog();
    }
}

this.setLayout(null);

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

It says that in the error code btw.

:)

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 . . .

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);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.