This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
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

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

Focusing on one JInternalFrame object using a JMenuItem actionListener

Ciaran Mooney
Ranch Hand

Joined: Jan 21, 2013
Posts: 74

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

Joined: Sep 13, 2007
Posts: 2985

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 OCPJP7 OCPWCD5 OCPBCD5 OCPWSD5 OCMJEA5 part 1 part 2/3
Darryl Burke

Joined: May 03, 2008
Posts: 4989

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: 74

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 ideas?
[private ArrayList windowList; //store frames as they are instanitaed

windowList = new ArrayList();
//windowMenuItem = new ArrayList(200);

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

public void actionPerformed(ActionEvent menuEvent) {

//call method to handle this event

}//end actionPerformed

}//end annon actionListener class

);//end method calll to actionListener

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;
frameNumber = Integer.parseInt(frameNumberSelected);
JInternalFrame tempFrame = (JInternalFrame) windowList.get(frameNumber-1);
}//end try
catch (NumberFormatException ex) {
System.err.println("No Such Number.");

}//end catch

}//end winActionPreformed][/code]

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: 74

'Declared' was supposed to be initialised ....
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Focusing on one JInternalFrame object using a JMenuItem actionListener
It's not a secret anymore!