-1

I have the following code to retrieve information from a text box to another within the same class

btnAddItem.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent e) {

         String tabno = textArea_TableNo.getText();
         String name = textArea_Name.getText();
         String size = textArea_size.getText();
         String quan = textArea_quantity.getText();
         String price = textArea_price.getText();
         textArea.append(tabno + ",    " + name + ",    " + size + ",    " + quan + ",    " + price + "\n");

But I am not sure how to do this exactly same operation between two class. I probably have to "extend" my class but I've already extended the class to my database class. Im just not sure how else I can do this. Any suggestions to get my head around this would be appreciated..

kleopatra
  • 51,061
  • 28
  • 99
  • 211
FatmaTurk
  • 83
  • 3
  • 3
  • 9
  • So what belongs in each class? In other words, how do you want the two classes to communicate? – Mark Peters Mar 14 '12 at 19:18
  • @MarkPeters well I'm quite new to java but the way I have been doing it so far is through using "extend" if that's what you mean – FatmaTurk Mar 14 '12 at 19:20
  • What I mean is that you've given almost no information about what you're trying to accomplish by splitting this code into two classes. Why do you want to split it up? How are the two classes organized? This question is really unanswerable in its current form because you haven't given any context at all. – Mark Peters Mar 14 '12 at 19:25
  • @MarkPeters Just to be more precise Lets say I have class1 and class2. Class1 has textarea_1 and textarea_2 so I want to be able to retrieve this information from class1 and display it on class2 with a button click. The reason for that is because both classes have some other information. So I want this data to be displayed on class2 with some other new data that is already on class2. I totally understand your point about not providing no context but I am just not sure how I can do this, though I have done some research I couldn't get my head around this.. – FatmaTurk Mar 14 '12 at 19:34

1 Answers1

0

Well, you can have a public method to retrieve the text, and use this on another class. For example:

class Class1 {
  private JTextArea textOne;

  //... declare other fields, build GUI, etc

  public String getTextOneText() {
    return textOne.getText();
  }
}

class Class2 {
  private JTextArea textTwo;
  private Class1 class1;
  public Class2( Class1 class1 ) {
    this.class1 = class1; //store reference to class1.
  }
  //use the getData method to append text from the Class1.
  void getData() {
    textTwo.append( class1.getTextOneText() )
  }
}

In this example, storing the reference to the instance of Class1 in Class2 and using the method getData should do what you want.

Another way is use the Observer Design Pattern to communicate between classes.