Opening JInternalFrame on JMenu selection

package testing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Testing
{
JDesktopPane desktop = new JDesktopPane();
int counter = 1;
int x = 0, y = 0;
public void buildGUI()
{
JMenu menu = new JMenu(“File”);
JMenuItem addIF = new JMenuItem(“add internal frame”);
addIF.setMnemonic(‘A’);
menu.add(addIF);
JMenuBar menuBar = new JMenuBar();
menuBar.add(menu);
JMenuItem addIF_1 = new JMenuItem(“add internal frame”);
addIF_1.setMnemonic(‘F’);
addIF_1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,0));
menuBar.add(addIF_1);

JFrame f = new JFrame();
f.setJMenuBar(menuBar);
f.getContentPane().add(desktop);
f.setSize(1000,800);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
addIF.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae){
JInternalFrame jif = new JInternalFrame(“#”+counter,true,true,true,true);
jif.setBounds(x,y,1000,800);
jif.setVisible(true);
desktop.add(jif);
counter++;
x += 0;
y += 0;
try{jif.setSelected(true);}catch(Exception e){e.printStackTrace();}
}
});
addIF_1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae){
System.out.println(“OK”);
}
});
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
new Testing().buildGUI();
}
});
}
}

Leave a Reply to assist you..

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s