A question similar to this has been asked before, and will be asked again I am sure. I have spent some time googling and trying to work this out for myself, but I am at an impasse.

What I am trying to achieve is to implement a database search screen for an application I have been tasked with creating.

What I am doing is loading a JFrame with JPanels through a cardlayout. On a few of these JPanels I will have a button that calls another JFrame that will search a DB and load the result set into a table displayed on the frame. The search is only conducted after the user has typed in the search data and pressed search. The screen is to be disposed of, and return the values once the user has selected a row in the table and pressed another button.

I have all the GUI components created, and the DB search functionality working. The problem I have is returning the value.

At the moment I have the Search JFrame object created by the JPanel that has the button, but any attempts I ave made to get the data from the search frame have resulted in null results.

Is there a way to return a value through this way and if so how?

I have tried getter/setter methods, but obviously since the initial results of the variables are null it returns as null. I tried linking the result to the table with the same result. (Actually it returned -1, but that's just splitting hairs). I tried changing the search screen to a JDialog as I read that that can halt the running code while waiting for a response. I tried various methods to check if the search screen was enabled/visible and to hold on those.

All to no avail.

Any help is appreciated.

  • 1
    This question might be better asked on StackOverflow. – Adam Zuckerman Mar 28 '14 at 20:12
  • @Fos, Sometimes it's better to consider the broad case instead of the specific case, looking for patterns the entire time. – alvonellos Mar 28 '14 at 23:36

Without going deep into details...

An alternative is to have a common model that both frames share. When you pop up the frame for the user to run the search, you pass the model object which should be shared between the two UI components. When you populate this model and come back to the main frame, you can use the shared model to update any views.

A second alternative is use callbacks with an observer pattern. In this way, the main frame registers with a listener to receive events, and when you pop up the search frame, you pass the listener to it so that when user does a search, you can notify all listening objects about the results.

  • 1
    Thank you a lot. You gave me the answer I needed and now I feel like a dolt for overlooking it. I used the callback method and added the listener for th.e button to accept the result on the search frame to the calling panel and it does just what I need – Fos Mar 28 '14 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.