• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Focusing on one JInternalFrame object using a JMenuItem actionListener

 
Ciaran Mooney
Ranch Hand
Posts: 74
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
K. Tsang
Bartender
Posts: 3404
13
Android Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ciaran Mooney
Ranch Hand
Posts: 74
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'Declared' was supposed to be initialised ....
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic