Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / July 2005

Tip: Looking for answers? Try searching our database.

ActionListener

Thread view: 
pete - 19 Jul 2005 21:22 GMT
Hi!

I have implemented several classes. One for the gui and one for processing
the input in a TextField...
How can I pass a String from the gui class to the processing class when a
certain button is clicked?

I tried it like this:

[code]
class myGUI implements ActionListener{
...
button1 = new JButton("button1");
button1.addActionListener(this);
...

public void actionPerformed(ActionEvent e) {
       if ("button1".equals(e.getActionCommand())) {
        String temp = url.getText();

       //what must I do now??
}
}
[/code]

Thanks!
pete
Mathias Lichtner - 19 Jul 2005 21:49 GMT
> Hi!
>
[quoted text clipped - 23 lines]
> Thanks!
> pete

try something like this:

class myGUI implements ActionListener {

    private myProcessingClass;

    public myGUI() {
        // not needed if method(s) are static
        myProcessingClass = new myProcessingClass(...); }

    public void actionPerformed(...) {
        tmp = url.getText();
        myProcessingClass.doThisAndThatWithString(temp);
    }

}

or did i misunderstand your problem?!
pete - 20 Jul 2005 11:37 GMT
> try something like this:
>
[quoted text clipped - 14 lines]
>
> or did i misunderstand your problem?!

No you did not. I incorrectly expressed what I wanted. Sorry!
If I do it your way, then I have to implement my main method in the MyGui
class?
Actually I wanted to separate my code into the following classes: gui,
processing, main...
Isn´t it possible to do it like that? Then I would have a main class with
instances of gui
and processing, to which I pass an instance of the main class. And then I
install the
ActionListener for MyGui in MyMain...

[code]
class MyMain() implements ActionListener{
...
MyGUI window = new MyGUI(this);
MyProcessing pross = new MyProcessing(this);
...
public void actionPerformed(ActionEvent e) {
       if ("button1".equals(e.getActionCommand())) {

       // how can I receive the string in a textfield?
        // this doesn´t work...
       String temp = (Textfield)
(MyGUI-instance.getComponent(1)).getText();

       MyProcessing-instance.DoSomething(temp);
}

}

class MyGUI{

public MyGUI(MyMain xxx){
MyMain instance = xxx;
}
...
Jbutton button1 = new JButton("button1");
button1.addActionListener(instance);
...
}
[/code]

Thanks!
pete
Mathias Lichtner - 20 Jul 2005 14:04 GMT
>>try something like this:
>>
[quoted text clipped - 18 lines]
> If I do it your way, then I have to implement my main method in the MyGui
> class?

No, no need to do that?!

> Actually I wanted to separate my code into the following classes: gui,
> processing, main...
[quoted text clipped - 3 lines]
> install the
> ActionListener for MyGui in MyMain...

[...]

The ActionListener for your GUI should not be inside the code of
your main as you wanted to seperated main from gui.
A common way to solve your problem is something like this: (nearly
pseudo-code)

class main {

main-method {
    // create gui and start/show it
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    gui = new GUI();
    gui.setVisible(true); }
    });
}
}

imho it is not a good way to let your main gui class implement
ActionListener. Either you add anonymous classes for your listeners
or (when suitable) you create (a) new class file(s) for your listener(s)

class gui{

private JTextField myText;
private JButton doSomething;

// constructor
gui
{ // create gui elements

    [...]

 // register actionListeners
 doSomething.addActionListener(new ActionListener() {

    // you don't need that ugly if-clause (if "button1".equals....)
    // this listeners
    // actionPerformed-method is only run when the user
    // pushes the button
     public void actionPerformed(ActionEvent e) {
        myProcessClass proc = new myProcessClass();
        result = proc.processIt(myText.getText());
        // display/doSomething with results
    }

    });
}

Now, none of your classes needs a reference to another and everything
should work fine. Probably this is not the best way to do something like
that, but it's one of the nicer ways ;-)

hth


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.