Cannot downcast an Object into Frame in Java Window Event handling - windowlistener

I am learning basic event handling in Java.
I am working on Frame Closing method (Implemented from WindowListener). I understand that getsource() call returns the object where actual event is taking place. When it downcast to Window, it works fine but when I downcast it to one more level below (to Frame), it doesn't work and gives an error- Frame cannot be resolved to a type
In my main class, I am extending Frame.
#Override
public void windowClosing(WindowEvent e) {
Object objSource= e.getSource();
//Window objWindow = (Window)objSource; - It works fine
Frame objWindow = (Frame)objSource; //- Why it doesn't work
objWindow.dispose();
}

Unfortunately import java.awt.Frame; package was not imported in code hence it was giving error in closing the Frame window. Above code works perfectly for both Frame and Window call.

Related

How to get return value in this case? (Java FX)

I'm writing part of a cross-platform application, where we mostly use REST (jersey) and Hibernate to communicate between systems. I'm new to JavaFX, but my side of the program should use it to get input values from users. Here is how the code flow would look:
public class startingClass{
...
public void startingMethod(Payload payload){
//send REST requests to different places with different payloads, like:
Response response = Utility.sendPostRequest(URI, payload2);
something = response.readEntity(something.class)
//more processing with the returned values
...
}}
In one of the places where I sent a request:
#Path("something")
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON + ";charset=UTF-8")
public class Resource{
...
#POST
#Path(something)
public Response doSomething(Payload payload) {
//show JavaFX window with text fields and an okay button
JavaFXClass.launch(JavaFXClass.class);
/* THIS IS WHERE I would need to get back the input values somehow */
//payload3 has the input values I need to send back
return Response.entity(payload3).build();
}}
The JavaFX class extends application and and overrides the (void) start method where I put together the window I want to show and after the button click (if inputs are okay) I close the window.
So the idea is that the startingMethod would have to wait, until the response comes back (maybe return with some default values, if the user doesnt type in anything for a minute - what would be the elegant solution for that?) with the input values. This would guarantee the sync.
If I use more REST or database saves inside the JavaFX class then I can't be sure the values are there by the time I wanna use them in the startingMethod (probably not) and it's probably a really bad looking solution anyway.
What could I do? I dont know much about callback methods in javafx, can those help me here? Thanks!
In the end I moved the JavaFXClass into the Resource class. Meaning Resource class extends Application, overrides start, etc. In the doSomethingMethod I call launch in a try-catch block, catch the IllegalStateException if needed and call start() instead (also in a try-catch block). The textfield input values are stored in a global variable after.
Also in the start() method I havePlatform.setImplicitExit(false);
so the doSomethingMethod() can be called multiple times without a problem, starting the javaFX window. It's not a pretty solution.

call ds_list from diffrent object error

I have created a textbox object and in its create event it creates list like
lines = ds_list_create();
in step event of textbox I use ds_list_add(lines, "line one");
and it works fine.
Now I have a diffrent object that try to call ds_list_add(Textbox.lines, "line from diff object");
but on running it gives error about var not set before reading. i tried also to change to global.list = ds_list_create(); and still same problems.
can someone explain how to call ds_list from diffrent object.
You can call ds_list_* functions right like you did. Just, you must make sure the data structures a function is referring to does actually exists.
I tried the following for testing purposes. Create two objects, objTextBox and objOther and set their events as follows.
For object objTextbox:
Create Event
list = ds_list_create();
Press 'Space' Event
var str = get_string("I'm objTextbox:","");
ds_list_add(list,str);
Draw Event
for (var i=0;i<ds_list_size(list);i++)
draw_text(10,10+15*i,string(ds_list_find_value(list,i)));
For object objOther:
Press 'Shift' Event:
var str = get_string("I'm objOther:","");
ds_list_add(objTextbox.list,str);
Now add them to a room, and make sure first object to be created is objTextbox, which is the one creating the ds_list. Run.
When dealing with data structures, always make sure they were created before working with them.

QT: Getting text content from web page

I've been trying to start with a simple app that retrieves data from a simple HTML page upon clicking a button and stumbled upon a rather helpful tutorial on QT-Project and have been trying to implement it for my own project.
Everything manages to compile until I try to actually try to implement the loadImage function (as found in the tutorial). (I actually had to initialize m_pImgCtrl as Filedownloader * m_pImgCtrl = new FileDownloader(imageUrl, this); and I'm not exactly sure how it's suppose to work without prior object declaration?)
From what I get, m_pImgCtrl isn't actually defined in the loadImage() function since it is initialized outside of the function? Or does the connect() function do something that I'm not too aware of?
Thanks for the help!
The tutorial doesn't tell you the whole story.
The code in section Usage is supposed to be part of a class MainWindow – the controller of your main window (see line 1 of tutorial's last snippet). This class contains a slot loadImaged() called when the NetworkReply has finished. It also has a member FileDownloader * m_pImgCtrl.
For instance, the second Usage snippet could be part of a slot MainWindow::buttonClicked() like
void MainWindow::buttonClicked()
{
QUrl imageUrl("http://qt.digia.com/Documents/1/QtLogo.png");
m_pImgCtrl = new FileDownloader(imageUrl, this);
connect(m_pImgCtrl, SIGNAL(downloaded()), SLOT(loadImage()));
}

in Flex 3.2 Having troubles converting remote object result to specific object on client side in modules

in Flex 3.2 Having troubles converting remote object result to specific object on client side in modules.
For example I have VIPSAdmin module.
it has function
private function doResult(event:ResultEvent):void {
var data_:Array = ArrayUtil.toArray(event.result);
var result:ResultDTO = data_[0] as ResultDTO;
if(!result.isError()) {
trace(result.result);
vipsAdminDTO = result.result as VIPSAdmin;
compId= vipsAdminDTO.compId; // second time dying here
}
}
Function invoked when I get data from remote objet.
First time all great,when I unload this modeule and load it again:
data_[0] as ResultDTO;
Performs fine, but
vipsAdminDTO = result.result as VIPSAdmin;
vipsAdminDTO always null!
Even when
trace(result.result);
produces [object VIPSAdmin]
What a heck I missing here!? Looks like it just cannot do
result.result as VIPSAdmin;
even when trace and debug says it is instance of VIPSAdmin
I've figured out what is the problem, problem is that when I first instantiate something in module then in main app, somehow classes are not alined even that they are identical !
So solution is to make a fake instance in application class first, then if you use that same class to make an instance in module it will work!
I do it very simple in my main application class I just added:
VIPSAdmin;
This seems to create some sort of ghost instance, which I belie will be pickup by GC later, but will build tables of instances properly! Which solved my problem.
Not sure if this is appropriate solution ! but it sure works.

Simple application of delegates in c# to get started

I am trying to learn delegates in C# from this article
http://msdn.microsoft.com/en-us/library/aa288459(v=vs.71).aspx
I am able to understand the code a bit but i am not able to understand where and why would a developer want to use delegates. Can somebody give an easy scenario which can help me start with delegates?
Update
I read this statement everywhere, "The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked."
But why would i want compiler shouldn't know about function i pass? I can smell abstraction here but what's the use? Any real time scenario is required.
A good example of using delegates would be callbacks. Imagine you have a class DbSearcher. That class has the method Search(string q) and when you call this method it takes it 1 minute to return. You want to eventually display results of the search but you don't want to keep the user waiting for them to appear while being unable to do anything more. What you do is you change your method to, for example, Search(string q, Action displayResults) and fire it in a separate thread. displayResults here is a delegate which you will call inside the Search method once the search results are retrieved from the db.
Look, in windows forms there are classes like button, image, textbox etc... All of them have event handlers like button.click, textbox.texchange, ... which are delegates themselves.
and when you want to do something on button click you write function which is void and has two aguments: of object type and EventArgs. The one who wrote that button class didnot know what to do on button click but gave you delegate:
public delegate EventHandler Click;
where will be your defined methods like:
public void mymethod(object s, EventArgs e)
or every method tha is void and has that parameters
You can find so many example and code snippets over the web . Here is my example, think that your user is going to decide which operation he/she is going to do in the following,
Add two numbers
Subtract two numbers
Multiple two numbers
Divide two numbers.
But you have one common method to perform all this
Operation(some delgate method)
{
// do some operation
}
u you can pass the delegate at run time based on user selection.
This is just an example.
A delegate is basically a reference to a method.
You can for example have to different methods for changing a string:
public static string ChangeOne(string s) {
return s.TrimStart();
}
public static string ChangeTwo(string s) {
return s.TrimEnd();
}
Depending on some criteria, you can choose between them, and put the choice in a delegate:
Func<string, string> change;
if (DateTime.Today.DayOfWeek == DayOfWeek.Sunday) {
change = ChangeOne;
} else {
change = ChangeTwo;
}
Then you can use the delegate just a regular method. The code that uses it doesn't have to know what the method does, or why:
string x = " asdf ";
x = change(x);
Delegates are for example widely used for generic collections, where the library methods doesn't have to know anything about the objects in the collection. You just provide it with a delegate to a method that picks out the relevant information.
Here the Where method doesn't know anything about the objects in the list, it only gets a delegate to a method that determines if an object should be included in the result or not:
IEnumerable<obj> older = listOfObj.Where(o => o.Age >= 18);

Resources