How To Use ActionListeners

Apr 27, 2014

I'm trying to learn how to use ActionListeners. I want a button to hold a int and everytime it's pressed, the int gets + 2. Basically the button is a product and everytime it's pressed, you buy it, the int is your total, at least for that button. I want to have several buttons, each a different price, so maybe button 2 is +3 everytime. At the end then I want to add all the ints together for a total cost and then write that to a file. The only thing is I'm having trouble.

Here's my code

import javax.swing.*;
import java.awt.*;
public class Shop
{
public static void main(String [] args)
{
Frame frame = new Frame();
Button button = new Button(frame.getPanel());

[code]...

The problem is in my Button class, I think it's because my actionPerformed is set to void, but when I change it to int, it says my class must be abstract. For the moment I only have the info going to a label, this is just for testing. Whenever I run it, it says Juice is set to 0, probably because like I said earlier, it's set to void.

View Replies


ADVERTISEMENT

Swing/AWT/SWT :: ActionListeners In A Different Class From GUI?

Jul 15, 2014

I have 3 classes:

1 class which creates an instance of my GUI class class and initializes it which works fine.

1 class with all GUI stuff and another class which I'm trying to add all my actionsListeners to (which I can't seem to figure out).

Here is my GUI class:
public class GUI extends JFrame{
Container container;
CardLayout cl;
public GUI(){
super("Game Finder");

[Code] .....

Using my new class "AllActionListeners" I want it to have the bulk of the actionListener code, in order to make the program more manageable. However, after looking around I don't seem to be any closer to a solution. How to move the action listeners to a different class?

View Replies View Related

Swing/AWT/SWT :: Adding ActionListeners To JButtons

Mar 11, 2009

I have an application that reads through a ResultSet... while I'm reading that ResultSet, I am creating new JButtons..... Now I need to add an ActionListener to those JButtons...... but they are not visible to the actionPerformed method. So I thought I'd try to add an ActionListener as I created the buttons with the following code:

final File file = new File(rs.getString(3));
SKPictureButton spb = new SKPictureButton(url,file.getName());
spb.addActionListener (
new ActionListener() {
public void actionPerformed( ActionEvent e )

[Code] ....

However this seems a bit excessive to me..... I don't think I really want to be creating a new ActionListener class for each JButton do I ? Is there a better way to make the JButtons created in this method visible to the actionPerformed method ? I'm just gettin' back into the "Swing" of things in Java.... haven't coded it for a while.

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved