Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Communication between JFrames

 
Onslow McCann
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I have made a program where a JFrame gets created with TextFields to hold scores and a bunch of buttons which change those scores.

Under some circumstances I need the user to type in a score and I've made it so that when a combination of buttons is pressed, another JFrame gets created. This 2nd JFrame contains another TextField and an enter button.

I need for the score in this TextField to be added to the score in the TextField in the original JFrame upon pressing the enter button but I have been unable to achieve this.

Does anyone have any advice they could give me?

Thanks,
Onslow
 
Campbell Ritchie
Marshal
Posts: 52621
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you are better using a dialogue from the JOptionPane class to get your input.

ActionEvents fired from the buttons, and add an ActionListener to the buttons.

BTW: JTextField fires an ActionEvent when the enter key is pressed, so you can fire the action from the JTextField directly, as well as from the button, using an ActionListener. There are a couple of other threads on this forum which I posted on about ActionListeners in the last half-hour; they might be of help to you.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, so you have classA which has a JTextField and JButtons, which,
when clicked in a certain combo, creates classB which also has a JTextField
and a JButton, which, when clicked should add classB_JTextField to classA_JTextField.

if this scenario is correct, these are the (rough) steps:
(add error handling to suit)
 
Onslow McCann
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys, huge help.

Never considered the possibility of passing the JTextField to the other class. Brilliant, it works!
 
Campbell Ritchie
Marshal
Posts: 52621
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Onslow McCann:
Never considered the possibility of passing the JTextField to the other class.
Nor did I.

That's the good thing about JavaRanch; you always learn something, even when you least expected it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!