I am trying to catch SWT events, like SWT.activate, SWT.deactivate and SWT.dispose in Eclipse. So, I can see which dialog was opened or activated, which was closed and which was deactivated. If the event was caught, I extract the Shell object and extracts its title with shell.getText(). To listen to events, I used an untyped listener (edited):
PlatformUI.getWorkbench().getDisplay().addFilter(SWT.Activate, shellListener);
Listener shellListener = new Listener(){
#Override public void handleEvent(Event e) {
if (event.widget.getClass() == org.eclipse.swt.widgets.Shell.class){
Shell shell = (Shell) e.widget;
String shellTitle = shell.getText();
if (event.type == Activate) {
String message = "The following dialog was activated: " + shellTitle;
// do other stuff with 'message'
}
}
}
};
If in Eclipse I open 'New' and the listener above correctly displays 'New' as activated dialog. But if I select 'Java Interface' within the 'New' dialog, then I am landing in a dialog, called 'New Java Interface'. But my handleEvent method is not fired and therefore I cannot extract the new dialog title. My question is: What kind of event is called or what happens, when I am in an Eclipse dialog and clicking on something in it which leads me to another dialog (with a new title)?
I think the problem here comes from the fact that the New 'dialog' in Eclipse is actually a Wizard. When you select 'Java Interface' (in the 'New' dialog) you are actually then landing not in another dialog but on a page within the same wizard. Every page in this wizard can have it's own title but behind the scene it is the same underlying shell object, that is why you don't receive further events.
By the way: when working with the SWT.Activate, SWT.Deactivate and other similar shell events, it might be helpflul / easier to the a ShellAdapter
Related
I'm looking to create a toolbar with a drop-down menu largely identical to Eclipse's "Open Console" button in the console view.
I can do this by programmatically adding actions (which is what the Console View does), however I'm looking to do this via commands & handlers in plugin.xml.
The instructions at https://wiki.eclipse.org/Menu_Contributions/Dropdown_Command are extremely good, however I'm looking to show the drop-down menu when the button itself is clicked (I.E.: clicking the button behaves the same as clicking the drop-down menu triangle to the right of the button). Again: identical to how the "Open Console" button works.
However I've no clue how to do this in "plugin.xml land". I've tried the following in the handler:
#Override
public Object execute(ExecutionEvent event) throws ExecutionException {
Event e = (Event) event.getTrigger();
ToolBar t = ((ToolItem) e.widget).getParent();
Menu m = t.getMenu();
m.setVisible(true);
[...]
... however t.getMenu() returns a null ...
I want to save a file before closing my JavaFX application.
This is how I'm setting up the handler in Main::start:
primaryStage.setOnCloseRequest(event -> {
System.out.println("Stage is closing");
// Save file
});
And the controller calling Stage::close when a button is pressed:
#FXML
public void exitApplication(ActionEvent event) {
((Stage)rootPane.getScene().getWindow()).close();
}
If I close the window clicking the red X on the window border (the normal way) then I get the output message "Stage is closing", which is the desired behavior.
However, when calling Controller::exitApplication the application closes without invoking the handler (there's no output).
How can I make the controller use the handler I've added to primaryStage?
If you have a look at the life-cycle of the Application class:
The JavaFX runtime does the following, in order, whenever an
application is launched:
Constructs an instance of the specified Application class
Calls the init() method
Calls the start(javafx.stage.Stage) method
Waits for the application to finish, which happens when either of the following occur:
the application calls Platform.exit()
the last window has been closed and the implicitExit attribute on Platform is true
Calls the stop() method
This means you can call Platform.exit() on your controller:
#FXML
public void exitApplication(ActionEvent event) {
Platform.exit();
}
as long as you override the stop() method on the main class to save the file.
#Override
public void stop(){
System.out.println("Stage is closing");
// Save file
}
As you can see, by using stop() you don't need to listen to close requests to save the file anymore (though you can do it if you want to prevent window closing).
Suppose you want to ask the user if he want to exit the application without saving the work. If the user choose no, you cannot avoid the application to close within the stop method. In this case you should add an EventFilter to your window for an WINDOW_CLOSE_REQUEST event.
In your start method add this code to detect the event:
(Note that calling Platform.exit(); doesn't fire the WindowEvent.WINDOW_CLOSE_REQUEST event, see below to know how to fire the event manually from a custom button)
// *** Only for Java >= 8 ****
// ==== This code detects when an user want to close the application either with
// ==== the default OS close button or with a custom close button ====
primaryStage.getScene().getWindow().addEventFilter(WindowEvent.WINDOW_CLOSE_REQUEST, this::closeWindowEvent);
Then add your custom logic. In my example i use an Alert popup to ask the user if he/she want to close the application without saving.
private void closeWindowEvent(WindowEvent event) {
System.out.println("Window close request ...");
if(storageModel.dataSetChanged()) { // if the dataset has changed, alert the user with a popup
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.getButtonTypes().remove(ButtonType.OK);
alert.getButtonTypes().add(ButtonType.CANCEL);
alert.getButtonTypes().add(ButtonType.YES);
alert.setTitle("Quit application");
alert.setContentText(String.format("Close without saving?"));
alert.initOwner(primaryStage.getOwner());
Optional<ButtonType> res = alert.showAndWait();
if(res.isPresent()) {
if(res.get().equals(ButtonType.CANCEL))
event.consume();
}
}
}
The event.consume() method prevents the application from closing. Obviously you should add at least a button that permit the user to close the application to avoid the force close application by the user, that in some cases can corrupt data.
Lastly, if you have to fire the event from a custom close button, you can use this :
Window window = Main.getPrimaryStage() // Get the primary stage from your Application class
.getScene()
.getWindow();
window.fireEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSE_REQUEST));
Ahh this is a known bug in JavaFX where the Stage will not close if a modal dialog is present at the time of closing. I will link you to the bug report which I just saw today. I think it is fixed in the latest release.
Here you go:
https://bugs.openjdk.java.net/browse/JDK-8093147?jql=text%20~%20%22javafx%20re-entrant%22
resolved in 8.4 it says. I think this what you are describing.
public Stage getParentStage() {
return (Stage) getFxmlNode().getScene().getWindow();
}
btnCancel.setOnAction(e -> {
getParentStage().close();
});
As far as i have seen the event:
(1) private void jTabbedPane1StateChanged(javax.swing.event.ChangeEvent evt) {}
Checks whether a new tab is added or an exiting tab is deleted or not.
On googling , i found this code:
(2) ChangeListener changeListener = new ChangeListener() {
public void stateChanged(ChangeEvent changeEvent) {
// my code
}
};
jTabbedPane1.addChangeListener(changeListener);
I guess since it uses stateChanged event , it should do what the same a my first code.
By t way even after using both the codes i could not get the required resuts(ie An event that could be invoked when user changes the tab).
Can anyone suggest me a good event [i am using netbeans GUI environment] for effective action. (I dont want any mouseEvents)
Edit:
I want the following code to be excecuted if the tab changes:
String send3=( jTabbedPane1.getSelectedComponent().getComponentAt(0,0)).getName();
The above code dynamically gets the name of jTextarea (in the current tab) which is created dynamically in the jTabbedPanel.
I just checked my own source code where addChangeListener() works fine. The event is fired whenever the tab is changed by the user or programatically. In stateChanged() itself, the now selected tab is determined by
JTabbedPane p = (JTabbedPane)e.getSource();
int idx = p.getSelectedIndex();
In my application , by clicking the save button, the dialog prompts a message with OK button. While recording 'the OK button' got recorded and shows 'text_ok().click(atpoint(11,8));'. But during play back it shows me a 'Object not found' error.
Recently updated the RFT Version 8.2.2.1 after this only this issue is seen.
Can anyone say me how to solve this problem or any coding in java.
My regression is waiting due to this, your help is highly appreciable.
Thanks in Advance.
You have not mentioned what type of dialog window it is. But you can try the IWindow API of RFT to find active top window and perform the click as specifed below.
The Following code for eg can handle an alert dialog box in html by calling
handleDialogButton("Message from Webpage", "ok");
Or,to click on canel button on Notepad's Font dialog (Format>Font) by calling
handleDialogButton("font","cancel");
-------Sample code----
/*
* Activates the top window with the given caption and clicks on the child control(window) with the specified text
* #param caption- Caption of the Dialog window
* #param btnToClick- Text of the button(any other control) to click
*/
void handleDialogButton(String caption,String btnToClick)
{
IWindow[] windows = getTopWindows();
for(IWindow window: windows)
{
if(window.getText().equalsIgnoreCase(caption))
{
window.activate();
//window.close(); //we can just close it also n break.
IWindow[] children = window.getChildren(); // OR go thru the children to get the child
for(IWindow child:children)
{
if(child.getText().equalsIgnoreCase(btnToClick))
{
child.click();
break;
}
}
}
}
unregisterAll();
}
From the poing of Selenium Web driver API i recommend you do the following:
Alert alert = driver.switchTo().alert();
alert.accept();
Hope this works for you
I am working on J2ME application. I want to show alert in Form and display another Form from another class. I have tried the following method to show alert.
public void showMsg()
{
Alert success = new Alert("Data Not found.");
//success.setImage(img2);
success.addCommand(new Command("Ok", Command.OK, 0));
success.addCommand(new Command("Cancel", Command.CANCEL, 0));
success.setCommandListener(this);
success.setTimeout(Alert.FOREVER);
Display.getDisplay(parent).setCurrent(success, chapterForm);
}
After showing the alert I am jumping to another form as:
Display.getDisplay(parent).setCurrent(welcomeForm);
When I run this it don't show the alert but jump to the welComeForm. So, how can I show alert and then jump to another form.
The Alert won't advance automatically to chapterForm because you have replaced the default listener on the Alert with this. Use the commandAction() event in the CommandListener interface to get the OK or Cancel from the Alert. Then you can use Display.setCurrent(Displayable d) to show the Form you want to display.
Display.getDisplay(parent).setCurrent(welcomeForm) is most likely the reason why it don't show the alert but jump to the welComeForm. To be precise it (device) may show alert for a moment, but as soon as you invoke that setCurrent(welcomeForm), it gets momentarily overwritten by welcomeForm.
If you want welcomeForm to be dissplayed by command from alert, just
wipe out the code setCurrent(welcomeForm) from where it is now
insert that wiped-out code into this.commandAction method (this is command listener you use in your code exerpt)
A nifty solution is to start a new thread after setting the current display to the Alert, and in this new thread you can do a Thread.sleep(2000); in order to wait, and after that you display the new form.