Results 1 to 3 of 3

Thread: GUI's

  1. #1
    diggitydoggz is offline Member
    Join Date
    Dec 2008
    Posts
    55
    Rep Power
    0

    Default GUI's

    Hey guys. I've recently started learning about GUI's, action events, and that whole shindig... I've been doing exercises out of my textbook. I recently got an exercise where I have to make a GUI with a text field and two buttons, one called upper case and one called lower case. When I press the upper case one it's supposed to make whatever I type in the text field turn upper case, and vice versa for the lower version. What I'm confused about is how exactly to write these two action listeners.

    Every example that I've seen in the book embeds the action listener in the client program. However I don't see how I'd have an actionPerformed method for both the upper and lower case buttons inside the client program so I'd probably have to make two separate classes.

    Now what confuses me about that is that the actionPerformed method, as far as I understand, is supposed to have something like textField.setText(textField.getField().toUpperCase ()) or vice versa for the lower case one. How would I be able to edit the textField from the client program through the separate action listener though??

    Also, another question: one of the answer solutions to one of the exercises uses an action listener that is a private class that is INSIDE the public class that creates the GUI. How do these private classes work and what are their strengths/limitations? I never even knew these existed and the book never spoke of them.

    Thanks a lot - knowing how to do use multiple action listeners to edit the components' fields will make the rest of the exercises a lot easier.

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    The private classes inside your public classes are called nested inner classes. These are very useful for listeners in particular(I have limited experience with them so google for more info)

    As for your listeners. Attach them directly to the buttons.
    Java Code:
    toLowerCaseButton.addSelectionListener(new SelectionAdapter() {
    	public void widgetSelected(SelectionEvent event) {
    //
    //	put your code in here to get the text and change the text
    //
    	}
    });

    Now what confuses me about that is that the actionPerformed method, as far as I understand, is supposed to have something like textField.setText(textField.getField().toUpperCase ()) or vice versa for the lower case one. How would I be able to edit the textField from the client program through the separate action listener though??
    The same way you would do it in the original. Just place that code inside the actionPerformed method of your selectionListener(I believe actionPerformed and WidgetSelected are the same thing in the code I posted)
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  3. #3
    diggitydoggz is offline Member
    Join Date
    Dec 2008
    Posts
    55
    Rep Power
    0

    Default

    Thanks for the reply.

    The book doesn't talk about selectionListener or widgetSelected though. Can I replace these with actionListener and actionPerformed?

Similar Threads

  1. Creating GUI's for simulators.
    By Modifier in forum New To Java
    Replies: 3
    Last Post: 11-20-2008, 01:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •