-2

In C# this is done by using delegates and event, here is the link: Passing Data between Windows Forms.

My requirement is like this. User have to search the item code by loading the whole items from the database to a new form's JTable, this new form is shown by clicking a button in the main form (or any other form). After that appears, user Double click on specific item code in the table and that item code is passed to the main form's (or any other form's) text field and closed the current form.

here is the output from c# https://www.youtube.com/watch?v=_lPkc1YV2vQ&feature=youtu.be

Macmarc25
  • 11
  • 2

2 Answers2

1

The second window should not be a JFrame, but rather a JDialog, possibly modal. If it is modal, then the calling window will know when the 2nd dialog window closes, since program flow stops at the calling code immediately from when the dialog is displayed (think of how JOptionPanes work) and does not resume until the 2nd dialog window closes. It is right then that you would extract pertinent data from the objects associated with the dialog window, and this information can be obtained by simple means such as by calling appropriate getter methods.

e.g.,

JDialog someDialog = new JDialog(myJFrame, "My Dialog", ModalityType.APPLICATION_MODAL);
someDialog.add(myContentPaneWithMyGui);
someDialog.pack();
someDialog.setVisible(true);

// here code flow stops until the dialog is no longer visible.

// now call my getter to extract data
SomeType someData = myContentPaneWithMyGui.getSomeData();
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
1

If you want to stick with your event/delegate way of doing, you could consider using BeansBinding.

It requires your model to fire events when their data changes, and add your Swing GUI elements registers as propertyListeners on those models.

JSlain
  • 566
  • 3
  • 20