Jbutton event handler not working properly - java

rollDice.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//generate roll
int roll = dice.getRoll();
addGameFeedMessage(players[1].getName() + " rolled " + roll);
//store the player's position before the roll
int currentPlayerPos = players[1].getPosition();
//update player's position
players[1].movePlayer(roll);
//move the player icon to the player's position
tiles[players[1].getPosition()].addIconCurrentPlayersPanel(players[1].getPlayerIcon());
//revalidate components
//tiles[currentPlayerPos].getCurrentPlayersPanel().revalidate();
//tiles[players[1].getPosition()].getCurrentPlayersPanel().revalidate();
//this loop was put in to see if doing revalidate() on all panels would make a difference but the problem is still there
for(int i = 0; i < tiles.length; i++)
{
tiles[i].getCurrentPlayersPanel().revalidate();
}
}
});
Can anyone explain why the for loop in the actionListener for roll dice doesn't seem to work yet the for loop just above it which is the exact same thing works? There's no actual error as such it just doesn't do anything, however it does get inside the actionListener and executes as we've put print statements etc in it.
Guessing its something strange about actionListeners but I'm not sure what.
Any assistance would be appreciated, thankyou.

You should call validate() after the for loop as follows:
for(int i = 0; i < getNumOfPlayers(); i++)
{
currentPlayersOnTile[5].add(players[i].getPlayerIcon());
}
validate();
For Example consider the code given below:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class DemoPanels extends JFrame
{
public void createAndShowGUI()
{
final JPanel panel = new JPanel();
getContentPane().add(panel);
JButton button = new JButton("Click");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
for (int i = 0 ; i < 10 ; i++ )
{
JPanel panel1 = new JPanel();
panel1.add(new JButton(String.valueOf(i)));
panel.add(panel1);
}
validate();//comment this line and then compile and execute the code to see the effect
}
});
getContentPane().add(button,BorderLayout.SOUTH);
setVisible(true);
setSize(300,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
DemoPanels dp = new DemoPanels();
dp.createAndShowGUI();
}
});
}
}
NOTE: comment the code validate() and then compile and execute it to see the effect.

Related

Java Swing: 'java.awt.AWTEventMulticaster.mouseMoved' error

I have been trying to create a 'catch me if you can' game: when I start it, it randomly chooses where to allocate a 'click me' button. I am not supposed to be able to click the button, the text should be re-assigned to another button before I am able to do that.
It works for a while but then it throws the following error: "java.awt.AWTEventMulticaster.mouseMoved".
I have been trying to fix the problem with removeListener() method but I don't seem to be able to find a solution. Any comments?
Here's my code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.lang.*;
public class Game extends JFrame {
//Panels
private JPanel mainPanel = new JPanel();
// Buttons
private JButton[] buttons = new JButton[9];
private JButton theChosenButton = new JButton();
// other
private int random = 0;
public Game() {
this.setTitle("Catch me if you can");
mainPanel.setLayout(new GridLayout(3, 3));
// creates buttons
for(int i = 0; i < 9 ; i++) {
buttons[i] = new JButton();
mainPanel.add(buttons[i]);
}
// Add everything to frame
this.getContentPane().add(mainPanel);
this.setSize(400, 400);
this.setVisible(true);
}
// generates random number between 1 and 9 to be used
public int clickMeGenerator(){
random = (int) Math.floor(Math.random() * 9);
return random;
}
// randomly assigns clickMeGenerator to a button
// add mouseMoved listener to the chosen button
public void assign(){
int randomButton = this.clickMeGenerator();
theChosenButton = buttons[randomButton];
theChosenButton.addMouseMotionListener(new MouseHover());
theChosenButton.setText("Click me");
}
public void removeListener() {
theChosenButton.removeMouseMotionListener(new MouseHover());
//}
}
// inner class
class MouseHover implements MouseMotionListener {
public void mouseMoved(MouseEvent e) {
theChosenButton.setText("");
Game.this.assign();
}
public void mouseDragged(MouseEvent e) {
}
}
} // end of class
Test class:
public class GameTest {
public static void main (String args[]) {
Game myGame = new Game();
myGame.assign();
}
}
Thank you so much for your help!
Just for clarity, the "actual" error is ...
Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at java.desktop/java.awt.AWTEventMulticaster.mouseMoved(AWTEventMulticaster.java:337)
at java.desktop/java.awt.AWTEventMulticaster.mouseMoved(AWTEventMulticaster.java:337)
So looking through the code...
public void assign() {
int randomButton = this.clickMeGenerator();
theChosenButton = buttons[randomButton];
theChosenButton.addMouseMotionListener(new MouseHover());
theChosenButton.setText("Click me");
}
You are repeatedly add a new MouseMotionListener to you buttons, over and over again, and...
public void removeListener() {
theChosenButton.removeMouseMotionListener(new MouseHover());
//}
}
is pointless, as you're trying to remove a new instance of MouseHover from the button, but it will never have been applied in the first place.
The first thing I would do is create an instance of MouseHover as an instance field in Game
private MouseHover mouseHover = new MouseHover();
and use it when calling addMouseMotionListener and removeMouseMotionListener.
I would then, remove the listener from the "currently" active button before adding it to the next one.
Personally, I would do this in the assign method
public void assign() {
int randomButton = this.clickMeGenerator();
if (theChosenButton != null) {
theChosenButton.removeMouseMotionListener(mouseHover);
}
theChosenButton = buttons[randomButton];
theChosenButton.addMouseMotionListener(mouseHover);
theChosenButton.setText("Click me");
}
I would also ensure that assign is called from within the Event Dispatching Thread when the class is first created, as the UI has been realised by the end of the constructor of Game, meaning the first call to assign is outside of the context of the EDT, which is not recommended.
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
Game myGame = new Game();
myGame.assign();
}
});
}

How do you get user input from a button using ActionListener? Java [duplicate]

I want to count the number of times the button is clicked using GUI.
I did this code:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
int clicked = 0;
clicked++;
System.out.println(clicked);
}
But it showing the output "1", each time I click the button.
I want every time I click the button to show me the count.
ex: If I click the button two times it should give me output of "2".
You are resetting the counter every time you click, because you have defined the variable inside the action method. Try not doing that.
int clicked = 0; // move this outside
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
// int clicked = 0; -- this resets it to 0 each time
clicked++;
System.out.println(clicked);
}
You've declared clicked as a local variable, initialised to 0, it can never be anything else but 1
Make clicked a class level variable instead...
private int clicked = 0;
//...
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
clicked++;
System.out.println(clicked);
}
Try below code
int clicked = 0;
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
clicked++;
System.out.println(clicked);
}
Change
int clicked = 0;
to be a member of your class. This way it wont be set to zero every time you press the button.
You have declared count variable inside the ActionListener. Declare it outside the block.
int clicked = 0; //make it as your class member.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
clicked++;
System.out.println(clicked);
}
Depending on how you are instantiating this class you need to declare the clicked variable at either the field level or the class variable level. Currently, the scope of the clicked variable is local to the method.
Option 1
int clicked = 0;
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
clicked++;
System.out.println(clicked);
}
Option 2
static int clicked = 0;
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
clicked++;
System.out.println(clicked);
}
The option you use will depend on instantiation. The second option should be avoided if possible.
every time jButton1ActionPerformed fires, the clicked variables gets instantiated back to 0 that's why it is always giving you a value of 1. You should move the clicked variable outside of that method
//Somewhere in your class
private intClicked = 0;
//More methods here.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
clicked++;
System.out.println(clicked);
}
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Threading extends JFrame {
private JPanel contentPane;
private JTextField DisplayOne;
private JTextField DisplayTwo;
int count;
int count1;
int count2;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run()
{
try {
Threading frame = new Threading();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Threading()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
DisplayOne = new JTextField();
panel.add(DisplayOne);
DisplayOne.setColumns(10);
DisplayTwo = new JTextField();
panel.add(DisplayTwo);
DisplayTwo.setColumns(10);
JButton btnCountOne = new JButton("Count 1");
count1=0;
btnCountOne.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
DisplayOne.setText(Integer.toString(count1++));
}
});
panel.add(btnCountOne);
JButton btnCountTwo = new JButton("Count 2");
count2=0;
btnCountTwo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
DisplayTwo.setText(Integer.toString(count2++));
}
});
panel.add(btnCountTwo);
JButton btnCountBoth = new JButton("Count Both");
count=0;
btnCountBoth.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
while (count < 10)
{
DisplayOne.setText(Integer.toString(++count));
DisplayTwo.setText(Integer.toString(count));
}
}
});
panel.add(btnCountBoth);
}
}

Program not overwriting variable

I have a program which uses 3 radiobuttons to switch between 3 incrementing values for a counter, here time.
I want to change status when a radiobutton is pressed, and it does so, but only for a fraction. When launching the program will keep printing
0
Normal
2
Normal
4
Normal
6
etc. When I press the button slow it prints CHANGE Slow once but keeps incrementing with 2 and still prints Normal every time.
How can I have this permenently switch to a different value for status, and a different increment, until I choose another radiobutton again?
package daynightcycle;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
/**
* Day/night cycle with visuals. Adjustable speed and time inserts.
* Optional date or daycounter later
* #author rogie
*/
public class DayNightCycle extends JFrame implements Runnable{
//JFrame entities
private JPanel animationPanel;
public JRadioButton button;
public JRadioButton button2;
public JRadioButton button3;
public int time = 0;
public String status = "Normal";
public static void main(String[] args) {
DayNightCycle frame = new DayNightCycle();
frame.setSize(2000, 1300);
frame.setLocation(1000,350);
frame.createGUI();
frame.setVisible(true);
frame.setTitle("Day/Night Cycle, Rogier");
(new Thread(new DayNightCycle())).start();
}
private void createGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new FlowLayout() );
animationPanel = new JPanel();
animationPanel.setPreferredSize(new Dimension(2000, 900));
animationPanel.setBackground(Color.black);
window.add(animationPanel);
JRadioButton option1 = new JRadioButton("Slow");
JRadioButton option2 = new JRadioButton("Normal", true);
JRadioButton option3 = new JRadioButton("Fast");
option1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.out.println("CHANGE");
status = "Slow";
System.out.println(status);
}
});
option2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
status = "Normal";
}
});
option2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
status = "Fast";
}
});
//option2.setFont(new java.awt.Font("Tahoma", Font.BOLD, 30));
//option2.putClientProperty("JComponent.sizeVariant", "huge"); //doesn't work
ButtonGroup group = new ButtonGroup();
group.add(option1);
group.add(option2);
group.add(option3);
add(option1);
add(option2);
add(option3);
pack();
}
public void run() {
while(true){
System.out.println(time);
System.out.println(status);
try
{
Thread.sleep(500);
if (status.equals("Slow")) {
time += 1;
}
else if (status.equals("Normal")){
time += 2;
}
else {
time += 3;
}
}
catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
}
}
}
}
You are creating to DayNightCycle-Objects, the first shows the GUI and the second prints on the console.
Change the line
(new Thread(new DayNightCycle())).start();
to
(new Thread(frame)).start();
public static void main(String[] args) {
final DayNightCycle frame = new DayNightCycle();
frame.setSize(2000, 1300);
frame.setLocation(1000,350);
frame.createGUI();
frame.setTitle("Day/Night Cycle, Rogier");
And then
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
frame.setVisible(true);
}
});
Or in java 8:
EventQueue.invokeLater(() -> frame.setVisible(true));
}
You in effect created a second DayNightCycle.

How to capture the amount of clicks over a group of loop generated JToggleButtons?

I have 20 loop-generated JToggleButtons and I need to count how many of them are active.
private void generarBotones(){
JToggleButton b;
this.panelCuerpo.setLayout(new GridLayout(4,5));
for(int i = 1; i<=20; i++){
b = new JToggleButton();
b.setText(String.valueOf(i));
this.panelCuerpo.add(b);
b.addActionListener(new ActionListener() {
int clicks = 0;
#Override
public void actionPerformed(ActionEvent ae2){
clicks = clicks + 1;
System.out.println(clicks);
}
public void setCantidadBoletas(int clicks){
cantidadBoletas = clicks;
}
});
}
}
The problem here is that it counts how many times is EACH ONE of them clicked instead of count how many of them are selected.
PS. I tried to use (b.isSelected()) but b needs to be final to access it so it wasn't the solution.
If you declare the JToggleButton inside the loop, you can make it final:
for (int i = 1; i<=20; i++) {
JToggleButton b = new JToggleButton();
Then you can use b.isSelected:
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (b.isSelected())
clicks++;
else
clicks--;
}
});
}
clicks would have to be a class variable.
Suggestions:
Create a field, JToggleButton[] toggleButtons = new JToggleButton[20]
Or use an ArrayList if you so choose
In your for loop create your JToggleButton and assign it to the proper array item.
In the ActionListener simply iterate through the array, counting how many of its JToggleButton items are selected.
You're done.
Create a class attribute that will count the selected toggles:
private int selectedCount;
Initialize the counter to 0 in your constructor:
this.selectedCount = 0;
Increment or decrement the counter every time the state of a toggle changes:
b.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent ev) {
if (ev.getStateChange() == ItemEvent.SELECTED){
YourClass.this.selectedCount++;
} else if (ev.getStateChange() == ItemEvent.DESELECTED){
YourClass.this.selectedCount--;
}
System.out.println(YourClass.this.selectedCount);
}
});
There are many ways to get this done and the best way depends on the rest of your code. I tried to keep it as close to yours.
You can just declare the buttons as final inside the loop and keep a global count of the number of buttons selected, which will be modified in the ActionListener:
public class ButtonsCount extends JFrame {
int clicks = 0;
ButtonsCount() {
JLabel label = new JLabel("0");
JPanel buttonsPanel = new JPanel(new GridLayout(4,5));
for(int i = 1; i <= 20; i++) {
final JToggleButton b = new JToggleButton();
b.setText(String.valueOf(i));
buttonsPanel.add(b);
b.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae2){
if (b.isSelected())
label.setText(String.valueOf(++clicks));
else
label.setText(String.valueOf(--clicks));
}
});
}
add(buttonsPanel);
add(label, BorderLayout.PAGE_END);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new ButtonsCount();
}
}

Card Game Graphics: Updating Vector of Canvas Objects in a Panel

So I am creating the card game Gin Rumy. I am working on the graphics, specifically, creating a panel that updates so that it always displays the vector of Card objects in the player's hand. The Cards themselves are a component that extends the canvas class. What I am asking is how to create some function updatePanel() that updates the panel as the player's hand (the vector of Card objects) changes. All I can think of is to remove the old panel from the GUI frame, create a new panel, fill that new panel with the Cards in the player's hand, and then stick that new panel onto the GUI frame. I haven't even coded this because I worry that taking a component off of the GUI frame and then slapping it back on runs the risk of disrupting the layout, and my gut tells me that creating a new panel every time is just a bad idea. So I'm asking for a better plan.
You'll also notice that the selectButton's actionPerformed() method is incomplete. What I'm trying to do is remove the cards that have focus. I understand how focus and action events work, but what I don't understand is how to search through the panel and find the card's that are selected and remove them.
I'd greatly appreciate any help on on either the updatePanel() or selectButton's actionPerformed() method, or any other useful information about adding and removing objects from panels throughout the course of a program.
// GraphicGinRumy
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Vector;
import java.util.Random;
public class GraphicGinRumy extends GUIFrame {
protected Card card;
protected Button selectButton, newGameButton;
protected Panel controlPanel, playerPanel;
protected RandomCardDeck deck;
protected Vector<Card> playerHand;
//Pardon this weird indentation...the code didn't copy to stack overflow correctly
public GraphicGinRumy() {
super("Gin Rumy");
deck = new RandomCardDeck();
playerHand = new Vector<Card>();
MouseListener ml = new MouseAdapter() {
public void mousePressed(MouseEvent e) {
Card selectedCard = (Card) e.getSource();
if (selectedCard.getFocused()) {
selectedCard.setFocused(false);
} else {
selectedCard.setFocused(true);
}
}
};
deck.shuffle();
rumyDeal();
//Player Panel
playerPanel = new Panel();
for (int c = 0; c < playerHand.size(); c++) {
playerPanel.add(playerHand.elementAt(c));
}
add(playerPanel, BorderLayout.CENTER);
//Control Panel
controlPanel = new Panel();
selectButton = new Button("Select");
selectButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
controlPanel.add(selectButton);
newGameButton = new Button("New Game");
newGameButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deck.shuffle();
deck.deal();
updatePanel();
}
});
controlPanel.add(newGameButton);
add(controlPanel, BorderLayout.SOUTH);
setSize(500, 100);
setVisible(true);
//__________________________________________________________________
//Game Setup
//__________________________________________________________________
//Set card values:
//2 to 9 is worth 5 pts; 10 to K is worth 10 pts; A is worth 15 pts
for (int c = 0; c < deck.getNumCards(); c++) {
card = deck.getCard(c);
if (card.getFaceValue() < 10) {
card.setValue(5);
} else if (card.getFaceValue() < Card.ACE) {
card.setValue(10);
} else {
card.setValue(15);
}
}
}
public static void main(String args[]) {
GraphicGinRumy rumy = new GraphicGinRumy();
//rumy.play();
}
protected void rumyDeal() {
//Give player and CPU 7 cards and add 1 to the discardPile
Card card;
int numCards = 7;
for (int i = 0; i < numCards; i++) {
playerHand.add(deck.deal());
}
for (int i = 0; i < numCards; i++) {
card = deck.deal();
card.setVisible(true);
cpuHand.add(card);
}
discardPile.add(deck.deal());
}
protected void updatePanel() {
playerPanel = new Panel();
}
}

Categories