0

We programmed a game with two windows: MainMenu and Playfield. When you click on start in the MainMenu - Playfield opens. In the MainMenu there are 2 JTextfields, where the two players can enter their name. At the moment the 2 JTextfields doesn't have any functionality.

Now the question is: We want to get this name and we want to store them till somebody wins and after win is true we want to output something like "xy wins!" in a Dialog Box. How can we implement that in both windows?

slartidan
  • 20,403
  • 15
  • 83
  • 131
  • I think you need to do some research into "model-view-controller" to better understand how data can be shared between classes – MadProgrammer Jun 22 '17 at 06:56

2 Answers2

1

The windows are just your views to present/get some information to the user. You need to store the data entered in the MainMenu in some model classm, which both Frames can access.

M. Haverbier
  • 383
  • 2
  • 13
0

It doesn't matter in which Frame your textfields are and in which Frame you want to print the output - the component hierarchy (i.e. which component is in which container) is irrelevant for processing input and output.

In this particular case, you have to listen to an event which tells you that the JTextFields have received input. This could be

  • a focusLost event from a FocusListener registered with the JTextFields
  • a changedUpdate/insertUpdate/removeUpdate event from a DocumentListener registered with the document backing the JTextField (i.e. textfield.getDocument().addDocumentListener(...))
  • an action event from an ActionEvent from a JButton which the user clicks to confirm their input

Once you receive that event, you retrieve the text from the JTextfield, store it in a suitable variable/field and then later use it to fill the text message presented in the dialog.

Markus Fischer
  • 1,326
  • 8
  • 13