Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swing Execution

Tags:

java

swing

Hi I just wrote a code for a Swing using Menu bar. But the problem arises in running. I type:

javac Menu.java
java Menu

It gives no errors but the GUI does not get displayed. Here's my source code for reference:

import javax.swing.*;
class Menu extends JFrame
{
    JMenuBar mb;
    JMenu m1,m2,m3;
    JMenuItem mn1,mn2,mn3;
    JCheckBoxMenuItem chk1,chk2,chk3;
    JRadioButtonMenuItem rd1,rd2,rd3;
    JSeparator sp1,sp2,sp3;
    Menu()
    {
        setTitle("MenuBar Demo");
        mb = new JMenuBar();
                m1 = new JMenu("File");
                m2 = new JMenu("Edit");
                m3 = new JMenu("View");
                mn1 = new JMenuItem("New");
                mn2 = new JMenuItem("Open");
                mn3 = new JMenuItem("Close");
                chk1 = new JCheckBoxMenuItem("Cut");
                chk2 = new JCheckBoxMenuItem("Copy");
                chk3 = new JCheckBoxMenuItem("Paste");
                rd1 = new JRadioButtonMenuItem("Find");
                rd2 = new JRadioButtonMenuItem("Find Next");
                rd3 = new JRadioButtonMenuItem("Replace");
                sp1 = new JSeparator();   
                sp2 = new JSeparator();   
                sp3 = new JSeparator();   
                m1.add(mn1);
                m1.add(mn2);
                m1.add(sp1);
                m1.add(mn3);
                m2.add(chk1);
                m2.add(chk2);
                m2.add(sp2);
                m2.add(chk3);
                m3.add(rd1);
                m3.add(rd2);
                m3.add(sp3);
                m3.add(rd3);
                mb.add(m1);
                mb.add(m1);
                mb.add(m2);
                mb.add(m3);
                getContentPane().add(mb);
                mb.setSize(300, 300);
                mb.setVisible(true);
    }
        public static void main(String[] args)
        {
            new Menu();
        }

}

Why does the GUI not appear?

like image 999
sri bharath Avatar asked Feb 10 '26 13:02

sri bharath


2 Answers

Use something like:

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            Menu app = new Menu();
            app.setVisible(true);
        }
    });

}

I suggest to rename your class, as this an application/ frame not a Menu.

like image 141
Puce Avatar answered Feb 13 '26 10:02

Puce


At the end of the constructor, call this.setVisible(true);

General tips

  • Don't extend frame, instead keep a reference to one.
  • Don't call setVisible(true) on components, instead add them to a container that will be made visible. The menu bar does not seem to be added to anything. Add it using setJMenuBar(JMenuBar)
  • Don't set the size of components. Use appropriate layouts, padding and borders then pack() the GUI.
  • Use a consistent and logical indent for code blocks. It helps readability.
  • & as Puce mentions, start and alter GUIs on the EDT.

These changes will produce a small GUI on-screen.

// ...
//getContentPane().add(mb);
setJMenuBar(mb);
//mb.setSize(300, 300);
//mb.setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
pack();
setVisible(true);
like image 23
Andrew Thompson Avatar answered Feb 13 '26 10:02

Andrew Thompson