Search code examples
javaswinguser-interfacejtextfieldjlist

How can I pass data from text-fields in 1 JFrame Form to a JList in another JFrame Form?


Working on a GUI that is for allowing Customers to register their information on 1 GUI (Button click to add new customer) whereby after clicking the add button to register their information it will collect up all their data (forename, surname, address Line 1/2, city & postcode) and then upload it onto a JList in the previous GUI in which it displays the customer's first & last name. From which a user can then select that particular customer and click to update the customer's details in an new GUI.

The problem I'm having is trying to figure out how I can pass all the customer's data from the text-fields in the 2nd GUI and transfer them into the JList in the 1st GUI with only the first & last name being displayed. The 2nd problem I'm also facing is how to then open up another GUI which will then reverse the process when that particular customer has been selected so that changes can be made to their details.

In my Person class in the data-model package I have got the method getFullName which goes as follows:

public String getFullName()
{
    String result = this.forename + " " + this.surname;
    return result;
}

Is does anyone know how or could show me how I would be able to apply a solution to these problems?

I have tried experiementing with the getFullName method within the 2nd GUI upon pressing btnAdd, as shown below:

public void addItem()
{
    String result = this.forename + " " + this.surname;
    return result;
}

But alas I have had no luck.


Solution

  • Regarding your questions:

    The problem I'm having is trying to figure out how I can pass all the customer's data from the text-fields in the 2nd GUI and transfer them into the JList in the 1st GUI...

    There are usually two problems in this situation --

    1. How to get the information held by the fields of add new customer dialog window (and yes, this should be a dialog window of some form, either a JDialog or a JOptionPane) into the parent window object, and
    2. How to be notified exactly when this transfer of information should take place, and this is usually the more problematic of the two.

    Again the first issue is usually easy to solve, since it is nothing more than a specific version of the more general problem of how to pass information between two classes. The most simple way to solve it is to have the parent window class call getter methods in the dialog window class to extract its state. For instance, the dialog window could have public getter methods for the data held in each JTextField, which the calling parent window could call, or the dialog class could have a getCustomer() method within which it gathers all the information held by its input fields, creates a Customer object, and then returns that object.

    Perhaps a better and more robust way of transferring your data is to structure your program as a Model-View-Control or MVC type program, have your add new customer dialog window update the model when its submit button is pressed, and have the parent main window be notified by the model that its data has been changed, and so it knows that it must update its view of the data. This may be overkill for your program, and so I am not going to push this recommendation at this time, but in the future, when you are creating larger more complex programs, this is really what you will want to be doing since it will more easily allow your program's classes to have high cohesion (code that deals with the same things are close together) and low coupling (code that deals with unlike things are not tightly bound which decreases code complexity), making it much easier to debug and improve your program.

    Again, the 2nd issue is more difficult to solve. If the dialog window is to close after obtaining and submitting the information that constitutes one customer, than the easiest solution to this is to make the new customer dialog window a modal JDialog or a JOptionPane (which in really is nothing more than a specialized modal JDialog). Why this helps is that Swing has a special mechanism for modal dialogs where it freezes code flow in the calling window starting immediately after setting the dialog visible. And so the calling code will always know when the dialog is no longer visible since its code's program flow will resume only when the dialog isn't visible. So you would want to extract the dialog window's data in the lines immediately following the one that sets the dialog or JOptionPane visible. And before you discard JOptionPanes as being too simplistic, understand that their second parameter, the one of type Object can take any Swing GUI component as a parameter, including a JPanel that holds a very large and complex GUI, making these tools extremely useful.

    If the second dialog window will not be closed on the submission of a Customer and is to be left open, for instance if you want to leave it open so that the user can enter multiple Customers, then you'll likely want it to be a non-modal JDialog and will need to set up some kind of notification scheme so that the calling code will be notified when the user has submitted a Customer. One possible way is to allow the calling class to add an ActionListener to the dialog's submit button by giving the dialog class a public addActionListener(....) method. Another is to use Swing's PropertyChangeSupport and have the calling class add a PropertyChangeListener to the add customer dialog, and thus be notified of significant changes to its properties (i.e., that the submit button has been changed, or that a new Customer has been created). If you use an MVC design for your program, then the notification will be from the model to the view. In other words, the dialog will create the new Customer, the submit button will have the Control add the Customer to the Model, and the main GUI's Model listeners will be notified of the newly created object and will display it.


    ... how I can pass all the customer's data from the text-fields in the 2nd GUI and transfer them into the JList in the 1st GUI with only the first & last name being displayed

    Your JList should be a JList<Customer> so that it holds a collection of complete Customer objects. You can easily change how the JList displays its data by overriding the Customer toString() method (not recommended since this method is more for debugging than for end user display), or better by assigning the JList a custom ListCellRenderer (recommended). The Swing JList tutorial will explain how to do this, and you can come back here with your code attempt if you're stuck at this step.


    The 2nd problem I'm also facing is how to then open up another GUI which will then reverse the process when that particular customer has been selected so that changes can be made to their details.

    This would be solved in largely the same way as above, except rather than creating a new Customer and adding it to the collection displayed by the JList, you would replace one Customer in the JList with the edited one.

    For more detailed help, consider creating and posting a Minimal, Complete, and Verifiable Example Program. We don't want to see your whole program, but rather you should condense your code into the smallest bit that still compiles, has no extra code that's not relevant to your problem, but still demonstrates your problem.