Search code examples
javajmenubar

Why I cant see all the elements for my menubar?


Sometimes 2 elements are on my menu, sometimes all but if the elements are not visible I need to resize my window. If I resize my window the elements appear. Why?

This is my code:

//Class TextEditor start
public class TextEditor extends JFrame{

private JMenuBar menuBar;
private JMenu file,edit,format,view,help; 
private JMenuItem newFile;
private JMenuItem exit;

//Main method start
public static void main(String[] args){
    try{
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }catch (Exception e){

    }
    new TextEditor();
}
//Main class end


//Class constructor start
private TextEditor(){
        super("Untitled");
        sendUI(this);
        sendMenuBar();
    }
//Class constructor end

//Menu bar start//
public void sendMenuBar(){
    menuBar = new JMenuBar();
    setJMenuBar(menuBar);

    //File menu and Items
    file = new JMenu(" File ");
    newFile = new JMenuItem("New");
    exit = new JMenuItem("Exit");
    menuBar.add(file);
    file.add(newFile);
    file.add(exit);

    //Edit menu and items
    edit = new JMenu(" Edit ");
    menuBar.add(edit);

    //Format menu and items
    format = new JMenu(" Format ");
    menuBar.add(format);

    //View menu and items
    view = new JMenu(" View ");
    menuBar.add(view);

    //Help menu and items
    help = new JMenu(" Help ");
    menuBar.add(help);
}


private void sendUI(TextEditor texteditor) {
    texteditor.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    texteditor.setSize(700,400);
    texteditor.setLocationRelativeTo(null);
    texteditor.setVisible(true);
}

}
//Class TextEditor end

When the error appears:

enter image description here

After I resized the window:

enter image description here


Solution

  • You need to call senUI after sendMenuBar.