Vitor Costa Vitor Costa - 28 days ago 22
Java Question

Open JInternalFrame with JMenuItem

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.

I am new to Swing API so would love if you could make it simples :D !

Thanks for the patience.

Answer

JMenuItem objects can have action listeners:

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

    @Override
    public void actionPerformed(ActionEvent e) {
        neworder_jif.setVisible(true);
    }
});
Comments