When i use mouseListener and I chek for a middle mouse button it does not react properly I don't know why but it looks like i need to scroll while cliking to get the event to occur
some part of my code if it helps
public void mouseClicked(MouseEvent e) {
if(new Rectangle(0,0,1274,30).contains(Screen.mse)){
TopMenu.click();
}else if(new Rectangle(0,31,1100,549).contains(Screen.mse)){
Map.cliked(e.getButton(),0);
System.out.println("mouse:"+e.getButton());
}else if(new Rectangle(1100,30,174,550).contains(Screen.mse)){
//cliked ModeMenu
}else if(new Rectangle(0,580,1100,164).contains(Screen.mse)){
//cliked ToolsMenu
}else{
//cliked mode change
}
switch(e.getModifiers()) {
case InputEvent.BUTTON1_MASK: {
System.out.println("That's the LEFT button");
break;
}
case InputEvent.BUTTON2_MASK: {
System.out.println("That's the MIDDLE button");
break;
}
case InputEvent.BUTTON3_MASK: {
System.out.println("That's the RIGHT button");
break;
}
}
}
If you look at the javadoxs for MouseEvent, you can see that BUTTON1, BUTTON2 and BUTTON3 are not referred to the left, middle and right mouse buttons. It depends on the mouse what BUTTON 1,2 and 3 mean, so it can happen that BUTTON2 does not refer to the middle Button. To see if the middle Button of your mouse is recognized correctly, try the following:
public void mouseClicked(MouseEvent e){
System.out.println(e.getButton());
}
Now press your middle mouse button. If there is no output in the console, your mouse has no middle button (or it is not recognized properly). If there is an output, it corresponds to the button(1=BUTTON1,2=BUTTON2,3=BUTTON3). If the ouput is 0, then the button is MouseEvent.NOBUTTON, which is unlikely to happen.
Another thing: Try using SwingUtilities.isMiddleButton(MouseEvent e). This may fix some problems with your mouse. If you do so, chage your mouseClicked() method to
public void mouseClicked(MouseEvent e)
{
if(SwingUtilities.isLeftMouseButton(e))
{
System.out.println("That's the LEFT button");
}
else if(SwingUtilities.isMiddleMouseButton(e))
{
System.out.println("That's the MIDDLE button");
}
else if(SwingUtilities.isRightMouseButton(e))
{
System.out.println("That's the RIGHT button");
}
}
(of course with all the other code you wrote above the original switch statement)
Related
I need to start off with a JLabel, for example a JLabel that reads,"Old Text". With a button click, I want to update that JLabel to, "Updated Text". I am able to do this with the code I posted below, but my issue is I want to be able to click the button again to go back to "Old Text" and so on. Basically, I need the button to allow me to alternate between those two texts but I can't get that to work.
// this doesn't switch back to "Old Code"
public void actionPerformed(ActionEvent event) {
if(event.getSource() == jbutton)
jlabel.setText("Updated Text");
if(event.getSource() == jbutton)
jlabel.setText("Old Text");
}
This works but isn't what I fully need because it only changes the JLabel once.
public void actionPerformed(ActionEvent event) {
if(event.getSource() == jbutton)
jlabel.setText("Updated Text");
}
The reason your code isn't work is, both if statements evaluate to true, so both get executed
There are several ways you might do this, probably the simplest is just to inspect the state of the text of the label and make some decision about what to do, for example...
public void actionPerformed(ActionEvent event) { if(event.getSource() == jbutton) jlabel.setText("Updated Text");
if(event.getSource() == jbutton) {
if (!jlabel.getText().equals("Old Text")) {
jlabel.setText("Old Text");
} else {
jlabel.setText("Updated text");
}
}
}
Now, if you wanted to be "really" fancy, you could use a little bit of modular maths...
private int trigger = 0;
#Override
public void actionPerformed(ActionEvent e) {
trigger++;
if ((trigger % 2) == 0) {
label.setText("Old text");
} else {
label.setText("Updated text");
}
}
Novice programmer here trying to make a tic tac toe GUI game. I'm stuck with my program though. I'm not sure how to place a check on hitting the same square twice. I was thinking an if statement inside my actionListener that said
if(button clicked = True)
{
JOptionPane.showMessageDialog((null, "ERROR", "Button already used.
Please hit again to change back", JOptionPane.ERROR_MESSAGE);
// STOP something along those lines
}
else
{
//Do nothing
}
would work but I can't get the program to work properly. I tried newTurn.getmodel().isPressed() and that didn't work and now with my current code the program outputs the error message after each move and the changes still appear on the board. Here's my code for this method. Any help is appreciated.
private class buttonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JButton newTurn = (JButton)e.getSource(); //get the particular button that was clicked
if(switchMove%2 == 0)
newTurn.setText("X");
else
newTurn.setText("O");
if(newTurn.isEnabled())
JOptionPane.showMessageDialog(null, "ERROR", "Button already used. Please hit again to change back", JOptionPane.ERROR_MESSAGE);
if(checkForWin() == true)
{
JOptionPane.showConfirmDialog(null, "Game Over.");
resetButtons();
}
switchMove++;
}
switch move is just an int set to 0 so evens are X and O's are odd. My if (newTurn.isEnabled()) is my issue
Here is my resolved code.
public void resetButtons()
{
for(int i = 0; i <= 8; i++)
{
buttons[i].setText("");
buttons[i].setEnabled(true);
}
}
private class buttonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JButton newTurn = (JButton)e.getSource();
if(switchMove%2 == 0)
newTurn.setText("X");
else
newTurn.setText("O");
if(newTurn.isEnabled())
newTurn.setEnabled(false);
if(checkForWin() == true)
{
JOptionPane.showConfirmDialog(null, "Game Over.");
resetButtons();
}
switchMove++;
}
In the actionPerformed() method I set the buttons to setEnabled(false) after a button was clicked. Then when a game is over the buttons that were previously disabled will be set to setEnabled(true) via the resetButtons method.
I have a JButton[][] array that stores every button on a grid.
What I want to do is :
1) click the jbutton(icon) that I want to move on the grid.
2) click on the jbutton that I want the previous selected jbutton(icon) to move to.
private class BListener implements MouseListener {
#Override
public void mouseClicked(MouseEvent e) {
JButton but = ((JButton) e.getSource());
if(iconSelected && !but.equals(selectedButton)){ // move(swap) buttons
but.setIcon(selectedButton.getIcon());
selectedButton.setBorder(BorderFactory.createLineBorder(Color.black));
selectedButton.setName(null);
selectedButton=but;
iconSelected=false;
}else if(!iconSelected && but.getName()!=null){
iconSelected=true;
selectedButton=but;
but.setBorder(BorderFactory.createLineBorder(Color.YELLOW,3));
}else{
if(iconSelected){
System.out.println("Already Selected");
}else{
System.out.println("Not selected");
}
}
}
I have tried some things that didnt work ( this moves the icon but the icon also remains at the starting location). Any insight would be helpfull.
That is because you never change the selectedButton's icon, Try this:
if(iconSelected && !but.equals(selectedButton)){ // move(swap) buttons
Icon bIcon = but.getIcon();
but.setIcon(selectedButton.getIcon());
selectedButton.setIcon(bIcon);
...
}
I'm writing a simple paint program with Java. As all paint applications there are buttons for brushTool, sprayTool, sprayTool... This tools have their own class which extends to MouseAdapter. They are working as they should. However, the problem starts when I choose a tool after choose another tool, both buttons and their ActionListeners keep executing and they do what they are written for at the same time. I mean if I choose lineTool(which draws straight line) with rectangleTool I hava a diagonal too. here is example of my two button. What I'm tring to do is stop the current action when I click another button. Can you guys help me
brushBotton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
pen = new PenTool(mainDrawArea);
mainDrawArea.addMouseListener(pen);
mainDrawArea.addMouseMotionListener(pen);
}
});
rectangleButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
shapeToolbar.setVisible(false);
rect = new RectangleTool(mainDrawArea);
rect.setStrokeSize(strokeInt);
mainDrawArea.addMouseListener(rect);
mainDrawArea.addMouseMotionListener(rect);
}
});
You can't keep adding a MouseListener to the drawing area every time you click a button.
Instead you need to keep track of the current MouseListener. Then when you click a button you need to:
remove the current MouseListener
add the new MouseListener
I would replace the button action listener for a set of Toggle Buttons in a group
https://docs.oracle.com/javase/tutorial/uiswing/components/buttongroup.html
Then you move everything in a single mouse listener.
public void mousePressed(MouseEvent e) {
this.drawingState = !this.drawingState
if ( isRightCLick(e) ) resetAllPendingOperation();
if (drawingState) {
this.startPoint = getPointFromEvent(e);
switch(toolbarGetCurrentTool()) {
case "line":
registerMouseLineListener(startPoint);//here you draw live preview
break
case "rectangle":
registerMouseRectangleListener(startPoint); //here you draw live preview
break;
}
} else {
//user clicked the second time, commit changes
//same switch as above
this.endPoint = getPointFromEvent(e);
switch(toolbarGetCurrentTool()) {
case "line":
commitLine(startPoint, endpoint);//here you draw live preview
break
case "rectangle":
commitRectangle(startPoint, endpoint); //here you draw live preview
break;
}
}
}
You are currently binding the listeners to the mainDrawArea, not setting an action for each individual button.
Note that the codes you write within actionPerformed() for each button's actionListener is the action you want to trigger everytime that button is clicked. You do not want to add a new listener to the mainDrawArea everytime we click the buttons.
You can a create a state for your current action, for example:
brushBotton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
state = BRUSH;
}
});
lineBotton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
state = LINE;
}
});
state can be an integer and BRUSH and LINE are constant such as 0 and 1.
Then in the listener (for the mainDrawArea), check the current state
switch (state){
case BRUSH: //trigger action needed for brushing;
break;
case LINE: //trigger action needed for drawing line;
break;
}
I am making a game on Java and i am doing a character selection menu. Within that menu i have the characters and if the user clicks on certain character a JOptionPane.showMessageDialog appears shows the stats of the character. So my question is if the person clicks "ok" which is automatically created when using the function how to i get that to select the character?
JButton chuck = new JButton(new ImageIcon("8bitChuckNorris.jpg"));//this part of program runs this if user picks lizard
chuck.setSize(210,175); //sets size of button
chuck.setLocation(300,317); //sets location of button
chuck.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent e) {
JOptionPane.showMessageDialog(null, "\t\tSTATS\nAttack\ndefence\nspecial");
}
});
The simplest method would probably be to provide a method that the ActionListener can call after the
the option pane is closed, for example...
chuck.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent e) {
JOptionPane.showMessageDialog(null, "\t\t STATS\nAttack\ndefence\nspecial");
characterSelected("chuck"); // Pass whatever you need to identify the character
}
});
Updated
It would be easier to use JOptionPane.showConfirmDialog as it will actually return a int result representing the option that the user selected (or -1 if they dismissed the dialog without selecting anything)
switch (JOptionPane.showConfirmDialog(null, "\t\t STATS\nAttack\ndefence\nspecial", "Character", JOptionPane.OK_CANCEL_OPTION)) {
case JOptionPane.OK:
// Process selection...
break;
}