Results 1 to 9 of 9
Thread: Help with actionPerformed
- 02-09-2009, 03:06 PM #1
Member
- Join Date
- Dec 2008
- Posts
- 39
- Rep Power
- 0
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())
{
}
- 02-09-2009, 08:54 PM #2
Member
- Join Date
- Feb 2009
- Posts
- 4
- Rep Power
- 0
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).
- 02-09-2009, 10:14 PM #3
Senior Member
- Join Date
- Jul 2008
- Posts
- 125
- Rep Power
- 0
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.*;
- 02-09-2009, 11:22 PM #4
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.
-
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...
- 02-10-2009, 06:52 AM #6
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
use...
Maybe that help. report...
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"){}
//...
}Last edited by Webuser; 02-10-2009 at 06:56 AM.
-
- 02-10-2009, 02:38 PM #8
Fubarable makes a good point. My comment came across too dogmatic. There is always a balance.
-
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
-
trouble with actionPerformed
By diggitydoggz in forum New To JavaReplies: 2Last Post: 12-26-2008, 02:18 AM -
Issue with Buttons and ActionPerformed
By Deathmonger in forum Advanced JavaReplies: 1Last Post: 04-17-2008, 08:47 AM -
Help with actionPerformed Statements
By wco5002 in forum New To JavaReplies: 8Last Post: 03-26-2008, 04:02 AM -
actionPerformed problem
By tomitzel in forum New To JavaReplies: 1Last Post: 01-08-2008, 06:10 PM -
Problems with jButton ActionPerformed
By susan in forum AWT / SwingReplies: 3Last Post: 08-07-2007, 04:19 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks