1

I am trying to make a menu item which when I click it makes a JInternalFrame visible. I created the JDesktopPane and added the JInternalFrame to it.

JInternalFrame neworder_jif;

public MainFrame() {
    Login login = new Login(this, true);
    login.setVisible(true);
    initComponents();

    //NEW ORDER JIF
    desk.add(neworder_jif = new NewOrder());
    neworder_jif.pack();
    neworder_jif.setVisible(true);
}

As you can see, with this code the internal frame appears correctly but I want it to start invisible, but when I make

neworder_jif.setVisible(false);

on the constructor and create the action listener to when mouse clicked

private void new_order_menuMouseClicked(java.awt.event.MouseEvent evt) {        
    neworder_jif.setVisible(true);
}

it does not work, I click the menu item and nothing happens.

ANSWER

For those looking for the bug in this code I will explain how I fixed it: changed the event from MouseButtonClicked event created with NetBeans design editor to MouseButtonReleased.

  • Without seeing runnable code, it's anyone's guess what you might be doing wrong. Please consider taking a little time to create and post a [minimal example program](http://stackoverflow.com/help/mcve), a small but complete program that only has necessary code to demonstrate your problem, that we can copy, paste, compile and run without modification, since this would be the best and quickest way to get folks to fully understand your problem and then help you. – Hovercraft Full Of Eels Nov 06 '16 at 14:13
  • Also, you've posted your code here with your question and not in a link -- good for you! If you decide to create and post a valid MCVE, please do the same -- post your code text here with your question and not in a link. – Hovercraft Full Of Eels Nov 06 '16 at 14:14
  • Did you manage to get it working? – ItamarG3 Nov 06 '16 at 14:22
  • @ItamarGreen just did changing from mouse clicked to released, will update answer soon. But no idea the why works now –  Nov 06 '16 at 14:32
  • 1
    @VitorCosta From my experience, a surer way to do it is with the menu item's action listener (as I wrote in my answer) – ItamarG3 Nov 06 '16 at 14:33
  • Victor, @ItamarGreen is correct -- use the right tool for the job. For instance, if you use mouse listeners on menu items or on buttons, then both will still function even if you *disable* the menu item or button, and this is not good program behavior. 1+ to Itmar's answer. – Hovercraft Full Of Eels Nov 06 '16 at 14:40

2 Answers2

2

JMenuItem objects can have action listeners:

JMenuItem item = new JMenuItem();
item.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        neworder_jif.setVisible(true);
    }
});
ItamarG3
  • 4,092
  • 6
  • 31
  • 44
0

You can also try this to use only one JFrameForm for multiple InternalFrames, using the JMenuItem. This works for me:

private void jMenuItem8ActionPerformed(java.awt.event.ActionEvent evt) {                                           
    // Sample Mean: 
    SampleMean inter = new SampleMean(); // You have to change SampleMean to the name of your InternalFrame 
    jDesktopPane1.add(inter); // Name of your JDesktopPane
    inter.show();
}      

Also do not forget that you have to use events -> Action -> actionPerform

Robert
  • 7,394
  • 40
  • 45
  • 64