This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

One class calling another class incharge of GUI

 
Lucky Singh
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a class Manager and a class ManagerGUI.

My ManagerGUI class looks somehting like this-

public class ManagerGUI extends JFrame
{
String name;
JPanel namePanel;
JTextField nameInput;
JLabel nameLabel;
Manager empAgent;

ManagerGUI(Manager ea)
{
super(ea.getLocalName());
empAgent = ea;

//make the name panel and add it to the topPanel.

addListeners();

getContentPane().add(topPanel, BorderLayout.CENTER);
setResizable(false);
}//end constructor.

public void show()
{
pack();
super.setVisible(true);
}

void addListeners()
{
nameInput.addActionListener(new NameL());
}

class NameL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
nameE = nameInput.getText();
}
}
}//end class ManagerGUI.

I have tried to seperate it out so that any changes can be easily implemented (although it perhaps is a long way of doing things).

Now I have a class Manager that wants to call the GUI and then process the information got from the text field.

I use the following lines to call the GUI class.
manGui = new ManagerGUI(this);
manGui.show();

Is this the correct way of calling the GUI class?
How do I get to use the variable nameE here, in the Manager?

Thanks,
Lucky.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50251
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is your main method? To call a JFrame you have to do 4 things:
  • Create it, with its constructor; you can give it a [String] name and the super(name) call will write that name in its title bar. You have used a different way of getting the name, from the Manager class.
  • Set its closing behaviour, with the setDefaultCloseOperation() method
  • Set its size; there are several versions of the methods available, or you can set its bounds.
  • Set its visibility.
  • All those methods are to be found in the API under JFrame.
    I can't see from your code where the nameE variable comes from, so don't know how you can use it.

    CR
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 50251
    79
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sorry, I forgot to say: you don't use the show() methdo any more.
     
    Lucky Singh
    Ranch Hand
    Posts: 125
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for your response. Well, this is for an application using agents hence I don't have the main class.
    I also have a problem here-

    class ManagerGUI extends JFrame
    {
    String nameE, locE;
    //create panels, buttons and textfields and set up the JFrame all in the
    //constructor. call method addListeners();

    // Then add the listeners to both form fields as follows.
    void addListeners()
    {
    nameInput.addActionListener(new NameL());
    locChoices.addActionListener(new LocationL());
    }

    class NameL implements ActionListener
    {
    public void actionPerformed(ActionEvent e)
    {
    nameE = nameInput.getText();
    }
    }

    //Likewise I use a class LocL that implements ActionListener.

    //Now I want to use the values got, nameE and locE. I want to do some checks and further processing. I finally want to return both these form values to another class as a vector.However, I cannot understand how do I get these values here in the ManagerGUI class and how do I send them to another class?
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 50251
    79
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If your addListeners() method is in the ManagerGUI class, then you already have the values in the ManagerGUI class; as soon as you click those buttons, the ActionListeners put the values into your name String (etc). So your first problem is sorted out already.
    You don't say what processing you are doing, so I shall presume you already know how to do that.
    You could have a getNameVector() method, which returns your name Vector and similar for your location Vector.
    If, however, you don't have a way of getting the Manager class to call the getNameVector() method, you have a problem. I wondered how you are supposed to sort that out for a long time, but never actually had to do it until this week. Then I thought, why not pass the other class as an argument. What I mean is, you can have a object in the ManagerGUI class, and pass a Manager object as a parameter in the ManagerGui constructor. Then your ManagerGUI object can call a myManager.setNameVector() method . . .

    There may be other ways to do this; anybody else got any suggestions?

    CR
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic