Link to home
Start Free TrialLog in
Avatar of Chisel
Chisel

asked on

Sending information between classes

Hi,
    I'm a relative newcomer to Java and am having a problem getting some code to do what I want. I have a main window with a vareity of menu options/buttons. One of these buttons opens up a new window as a data entry panel.

My problem is that I have code in this new window such that when I click OK the data entered in the field has an operation performed on it, and the result is displayed back in the main window. I can't figure out how to get this to work though. Here is a sample of my code cut down to illustrate the bits I'm using.

---From the Main Window---

public class MainMenu extends JFrame implements Action Listener
{
     ...
     JTextArea editPanel;
     ...
     private AddEntry addentry;

     public MainMenu()
          ...
          editPanel = newJTextArea();
          ...
         
At some point the following code is called on pressing a button:

     addentry = new AddEntry(this);
     addentry.pack;
     addentry.setLocationRelativeTo(this);

This creates the new window I'm working with. Inside this new window is a button handler, and here is where my problem lies. I want the method in here to process some information entered by the user in a JTextField and return the output to the main window in the editPanel. Here is a code snippet of the new window (It uses 2 classes to draw it).

public class AddEntry extend JDialog
{
     private AddEntryPanel addentrypanel;
     public AddEntry(JFrame owner)
     {
     ...
     okButton=new JButton("OK");
     ...
     addentrypanel = new AddEntryPanel();
     ...
}
class ButtonHandler implements ActionListener
{
     ...
     if ("OK".equals(label))
     {
     String newinfo = addentrypanel.getInfo();
     ...
     <<Do update to main page code using String newinfo>>
     }
}

Hope this code is sufficient to display the structure of my program.

Thanks in advance,
                           Chris
ASKER CERTIFIED SOLUTION
Avatar of sudhakar_koundinya
sudhakar_koundinya

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sudhakar_koundinya
sudhakar_koundinya

public class AddEntry extend JDialog
{
    private MainMenu owner=null;
     private AddEntryPanel addentrypanel;
     public AddEntry(MainMenu owner)
     {
         this.owner=owner;
         okButton=new JButton("OK");
         addentrypanel = new AddEntryPanel();
    }
    class ButtonHandler implements ActionListener
    {

     if ("OK".equals(label))
     {
     String newinfo = addentrypanel.getInfo();
     owner.editPanel.setText(newinfo);

     }
   }
Avatar of zzynx
>>  <<Do update to main page code using String newinfo>>

Since you pass your JFrame to AddEntry
>> public AddEntry(JFrame owner)
you can pass it to your ButtonHandler too:

public class ButtonHandler {

     private JFrame mainFrame;

    public ButtonHandler(JFrame f) {
       mainFrame = f;
    }

    Then you can replace
     <<Do update to main page code using String newinfo>>
     by
            mainFrame.setInfo(newinfo);

}
Avatar of Chisel

ASKER

Apologies for the last response, worked great thanks =)
>> Apologies for the last response, worked great thanks =)
You can always ask to reopen the question by posting a zero-point question in
https://www.experts-exchange.com/Community_Support/

Subject: Moderator Please Reopen
Body: Please reopen this question:
https://www.experts-exchange.com/questions/21346794/Sending-information-between-classes.html

Once the question is reopened, you can re-accept by splitting the points
I can even ask to reopen this one for you. Just let me know what you want
Avatar of Chisel

ASKER

Sorry, that was meant to say late response, not last response, I used the code suggested by sudhakar_koundinya.
OK