*
The moose likes Swing / AWT / SWT and the fly likes One class calling another class incharge of GUI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "One class calling another class incharge of GUI" Watch "One class calling another class incharge of GUI" New topic
Author

One class calling another class incharge of GUI

Lucky Singh
Ranch Hand

Joined: Jan 19, 2004
Posts: 125
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

Joined: Oct 13, 2005
Posts: 38087
    
  22
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

    Joined: Oct 13, 2005
    Posts: 38087
        
      22
    Sorry, I forgot to say: you don't use the show() methdo any more.
    Lucky Singh
    Ranch Hand

    Joined: Jan 19, 2004
    Posts: 125
    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

    Joined: Oct 13, 2005
    Posts: 38087
        
      22
    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
     
     
    subject: One class calling another class incharge of GUI
     
    Similar Threads
    Odd effects with Swing - Button pressed changes screen layout
    how to obtain the name of an instance class
    How to make popup invisible
    Help me ..how I can add an action to the button
    creating a gui