wood burning stoves*
The moose likes Swing / AWT / SWT and the fly likes Focusing on one JInternalFrame object  using a JMenuItem actionListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Focusing on one JInternalFrame object  using a JMenuItem actionListener " Watch "Focusing on one JInternalFrame object  using a JMenuItem actionListener " New topic
Author

Focusing on one JInternalFrame object using a JMenuItem actionListener

Ciaran Mooney
Ranch Hand

Joined: Jan 21, 2013
Posts: 72

Hi,
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?


I love deadlines, I love the whooshing sound they make as they fly by...
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2379
    
    7

Hello there just thinking out loud not sure if this works.

Pass in the JMenu to the JInternalFrame program, then add the JMenuItems and update the JMenu. This way you will need need create a static method to actual get the JMenu instance from the main JFrame before creating your JInternalFrame. Once the JMenu is updated (added in the new JMenuItem), replace it by setting it to the main JFrame.

K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4530
    
    5

Create a class that implements ActionListener (or extends AbstractAction) and accepts a JInternalFrame parameter to its constructor. Add separate instances of this class as actionListeners for each JMenuItem.

I think you can take it from there.


luck, db
There are no new questions, but there may be new answers.
Ciaran Mooney
Ranch Hand

Joined: Jan 21, 2013
Posts: 72

Thanks for the info.
I am adding each internalFrame to a ArrayList as it is instantiated.
Then retrieving the frame via maniukation of the String title. Seems to work OK but the size of the array is 1 of how many internalFrames I add, and its is always the last one to be instantiated....an ideas?
[private ArrayList windowList; //store frames as they are instanitaed

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 calll to actionListener

][/code]
Then the method to retrive internal frame from arrayList and call its toFront method to set 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][/code]


Update:
Its ok got it, the ArrayList was created within a method so I declared it as a private member and made it global ....ta for the helps
Ciaran Mooney
Ranch Hand

Joined: Jan 21, 2013
Posts: 72

'Declared' was supposed to be initialised ....
 
Don't get me started about those stupid light bulbs.
 
subject: Focusing on one JInternalFrame object using a JMenuItem actionListener