3

I have recently started making a minesweeper-like game in java with swing (using eclipse WindowBuilder), and in the app there is an ordinary JFrame with a JMenu with options in it. It works as it should until window is resized. Then, when I click on the menu to show it, it is shown only until I release the mouse button, and then it dissapears as if I clicked soewhere else. But when I access the menu by keyboard shortcut, it works properly. Does anyone know where might the problem be? Code of menu:

    JMenu mnHelp = new JMenu( "Help" );
    mnHelp.setMnemonic( 'h' );
    menuBar.add( mnHelp );

    JMenuItem mntmControls = new JMenuItem( "Controls" );
    mntmControls.setMnemonic( 'c' );
    mntmControls.setIcon( new ImageIcon( Main.class
            .getResource( "/org/img/menu-help.png" ) ) );
    mnHelp.add( mntmControls );

    JSeparator separator_1 = new JSeparator();
    mnHelp.add( separator_1 );

    JMenuItem mntmAbout = new JMenuItem( "About" );
    mntmAbout.setMnemonic( 'g' );
    mntmAbout.setIcon( new ImageIcon( Main.class
            .getResource( "/org/img/menu-about.png" ) ) );
    mnHelp.add( mntmAbout );

EDIT:

I made some experiments, and it turned out it's not a problem caused by something in my program, but in java or eclipse instead, because even the simplest program like the one whose code I'll paste here has the same problem: `

package testpack;

import java.awt.Dimension;
import javax.swing.*;

public class Main {

    public static void main( String[] args ) {
        JFrame jfr = new JFrame( "" );
        jfr.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        JMenuBar jmb = new JMenuBar();
        JMenu jm = new JMenu( "Hello" );
        JMenuItem jmi = new JMenuItem( "Hi" );
        jm.add( jmi );
        jmb.add( jm );
        jfr.setJMenuBar( jmb );
        jfr.setMinimumSize( new Dimension( 400, 400 ) );
        jfr.setVisible( true );
    }
}

` ( this is the whole source )

I'm using Mint Cinnamon 13 and Eclipse 4.2. So is there anything I could do to fix the problem?

Zvonimir
  • 339
  • 1
  • 7
  • Can you show the code of the creating and setting of the JFrame? – La bla bla Sep 10 '12 at 18:56
  • 1
    For better help sooner, post an [SSCCE](http://sscce.org/) (as opposed to more 'bits & pieces'). As an aside, will that SSCCE include any AWT components? Will it involve custom rendering? – Andrew Thompson Sep 10 '12 at 19:05
  • 3
    Not related I think, but it's better to put the swing code in the Event Dispatch Thread, like [in this HelloWorldSwing](http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/start/HelloWorldSwingProject/src/start/HelloWorldSwing.java). – Istao Sep 10 '12 at 20:14
  • Yes, I know, I did that in the original program. – Zvonimir Sep 10 '12 at 20:19
  • 1
    Your edited code runs fine on my Mac; try rebuilding your project. Use `jfr.pack()` instead of [`setMinimumSize()`](http://stackoverflow.com/q/7229226/230513), and follow Istao's advice – trashgod Sep 10 '12 at 20:23
  • 1
    Runs fine on Debian 6. Did you try to run it from the command line, i.e. without Eclipse? – tobias_k Sep 10 '12 at 20:31
  • No problems seen on Windows/Java 7 & Eclipse (1.4.1?). – Andrew Thompson Sep 11 '12 at 00:41

1 Answers1

0

Unfortunately, it's an java issue with Cinnamon. You can't avoid it unless you

  • switch to SWT (see Eclipse itself, which works fine),
  • switch to another os (they don't run Cinnamon),
  • switch to another window manager.
  • It may be, although not likely, that another JVM doesn't have this issue (f.i. another OpenJDK or even Oracle JVM or alike).

The point is (afaik) that Java is supposed to distinguish the plenty window managers running in linux but recognizes just some of them - not Cinnamon in this particular issue.

Even more sad: the problem seems to occur with Mate as well.

9oclock
  • 16