If you need some examples of action listener code, use the forum's Search for addActionListener. There are lots of code examples on threads in the forum.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
If you need some examples of action listener code, use the forum's Search for addActionListener. There are lots of code examples on threads in the forum.
If you don't understand my answer, don't ignore it, ask a question.
Norm could you please be more exact or give parts of the code i need.
Did you do the search I recommended? Did you find some examples of how to call the addActionListener() method? Did you try to copy any of the code into your program?
Do you have any questions about any of the code samples you found?
BTW I don't provide much code that can be copied and pasted into a student's program. I expect the student to attempt to write the code. I will help the student get their code to work.
If you don't understand my answer, don't ignore it, ask a question.
i have enter the following code ;
btBody.addActionListener(new ActionListener() { @Override /**HandleItemEvent*/ public void actionPerformed(ActionEvent e) { } }); }
however it says that btBody cannot be found as an symbol.
That's strange, I see btBody define here:for (int iCol = 1; iCol<4; iCol++) { num = 3*(iRow-1) + iCol; JButton btBody = new JButton("" + num); //<< Define btBody btBody.setBackground(Color.WHITE); btBody.setForeground(Color.BLUE); p2.add(btBody); }
Where did you add the code?
If you don't understand my answer, don't ignore it, ask a question.
i added here;
public class EX_7_2 extends JFrame { public EX_7_2() { setLayout(new BorderLayout(5, 10)); // create p1 JPanel p1 = new JPanel(); p1.setLayout(new BorderLayout(5, 10)); p1.add(new JTextField(), BorderLayout.NORTH); JPanel p3 = new JPanel(); p3.setLayout(new GridLayout(1, 3, 5, 5)); JButton btBackspace = new JButton ("Backspace"); btBackspace.setBackground(Color.WHITE); btBackspace.setForeground(Color.RED); JButton btCE = new JButton ("CE"); btCE.setBackground(Color.WHITE); btCE.setForeground(Color.RED); JButton btC = new JButton ("C"); btC.setBackground(Color.WHITE); btC.setForeground(Color.RED); p3.add(btBackspace); p3.add(btCE); p3.add(btC); p1.add(p3, BorderLayout.CENTER); // create p4 JPanel p2 = new JPanel(); p2.setLayout(new GridLayout(4, 3, 5, 5)); int num; for (int iRow = 3; iRow>0; iRow--) { for (int iCol = 1; iCol<4; iCol++) { num = 3*(iRow-1) + iCol; JButton btBody = new JButton("" + num); btBody.setBackground(Color.WHITE); btBody.setForeground(Color.BLUE); p2.add(btBody); } } JButton btZero = new JButton ("0"); btZero.setBackground(Color.WHITE); btZero.setForeground(Color.BLUE); JButton btPlusMinus = new JButton ("+/-"); btPlusMinus.setBackground(Color.WHITE); btPlusMinus.setForeground(Color.BLUE); JButton btDot = new JButton ("."); btDot.setBackground(Color.WHITE); btDot.setForeground(Color.BLUE); p2.add(btZero); p2.add(btPlusMinus); p2.add(btDot); JPanel p5 = new JPanel(); p5.setLayout(new GridLayout(4, 2, 5, 5)); JButton btBack = new JButton ("/"); btBack.setBackground(Color.WHITE); btBack.setForeground(Color.RED); JButton btsprt = new JButton ("sprt"); btsprt.setBackground(Color.WHITE); btsprt.setForeground(Color.BLUE); JButton btStar = new JButton ("*"); btStar.setBackground(Color.WHITE); btStar.setForeground(Color.RED); JButton btPercentage = new JButton ("%"); btPercentage.setBackground(Color.WHITE); btPercentage.setForeground(Color.BLUE); JButton btMinus = new JButton ("-"); btMinus.setBackground(Color.WHITE); btMinus.setForeground(Color.RED); JButton btOnex = new JButton ("1/x"); btOnex.setBackground(Color.WHITE); btOnex.setForeground(Color.BLUE); JButton btPlus = new JButton ("+"); btPlus.setBackground(Color.WHITE); btPlus.setForeground(Color.RED); JButton btEquals = new JButton ("="); btEquals.setBackground(Color.WHITE); btEquals.setForeground(Color.RED); p5.add(btBack); p5.add(btsprt); p5.add(btStar); p5.add(btPercentage); p5.add(btMinus); p5.add(btOnex); p5.add(btPlus); p5.add(btEquals); JPanel p4 = new JPanel(); p4.setLayout(new BorderLayout(5, 10)); p4.add(p2, BorderLayout.CENTER); p4.add(p5, BorderLayout.EAST); add(p1, BorderLayout.NORTH ); add(p4, BorderLayout.CENTER); btBody.addActionListener(new ActionListener() { @Override /**HandleItemEvent*/ public void actionPerformed(ActionEvent e) { } }); } public static void main(String[] args) { EX_7_2 frame = new EX_7_2(); frame.setTitle("Calculator"); frame.setSize(350, 375); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true);
The call to the btBody's addActionListener method must be made where the btBody variable is in scope.
In scope means within the same enclosing {}s. A variable defined inside of a {} pair is NOT in scope outside of those {}.
There needs to be a call made to the addActionListener method for ALL of the buttons that are created in the loop. If there is only one call to the method after the loop is finished, only ONE of the buttons will have a listener(the last one created), none of the other buttons will have a listener.
If you don't understand my answer, don't ignore it, ask a question.
how can i make all of them have a listener, can you give me an example from some code please.
Move the code you added that adds a listener to btBody to inside the loop where the btBody objects are created.how can i make all of them have a listener
For testing add a println() statement inside of the listener method that prints out the value of the ActionEvent object that is passed to the method.
If you don't understand my answer, don't ignore it, ask a question.
i have moved the code but how do i use make the buttons work so that they show up. do i have to use the println() method; if so could you provide an example.
What happens when you press a button?
What is the code supposed to do when a button is pressed? When you have decided on what is supposed to happen then work on the code for it.
If you don't understand my answer, don't ignore it, ask a question.
it a calculator the number are to show up in the text field.
and to answer the first question the button do nothing.
Did you add the println() I suggested? What it prints will show the number that is on the face of the button. There is a JButton class method that will return the String that is shown on the button. Use that to get the String so it can be used later.the button do nothing.
If you don't understand my answer, don't ignore it, ask a question.
i don't know how to en-corporate the println method with the actionlistener could please give the code
Post the code you tried and the error message you get from it.
If you don't understand my answer, don't ignore it, ask a question.
i don't get an error but the button don't work
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class EX_7_2 extends JFrame { public EX_7_2() { setLayout(new BorderLayout(5, 10)); // create p1 JPanel p1 = new JPanel(); p1.setLayout(new BorderLayout(5, 10)); p1.add(new JTextField(), BorderLayout.NORTH); JPanel p3 = new JPanel(); p3.setLayout(new GridLayout(1, 3, 5, 5)); JButton btBackspace = new JButton ("Backspace"); btBackspace.setBackground(Color.WHITE); btBackspace.setForeground(Color.RED); JButton btCE = new JButton ("CE"); btCE.setBackground(Color.WHITE); btCE.setForeground(Color.RED); JButton btC = new JButton ("C"); btC.setBackground(Color.WHITE); btC.setForeground(Color.RED); p3.add(btBackspace); p3.add(btCE); p3.add(btC); p1.add(p3, BorderLayout.CENTER); // create p4 JPanel p2 = new JPanel(); p2.setLayout(new GridLayout(4, 3, 5, 5)); int num; for (int iRow = 3; iRow>0; iRow--) { for (int iCol = 1; iCol<4; iCol++) { num = 3*(iRow-1) + iCol; JButton btBody = new JButton("" + num); btBody.setBackground(Color.WHITE); btBody.setForeground(Color.BLUE); p2.add(btBody); btBody.addActionListener(new ActionListener() { @Override /**HandleItemEvent*/ public void actionPerformed(ActionEvent e) { } }); } } JButton btZero = new JButton ("0"); btZero.setBackground(Color.WHITE); btZero.setForeground(Color.BLUE); JButton btPlusMinus = new JButton ("+/-"); btPlusMinus.setBackground(Color.WHITE); btPlusMinus.setForeground(Color.BLUE); JButton btDot = new JButton ("."); btDot.setBackground(Color.WHITE); btDot.setForeground(Color.BLUE); p2.add(btZero); p2.add(btPlusMinus); p2.add(btDot); JPanel p5 = new JPanel(); p5.setLayout(new GridLayout(4, 2, 5, 5)); JButton btBack = new JButton ("/"); btBack.setBackground(Color.WHITE); btBack.setForeground(Color.RED); JButton btsprt = new JButton ("sprt"); btsprt.setBackground(Color.WHITE); btsprt.setForeground(Color.BLUE); JButton btStar = new JButton ("*"); btStar.setBackground(Color.WHITE); btStar.setForeground(Color.RED); JButton btPercentage = new JButton ("%"); btPercentage.setBackground(Color.WHITE); btPercentage.setForeground(Color.BLUE); JButton btMinus = new JButton ("-"); btMinus.setBackground(Color.WHITE); btMinus.setForeground(Color.RED); JButton btOnex = new JButton ("1/x"); btOnex.setBackground(Color.WHITE); btOnex.setForeground(Color.BLUE); JButton btPlus = new JButton ("+"); btPlus.setBackground(Color.WHITE); btPlus.setForeground(Color.RED); JButton btEquals = new JButton ("="); btEquals.setBackground(Color.WHITE); btEquals.setForeground(Color.RED); p5.add(btBack); p5.add(btsprt); p5.add(btStar); p5.add(btPercentage); p5.add(btMinus); p5.add(btOnex); p5.add(btPlus); p5.add(btEquals); JPanel p4 = new JPanel(); p4.setLayout(new BorderLayout(5, 10)); p4.add(p2, BorderLayout.CENTER); p4.add(p5, BorderLayout.EAST); add(p1, BorderLayout.NORTH ); add(p4, BorderLayout.CENTER); } public static void main(String[] args) { EX_7_2 frame = new EX_7_2(); frame.setTitle("Calculator"); frame.setSize(350, 375); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
The action listener could be called, but there is no code in the actionlistener method. It is empty.
Add a call to println() that prints out the value of the ActionEvent object (e) that is passed to the listener method.
If you don't understand my answer, don't ignore it, ask a question.
what code do i use could you give an example please. i m new to java and using the method of actionlisteners, i have just hear of them today.
Strange you don't know how to code a println statement. Here is an example:System.out.println( PUT THE STRINGS, ETC HERE THAT YOU WANT PRINTED );
If you don't understand my answer, don't ignore it, ask a question.
i have try that but the button don't war could give a code which will active the the button form the number within the nested for loop.
Post the code so I can see what the problem is.but the button don't war
If you don't understand my answer, don't ignore it, ask a question.
can you please give me the code to fill in the actionlistener as i can't seem to get the button working.
import java.awt.*; import javax.swing.*; import java.awt.event.*; public class EX_7_2 extends JFrame { public EX_7_2() { setLayout(new BorderLayout(5, 10)); // create p1 JPanel p1 = new JPanel(); p1.setLayout(new BorderLayout(5, 10)); p1.add(new JTextField(), BorderLayout.NORTH); JPanel p3 = new JPanel(); p3.setLayout(new GridLayout(1, 3, 5, 5)); JButton btBackspace = new JButton ("Backspace"); btBackspace.setBackground(Color.WHITE); btBackspace.setForeground(Color.RED); JButton btCE = new JButton ("CE"); btCE.setBackground(Color.WHITE); btCE.setForeground(Color.RED); JButton btC = new JButton ("C"); btC.setBackground(Color.WHITE); btC.setForeground(Color.RED); p3.add(btBackspace); p3.add(btCE); p3.add(btC); p1.add(p3, BorderLayout.CENTER); // create p4 JPanel p2 = new JPanel(); p2.setLayout(new GridLayout(4, 3, 5, 5)); int num; for (int iRow = 3; iRow>0; iRow--) { for (int iCol = 1; iCol<4; iCol++) { num = 3*(iRow-1) + iCol; final JButton btBody = new JButton("" + num); btBody.setBackground(Color.WHITE); btBody.setForeground(Color.BLUE); p2.add(btBody); btBody.addActionListener(new ActionListener() { @Override /**HandleItemEvent*/ public void actionPerformed(ActionEvent e) { } }); } } JButton btZero = new JButton ("0"); btZero.setBackground(Color.WHITE); btZero.setForeground(Color.BLUE); JButton btPlusMinus = new JButton ("+/-"); btPlusMinus.setBackground(Color.WHITE); btPlusMinus.setForeground(Color.BLUE); JButton btDot = new JButton ("."); btDot.setBackground(Color.WHITE); btDot.setForeground(Color.BLUE); p2.add(btZero); p2.add(btPlusMinus); p2.add(btDot); JPanel p5 = new JPanel(); p5.setLayout(new GridLayout(4, 2, 5, 5)); JButton btBack = new JButton ("/"); btBack.setBackground(Color.WHITE); btBack.setForeground(Color.RED); JButton btsprt = new JButton ("sprt"); btsprt.setBackground(Color.WHITE); btsprt.setForeground(Color.BLUE); JButton btStar = new JButton ("*"); btStar.setBackground(Color.WHITE); btStar.setForeground(Color.RED); JButton btPercentage = new JButton ("%"); btPercentage.setBackground(Color.WHITE); btPercentage.setForeground(Color.BLUE); JButton btMinus = new JButton ("-"); btMinus.setBackground(Color.WHITE); btMinus.setForeground(Color.RED); JButton btOnex = new JButton ("1/x"); btOnex.setBackground(Color.WHITE); btOnex.setForeground(Color.BLUE); JButton btPlus = new JButton ("+"); btPlus.setBackground(Color.WHITE); btPlus.setForeground(Color.RED); JButton btEquals = new JButton ("="); btEquals.setBackground(Color.WHITE); btEquals.setForeground(Color.RED); p5.add(btBack); p5.add(btsprt); p5.add(btStar); p5.add(btPercentage); p5.add(btMinus); p5.add(btOnex); p5.add(btPlus); p5.add(btEquals); JPanel p4 = new JPanel(); p4.setLayout(new BorderLayout(5, 10)); p4.add(p2, BorderLayout.CENTER); p4.add(p5, BorderLayout.EAST); add(p1, BorderLayout.NORTH ); add(p4, BorderLayout.CENTER); } public static void main(String[] args) { EX_7_2 frame = new EX_7_2(); frame.setTitle("Calculator"); frame.setSize(350, 375); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
@Override /**HandleItemEvent*/ public void actionPerformed(ActionEvent e) { // <<<<<<<<<<< THE PRINTLN CODE GOES HERE <<<<<<<< }
Look at the tutorial on how to write a method:
http://docs.oracle.com/javase/tutori...O/methods.html
If you don't understand my answer, don't ignore it, ask a question.
what are the string i need to put in the code
To see what is passed to the listener method, print the value of the ActionEvent object: e
I'm done for tonight. I'll be back tomorrow.
If you don't understand my answer, don't ignore it, ask a question.