Results 1 to 5 of 5
Like Tree2Likes
  • 1 Post By Fubarable
  • 1 Post By LaughingSeraph

Thread: Buttons, listeners etc

  1. #1
    Join Date
    Jan 2013
    Posts
    24
    Rep Power
    0

    Default Buttons, listeners etc

    Hello,

    So at the moment my classes had me use my first button. Yay?

    The interface raises the whole ActionEvent() method.

    A couple of things. and I am just asking to be pointed to reading material or examples.

    I determined that getActionCommand returns the label of a button.
    //--short questions--//
    How do I position buttons?
    Is there another way to to determine which button was pressed without performing logic on the caption? Something similar in nature to Button1.Click(), Button2.Click() from VC,VB etc ?
    //-Not even sure hwo to explain my troubles-//
    lastly, this one is hard to explain.
    Is there a way to drawString(), drawImage() etc from outside of paint? As in, can I declare a Graphics g in another function to pass it down to a method, and have THAT method paint to the main context define(apparently) by AWT in paint?

    so like this...

    public void function1()
    {
    Graphics g = new Graphics();
    g.drawString("Etc.",50,200,this.getImage());//or whatever would return that image context to a function
    }

    I ask this because it seems odd to call repaint() , when I am using a function to explore the raised events from getActionCommand(). This would allow me to check stuff without jamming everything into paint() so that I could see stuff when a button is clicked. Does that make sense? Am I just completely off my rocker with the methodology or logic in Java? For me this is confusing as it is the simplest thing in certain languages, and is apparently a miracle to perform in java... :(

    I don't even know where to start looking to produce the equivalent to This->Form1.text1.Text = getActionCommand();
    Does java have a way to print to the little applet window like cout in c++ or print in Basic?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Buttons, listeners etc

    Quote Originally Posted by LaughingSeraph View Post
    The interface raises the whole ActionEvent() method.
    I am unaware of such a method.

    How do I position buttons?
    This is a specific version of the general question of how to layout components in a container. And the key is in learning about and using layout managers. Please Google and check out the tutorial with that name -- Laying out Components in a Container.

    Is there another way to to determine which button was pressed without performing logic on the caption? Something similar in nature to Button1.Click(), Button2.Click() from VC,VB etc ?
    I think that most of us who use Swing a lot would use either anonymous ActionListeners -- small classes created on the spot for each button, or AbstractActions -- reusable classes that also implement the ActionListener interface. You can read about using Actions in the same Swing tutorials referred to above.

    lastly, this one is hard to explain.
    Is there a way to drawString(), drawImage() etc from outside of paint? As in, can I declare a Graphics g in another function to pass it down to a method, and have THAT method paint to the main context define(apparently) by AWT in paint?

    so like this...
    Java Code:
    public void function1()
    {
      Graphics g = new Graphics();
      g.drawString("Etc.",50,200,this.getImage());//or whatever would return that image context to a function
    }
    You can draw directly to a BufferedImage in such a way, but you'll still want to display that image in a JPanel's paintComponent(...) override method, in other words in a "passive" way. It is a mind set that will take some getting used to, but the more you do it, the more you'll appreciate it.

    I ask this because it seems odd to call repaint() , when I am using a function to explore the raised events from getActionCommand(). This would allow me to check stuff without jamming everything into paint() so that I could see stuff when a button is clicked. Does that make sense? Am I just completely off my rocker with the methodology or logic in Java? For me this is confusing as it is the simplest thing in certain languages, and is apparently a miracle to perform in java... :(
    You may need to tell us more specifics. I am not sure what problems you might be having here.

    I don't even know where to start looking to produce the equivalent to This->Form1.text1.Text = getActionCommand();
    Does java have a way to print to the little applet window like cout in c++ or print in Basic?
    You're better off not trying to shoehorn C++ or Basic techniques into your Java code. They approach things differently, and you will want to learn how to do thing the Java way to be able to use it best.
    DarrylBurke likes this.

  3. #3
    Join Date
    Jan 2013
    Posts
    24
    Rep Power
    0

    Default Re: Buttons, listeners etc

    Thanks!
    I will explore that stuff and come back to it.

    I am not trying to shoehorn methodologies in for the record, I just don't know how else to explain what it was I was trying to accomplish.
    I am basically just looking for a way to easily print out the returns from these 'interfaces' such as getCommandAction() without having to go back into the paint method. I am new enough, that I don't quit understand what is happening there.
    For instance, todays lesson I used that button and teh getCommandAction() returned was the buttons label String from its declaration. There is ALOT of stuff in the api documentation about listeners.So I was just trying to find out what each list, button, radiobutton etc was returned with that call.

    Anyway, thanks. It looks like that googel search and the resulting tutorial has a great deal of information to digest.

  4. #4
    Join Date
    Jan 2013
    Posts
    24
    Rep Power
    0

    Default Re: Buttons, listeners etc

    System.println and System.out appear to be what I was looking for. I will check them out tomorrow and mark this as solved if they work.

    Yeah, that set of tutorials in pretty solid.

  5. #5
    Join Date
    Jan 2013
    Posts
    24
    Rep Power
    0

    Default <Solved>Re: Buttons, listeners etc

    The tutorials and methods mentioned did what I needed.
    Fubarable likes this.

Similar Threads

  1. Key Listeners
    By wired-in=p in forum New To Java
    Replies: 17
    Last Post: 09-17-2011, 09:27 PM
  2. two different Listeners
    By Billaguana in forum New To Java
    Replies: 8
    Last Post: 01-21-2011, 04:20 AM
  3. Replies: 8
    Last Post: 08-10-2010, 03:59 AM
  4. Help with Listeners
    By Psyclone in forum AWT / Swing
    Replies: 8
    Last Post: 02-09-2010, 08:21 PM
  5. Seriously need help on my listeners!!
    By themburu in forum Java Applets
    Replies: 4
    Last Post: 05-26-2008, 11:41 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
  •