Question

I am trying to write a multiple document interface programme drawing application. Each instance of a drawing application is displayed in a JInternalFrame obj within a JDesktopane. The user can create multiple child window drawing apps via 'File-New Window ' (a JMenu ).

For every internal frame created a corresponding descriptive JMenuitem is created and added to a JMenu (windows).

The JMenu Window command then displays a list of open windows and allow the user select one window.

This is where I've had a mental block, is there a way one can link the JMenuitem to the window created?

06/07/13 Thanks for the advice. I am adding a internalFrame to a ArrayList every time one is instantiated, then retrieving it from array by manipulating the String title to acquire the frame no (see code below), however the ArrayList size is always 1, and always the last frame to be instantiated....any ideas?

windowList = new ArrayList();
            windowList.add(internalFrame);
            //windowMenuItem = new ArrayList(200);
            windowMenuItem.add(newWindow);

            //picNumber = new int[200];
            //allow suer to select internalFrame according to menuItem selceted
            newDrawFrame.addActionListener(new ActionListener(){

          @Override
          public void actionPerformed(ActionEvent menuEvent) {



                  //call method to handle this event
                  winItemActionPreformed(menuEvent);
                  //frames[openFrameCount].setSelected(true);


          }//end actionPerformed

            }//end annon actionListener class

        );//end method calal to actionListener 

Then the winItemActionPerfomed method...

 //handle menu selction for a internalFrame and set to front/on focus
public void winItemActionPreformed(ActionEvent menuEvent){
    //get selected menuItem via event and casdt to JMenuItem type
    //manipulate returned String object to retrive frame number

    JMenuItem tempItem = (JMenuItem) menuEvent.getSource();
    String tempItemString = tempItem.getText();
    int getFrameNumber = tempItemString.lastIndexOf("#");
    String frameNumberSelected = tempItemString.substring(getFrameNumber+1); 

    //convert String to int representong frameNumber in ArrayList
    int frameNumber;
    try 
    {
        frameNumber = Integer.parseInt(frameNumberSelected);
        JInternalFrame tempFrame = (JInternalFrame) windowList.get(frameNumber-1);
        tempFrame.toFront();
    }//end try
    catch (NumberFormatException ex) {
        System.err.println("No Such Number.");


    }//end catch 




}//end winActionPreformed
Was it helpful?

Solution

I'd take advantage of the Actions API

For example...

public class SelectWindowAction extends AbstractAction {

    private JInternalFrame frame;

    public SelectWindowAction(JInternalFrame frame) {
        putValue(NAME, frame.getTitle());
    }

    public void actionPerformed(ActionEvent evt) {
        frame.setSelected(true);
    }
}

Then you can simple create a JMenuItem using something like...

JMenuItem mi = new JMenuItem(new SelectWindowAction(frame));

OTHER TIPS

Update: Its ok got it, the ArrayList was created within a method so I initialised elsewhere and made it global ....ta for the helps :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top