-1

I have 2 frames in an application. In the first one, I have a JList and a JButton. By clicking the button the second frame opens and the client should fill a form, like name and family name. By clicking submit, the name and family name should go in the list (in the first frame).

But I can't do the last part. What can I do? I know it's related to objects, but I don't know how to do this. I heard that I have to create an object, and transfer the data which are filled by the client, to the object, and then send from the object to the list.

The point is I don't know which listener do I have to use for the list.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Sep 09 '14 at 07:48
  • *"The point is I don't know which listener do I have to use for the list."* You shouldn't need to add any listener to the list. If the new information is added to the list **model**, it will appear in the list. – Andrew Thompson Sep 09 '14 at 07:53
  • `By clicking submit, the name and family name should go in the list` - don't use a JList to display multiple columns of data. Instead you should be using a [JTable](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html). – camickr Sep 09 '14 at 14:10

2 Answers2

1

One way is to follow suit of JOptionPane, which just has a static method that returns a value (and "blocks" parent frame interaction) when called.

  1. Create a JPanel, say NameInputPane. Make the ui view there.
  2. Have a static method in that panel showInputDialog(...) which returns whatever you want the dialog to return, say a User object or just a simple String[].. up to you. Also in the method you create a JDialog. When the user hits submit or closes the window, the value is returned.

What this does is separate the responsibilities. The dialog is simply there to get information, and returns that information. It's the responsibility of the main app (frame) to decide what to do with that info. (i.e. add it the the list)

There are many different ways you can handle this task, but the general rule for short lived windows (e.g. for just getting input) is to use a modal dialog, rather than a frame.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
0

Maybe this can help you. The below code creates a Main-Frame with a JList and a JButton. If you click on the button a Client-Frame will appear and you can modify the names (in JList).

You just have to work on the same data object (the model)...

image

public class TransferDataTest {

    public static void main(String[] args) {
        new TransferDataTest();
    }
    public TransferDataTest() {
        new MainFrame();
    }
}
public class MainFrame extends JFrame {

    public MainFrame() {
        super("Main");
        setLayout(new BorderLayout(4, 4));

        // Create a model which you can use for communication between frames
        final DefaultListModel<String> model = new DefaultListModel<>();
        model.addElement("Mike");
        model.addElement("Smith");

        JList list = new JList(model);
        getContentPane().add(list, BorderLayout.CENTER);

        JButton b = new JButton("Change...");
        b.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // Pass over your model so the client will can modify the data
                new ClientFrame(model);
            }
        });
        getContentPane().add(b, BorderLayout.SOUTH);

        pack();
        setVisible(true);
    }
}

public class ClientFrame extends JFrame {

    public ClientFrame(final DefaultListModel<String> model) {
        super("Client");
        setLayout(new BorderLayout());

        // Init the GUI with data from the model
        final JTextField tfFirstName = new JTextField(model.get(0));
        final JTextField tfLastiName = new JTextField(model.get(1));
        JButton submit = new JButton("Submit");
        submit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // On submit click update the model with data from the GUI
                model.set(0, tfFirstName.getText());
                model.set(1, tfLastiName.getText());
            }
        });

        getContentPane().add(tfFirstName, BorderLayout.NORTH);
        getContentPane().add(tfLastiName, BorderLayout.CENTER);
        getContentPane().add(submit, BorderLayout.SOUTH);

        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }
}
msrd0
  • 7,816
  • 9
  • 47
  • 82
a3po2.0
  • 251
  • 1
  • 3
  • 8