Java event handler difficulties

Status
You're currently viewing only glenthas's posts. Click here to go back to viewing the entire thread.
Not open for further replies.

glenthas

Ars Tribunus Angusticlavius
6,979
Subscriptor++
It seems it's time for me to begin another round of Java questions. So without further ado...I'm trying to associate mouse listeners with dynamically created JButtons. The problem is I want the event handlers to affect the JButton that was clicked. I.E. when a button is clicked I want it to move 50 pixels higher on the screen.

When I try this I can't get around errors similar to "Cannot use non-final local variable from a different method." Here is what I believe to be the relevant portion of the code.

Code:
public void displayCard(Card aCard, int i)
	{
		 
		Insets i1 = new Insets(0, 0, 0, 0);
		ImageIcon cardImage = new ImageIcon(aCard.getGraphic());
		JButton button = new JButton(cardImage);
		button.setMargin(i1);
		centerPane.add(button);
		button.setSize(button.getPreferredSize());
		button.setLocation(225  + i*50, 475);
		aCard.setCard(button);
		aCard.getCard().addMouseListener(new MouseAdapter(){
			public void mousePressed(MouseEvent e) {
				aCard.getCard().setLocation(aCard.getCard().getX() - 50, aCard.getCard().getY() - 50);
			}
			}
						 );
		
	}

The setLocation, getX, getY are the problems.

Let me know if any additional information would be helpful.

Thanks,

Brian
 

glenthas

Ars Tribunus Angusticlavius
6,979
Subscriptor++
Nor sure if it will help, but here it is:

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class graphicsManager extends JFrame
{
	private JFrame f;
	private JPanel pane;
	private JPanel centerPane;
	public String playerName = null;
	public JTextField textBox = new JTextField();
	private JTextArea infoDisplay;
		
	
	public void infoDisplay(String input)
	{
		infoDisplay.setText(input);
	}
	
	
	private Component createPlayingField()
	{
		infoDisplay = new JTextArea();
		pane = new JPanel();
		pane.setLayout(new BorderLayout());
		pane.setBackground(Color.red);
		centerPane = new JPanel();
		centerPane.setBackground(Color.red);
		centerPane.setLayout(null);
		createMenus();
		pane.add(centerPane, BorderLayout.CENTER);
		pane.add(infoDisplay, BorderLayout.SOUTH);
		
		return pane;
	}
	
	private void createMenus()
	{
		JMenuBar menuBar;
		JMenu menu, menu2;
		JMenuItem menuItem, menuItem2, menu2Item;
		
		menuBar = new JMenuBar();
		
		menu = new JMenu("Game");
		menuBar.add(menu);
		menuItem = new JMenuItem("Options");
		menuItem2 = new JMenuItem("Score");
		menu.add(menuItem);
		menu.add(menuItem2);
		
		menu2 = new JMenu("Help");
		menuBar.add(menu2);
		menu2Item = new JMenuItem("How to Play");
		menu2.add(menu2Item);
		
		pane.add(menuBar, BorderLayout.NORTH);
	}
	
	public void getPlayerName()
	{
		
		f = new JFrame("Enter your name");
		JLabel label1 = new JLabel("Please enter your name: ");
		JButton nameButton = new JButton("OK");
		f.getContentPane().setLayout(new GridLayout(2,2));
		f.getContentPane().add(label1);
		f.getContentPane().add(nameButton);
		f.getContentPane().add(textBox);
		textBox.setSize(100,40);
		f.setSize(350,100);
		f.setVisible(true);
		nameButton.addMouseListener(new MouseAdapter(){
			public void mousePressed(MouseEvent e) {
				playerName = textBox.getText();
				f.setVisible(false);
				}
			}
						 );
	}
	
	public graphicsManager(String title)
	{
		super(title);
		setSize(800,800);
		Component contents = createPlayingField();
		getContentPane().add(contents);
		setVisible(true);
		addWindowListener(new WindowAdapter( ){
			public void windowClosing(WindowEvent we) {System.exit(0);}
		});
	}
	
	public void displayCard(Card aCard, int i)
	{
		 
		Insets i1 = new Insets(0, 0, 0, 0);
		ImageIcon cardImage = new ImageIcon(aCard.getGraphic());
		JButton button = new JButton(cardImage);
		button.setMargin(i1);
		centerPane.add(button);
		button.setSize(button.getPreferredSize());
		button.setLocation(225 + i*50, 475);
		aCard.setCard(button);
		aCard.getCard().addMouseListener(new MouseAdapter(){
			public void mousePressed(MouseEvent e) {
				aCard.getCard().setLocation(aCard.getCard().getX(), aCard.getCard().getY() - 50);
				
			}
			}
						 );
		
	}
		
}

The rest of the code can be found here for anyone interested.
Complete Java Code

Brian
 

glenthas

Ars Tribunus Angusticlavius
6,979
Subscriptor++
Another question:

Code:
aCard.getCard().addMouseListener(new MouseAdapter(){
			public void mousePressed(MouseEvent e) {
				JButton button = (JButton)e.getSource();
				button.setLocation(button.getX(), button.getY() - 50);
				
			}
			}
						 );

This let's me access the button, but how would I go about accessing aCard from the event handler? After the button is moved I need to invoke a method on the card it was associated with.

Thanks,

Brian
 
Status
You're currently viewing only glenthas's posts. Click here to go back to viewing the entire thread.
Not open for further replies.