Hey all

I am making a simple text editor, it is my first project and I have got the text area all set up but i have now tried to add a menu bar which will hold the File>Save,Open,exit etc...
I have created the menubar and added one menu item called File, there is nothing in the list now, i am going to do that later but I cant get the menu bar to even appear.
Can someone take a look at my code below and tell me why please. Feel free to point out any other mistakes I have made.

import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;


public class JotPad extends JFrame 
{
	private JTextArea textArea;
		
	public JotPad()
	{
		super("Jot Pad");
		
		//Arrange the Editing Pane
		textArea = new JTextArea(20, 50);
		textArea.setEditable(true);
		setBounds(300, 500, 700, 500);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JScrollPane scrollingText = new JScrollPane(textArea);
		
		//Set all the panel to size and add the components
		JPanel mainPanel = new JPanel();
		mainPanel.setLayout(new BorderLayout());
		mainPanel.add(scrollingText, BorderLayout.CENTER);
		setLocationRelativeTo(null);
		setContentPane(mainPanel);
		mainPanel.setOpaque(true);
		
		// We must set it as visible or else it will run in the background which is bloody useless
		setVisible(true);
		
		//Set all the menu bits
		JMenuBar menu = new JMenuBar();
		JMenu file = new JMenu("File");
		menu.add(file);
	
		
		
		
	}
	
	public static void main(String[] args)
	{
		new JotPad();
		
	}
	
}

Many thanks

HLA91

You may also want to consider using a BorderLayout so that your Menu bar will appear at the top within a JFrame--

import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;


public class JotPad extends JFrame
{
	private JTextArea textArea;

	public JotPad()
	{
		super("Jot Pad");
		setLayout(new BorderLayout());
		//Arrange the Editing Pane
		textArea = new JTextArea(20, 50);
		textArea.setEditable(true);
		setBounds(300, 500, 700, 500);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JScrollPane scrollingText = new JScrollPane(textArea);

		//Set all the panel to size and add the components
		JPanel mainPanel = new JPanel();
		mainPanel.setLayout(new BorderLayout());
		mainPanel.add(scrollingText, BorderLayout.CENTER);
		setLocationRelativeTo(null);
		setContentPane(mainPanel);
		mainPanel.setOpaque(true);

		// We must set it as visible or else it will run in the background which is bloody useless
		setVisible(true);

		//Set all the menu bits
		JMenuBar menu = new JMenuBar();
		JMenu file = new JMenu("File");
		menu.add(file);

		add(menu, BorderLayout.NORTH);


	}

	public static void main(String[] args)
	{
		new JotPad();

	}

}

i believe that with setJMenuBar it doesn't matter what layout manager you use, it will always be at the top, but i may be wrong

I didn't pay close attention to the first post. Apparently JFrame has an initial Layout of BorderLayout.

The OP added the TextArea to the Center Border. By simply adding the JMenuBar, I suppose it will have a default add to either the center of the next available border, or in a random location though I can't confirm which.

All he has to do is add the JMenuBar to the North to ensure that the JMenuBar will be visible and alligned over the JTextArea

Thankyou guys, i added

setJMenuBar(menu);

and the menu bar appeared but I will keep Alex Edwards comments in mind and try them out soon.

Thankyou all

HLA91

This question has already been answered. Start a new discussion instead.