JMenuItems are bad draw

63 Views Asked by At

I have build an UI but when i try to select an option of MenuBar, it's bad drawn.

This is my main Launcher.java

public class Launcher
{
    public static void main(String[] args) throws IOException
    {
        SwingUtilities.invokeLater(new Runnable() 
        {
            public void run()
            {
                new Gui();
            }
        });
    }
}

This is the class that manage UI GUI.java

public class Gui extends JFrame
{
    private JMenuBar menuBar=new JMenuBar();
    private JMenu info=new JMenu("Info");
    private JMenu help=new JMenu("Aiuto");
    private JMenu tool=new JMenu("Strumenti");
    private JMenuItem update=new JMenuItem("Controlla aggiornamenti");
    private JMenuItem resetTable=new JMenuItem("Reset tabella");
    private JMenuItem resetTextArea=new JMenuItem("Reset area di testo");
    private JMenuItem forum=new JMenuItem("Vai al forum");
    private JMenuItem guide=new JMenuItem("Guida");
    private JMenuItem information=new JMenuItem("Informazioni");
    private JMenuItem trouble=new JMenuItem("Segnala un problema");
    private final int GUI_WIDTH=1270;
    private final int GUI_HEIGHT=700;


    /**
     * Costruttore dell'interfaccia principale
     */
    public Gui()
    {
        setMenuBar();
        setTitle("WP 0.2");
        setGUI();
    }

    /**
     * Metodo che setta l'interfaccia grafica principale
     */
    private void setGUI()
    {
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(GUI_WIDTH,GUI_HEIGHT);
        Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
        int width=(int) screenSize.getWidth();
        int height=(int) screenSize.getHeight();
        setLocation((width-GUI_WIDTH)/2,(height-GUI_HEIGHT)/2);
        setResizable(true);
    }

    /**
     * Metodo che aggiunge i vari componenti al menubar
     */
    public void setMenuBar()
    {
        setJMenuBar(menuBar); // Creazione del Menu
        menuBar.add(info);
        menuBar.add(tool);
        menuBar.add(help);

        info.add(update);
        tool.add(resetTable);
        tool.add(resetTextArea);
        help.add(forum);
        help.add(trouble);
    }
}

Example of the mistakes:
Example 1
Example 2

Will be the problem because Swing isn't thread safe?

1

There are 1 best solutions below

1
On

Also, setVisible(true) needs to be after everything. When you set the JFrame visible, it draws the current components. If you resize and move the JFrame after the setVisible, you need to redraw the JFrame. I ussually put setvisible at the end of my constructor.

 private void setGUI()
{
    //setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(GUI_WIDTH,GUI_HEIGHT);
    Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
    int width=(int) screenSize.getWidth();
    int height=(int) screenSize.getHeight();
    setLocation((width-GUI_WIDTH)/2,(height-GUI_HEIGHT)/2);
    setResizable(true);
    setVisible(true);
}