advertisement

Article:
  Working with Sheets in Cocoa
Subject:   Java Version - Solution
Date:   2002-06-02 19:53:46
From:   jsumnertx
Here's the Java solution to this example. I've only included the code that has changed since the last exercise.


Notes:
1) Creating a selector is different in Java.
2) NSApp variable doesn't exist but it's easy enough to derive...
-----


public void deleteRecord(Object sender) { /* IBAction */
String title = "Warning";
String defaultButton = "Delete";
String alternateButton = "Don't Delete";
String otherButton = null;
String message = "Are you sure you want to delete the selected record(s)?";

// here's how to make a selector in Java
NSSelector sel = new NSSelector("sheetDidEnd", new Class[] {NSWindow.class, int.class, Object.class} );


if (tableView.numberOfSelectedRows() == 0)
return;
NSApplication.beep();
NSAlertPanel.beginAlertSheet(title, defaultButton , alternateButton, otherButton, mainWindow, this, sel, null, null, message);


}


public void sheetDidEnd(NSWindow sheet, int returnCode, Object contextInfo)
{
if ( returnCode == NSAlertPanel.DefaultReturn ) {
NSMutableArray tempArray = new NSMutableArray();
NSEnumerator enumerator = tableView.selectedRowEnumerator();
Integer item;
item = (Integer)enumerator.nextElement();
while ( item != null ) {
tempArray.addObject(records.objectAtIndex(item.intValue()));
item = (Integer)enumerator.nextElement();
}
records.removeObjectsInArray(tempArray);
tableView.reloadData();
saveData(records);
}


}


public void openPrefsSheet(Object sender)
{
// NSApp variable doesn't appear to exist in Java. We can get it easily though
NSApplication nsapp=NSApplication.sharedApplication();
nsapp.beginSheet(prefsWindow, mainWindow, null, null, null);
nsapp.runModalForWindow(prefsWindow);
nsapp.endSheet(prefsWindow);
prefsWindow.orderOut(this);
}

public void closePrefsSheet(Object sender)
{
NSApplication.sharedApplication().stopModal();
}



1 to 1 of 1
  1. Java Version - Solution
    2004-04-25 19:14:10  raydreams [View]

1 to 1 of 1