Results 1 to 9 of 9
  1. #1
    mayhewj7 is offline Member
    Join Date
    Dec 2008
    Posts
    39
    Rep Power
    0

    Default Help with actionPerformed

    I am creating a GUI that sets a letter, row, column, and modify option.
    I have buttons for all 4 but im unsure of how to go out about setting the buttons to perform the action I want them too.

    the set letter button sets a specific row,
    the set column button sets a specific column,
    the set letter button sets a letter,
    and the modify button when allow the user to add another letter in a new row, column.

    here is my actionperformed method so far, if anyone could help me with how to finish this I would appreciate it. Thanks


    public void actionPerformed(ActionEvent e){
    if(rowButton == e.getSource())
    {

    }

    if(colButton == e.getSource())
    {

    }

    if(letButton == e.getSource())
    {

    }

    if(chartButton == e.getSource())
    {

    }

    if(modButton == e.getSource())
    {

    }

  2. #2
    imaginationac is offline Member
    Join Date
    Feb 2009
    Posts
    4
    Rep Power
    0

    Default

    You haven't provided much information, and your description on what you're trying to do isn't clear. What does "setting" a row and column mean? You have a good understanding on how to check an event source, so I suggest you try out some code beforehand.

    Also, in the future, including the entire source is much more helpful. Place code in tags for better readability (press the # button on the post editor).

  3. #3
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default

    IF I UNDERSTAND YOU CORRECTLY..

    I am guessing that you have these buttons
    displayed in your applet or application,
    and the code you have shown is what
    you think should produce your button's
    functions.

    So I assume you are asking, "What more do I
    do to get this puppy running?"

    FIRST, insert your code into a class that
    implements an actionListener. Name the
    class "ButtonProcessor" or "MatrixProcessor"
    or something appropriate.

    Here is what it COULD look like:

    class ButtonProcessor implements ActionListener{
    public void actionPerformed(ActionEvent e){
    if(rowButton == e.getSource()){

    }
    if(colButton == e.getSource()){

    }
    if(letButton == e.getSource()){

    }
    if(chartButton == e.getSource()){

    }
    if(modButton == e.getSource()){

    }
    }
    }

    THEN, for convieniance, place this inside the
    class that contains the button assignments
    (making it an inner class).


    FINALLY, go to your code, where you assigned the
    buttons and 'add' this ButtonProcessor object
    to each button, using ".addActionListener(new ButtonProcessor());"

    Here is what that COULD look like:

    rowButton = new Button("ROW"); // You have aready
    colButton = new Button("COL"); // created some code
    letButton = new Button("LET"); // something like
    charButton = new Button("CHAR"); // this.
    modButton = new Button("MOD"); //

    rowButton. addActionListener(new ButtonProcessor()); // I think
    colButton. addActionListener(new ButtonProcessor()); // this is
    letButton. addActionListener(new ButtonProcessor()); // what you
    charButton.addActionListener(new ButtonProcessor()); // are asking
    modButton. addActionListener(new ButtonProcessor()); // about.

    Oh yes, you should make sure this
    is at the top of your code:

    import java.awt.event.*;

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    You seem to be off to the right start, assuming you added your listener to each of the buttons.

    I would suggest avoiding the "monolithic method" approach. "Monolithic" means "one stone", as in a mountain that is one, giant rock. For programmers, that means don't create one method that does everything for everyone and fill it with "if" statements.

    Instead, create a separate listener for each button. This seems like more code, but, by the time you remove all the "if" statements, it's not. The advantage is that you can see what each one does clearly. The massive method may make perfect sense when you first write it, but after a few modifications, it's a mess.

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

    Default

    I like to group my listeners so that the same listener goes to buttons with similar functionality. For an example, if I were to create a calculator, I'd create one listener for the number buttons, a separate listener for the math operations buttons (i.e., "+", "-", ...), a separate listener for the memory modifying buttons, etc...

  6. #6
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    use...
    ...
    JButton button1=new JButton("I am here");
    button1.setActionCommand("I am Button 1");
    ...
    actionPerformed(ActionEvent e){
    String event=e.getActionCommand();
    if(event.equals("I am Button 1")){/*action*/}
    if(event.equals("I am Button 2"){}
    //...
    }
    Maybe that help. report
    Last edited by Webuser; 02-10-2009 at 07:56 AM.

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

    Default

    Quote Originally Posted by Webuser View Post
    use...

    Maybe that help. report
    I think that the general idea here is to avoid "switchboard" action listeners.

  8. #8
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    Fubarable makes a good point. My comment came across too dogmatic. There is always a balance.

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

    Default

    Sometimes a little Dogma is good (great movie btw). It gets even trickier though when the listener has to keep track of a state such as when doing a memory game app. Then it's probably more important that the buttons not only use the same listener class, but the same listener object. But now I'm digressing.

Similar Threads

  1. trouble with actionPerformed
    By diggitydoggz in forum New To Java
    Replies: 2
    Last Post: 12-26-2008, 03:18 AM
  2. Issue with Buttons and ActionPerformed
    By Deathmonger in forum Advanced Java
    Replies: 1
    Last Post: 04-17-2008, 09:47 AM
  3. Help with actionPerformed Statements
    By wco5002 in forum New To Java
    Replies: 8
    Last Post: 03-26-2008, 05:02 AM
  4. actionPerformed problem
    By tomitzel in forum New To Java
    Replies: 1
    Last Post: 01-08-2008, 07:10 PM
  5. Problems with jButton ActionPerformed
    By susan in forum AWT / Swing
    Replies: 3
    Last Post: 08-07-2007, 05:19 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
  •