Pressing both mouse buttons not working correctly - java

i have to check if i pressed left, right or both buttons on my mouse, here is a sample code:
package sandbox;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Sandbox extends JFrame {
public static void main(String[] args) {
Sandbox s = new Sandbox();
s.setVisible(true);
}
public Sandbox() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300, 200);
setResizable(false);
setTitle("Frame");
setLayout(null);
JButton but = new JButton("click me");
but.setBounds(0, 0, 120, 50);
but.addMouseListener(new MouseListener() {
#Override
public void mouseClicked(MouseEvent e) {
}
#Override
public void mousePressed(MouseEvent e) {
if(SwingUtilities.isLeftMouseButton(e) | SwingUtilities.isRightMouseButton(e))
System.out.println("both");
else if(SwingUtilities.isLeftMouseButton(e))
System.out.println("left");
else if(SwingUtilities.isRightMouseButton(e))
System.out.println("right");
}
#Override
public void mouseReleased(MouseEvent e) {
}
#Override
public void mouseEntered(MouseEvent e) {
}
#Override
public void mouseExited(MouseEvent e) {
}
});
add(but);
}
}
i have to make it using SwingUtilities, but there is a problem if i press left or right single button it will print "both", not "left" or "right", if i used MouseEvent class it was working properly but i need it with SwingUtilities class if it's possible, thanks.
EDIT: using MouseEvent class it's working right:
if(e.getModifiersEx() == (MouseEvent.BUTTON1_DOWN_MASK | MouseEvent.BUTTON3_DOWN_MASK))
...

Don't check using OR (|) but use AND (& or better the short cut version &&) instead:
if(SwingUtilities.isLeftMouseButton(e) && SwingUtilities.isRightMouseButton(e))
...
Update:
For checking if both buttons are pressed, SwingUtilities doesn't seem to be the right tool. The isXxxxMouseButton(...) methods do the same as you do, they check the button masks, but use different masks which seem not be set when both buttons are pressed.
As an example, isRightMouseButton() is implemented as follows:
public static boolean isRightMouseButton(MouseEvent anEvent) {
return ((anEvent.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK);
}
As you can see, the implementation isn't that complext, so you might be better off to just do the mask check yourself.

mouse listene event on button
but.addMouseListener(new MouseListener() {
mouse listener event on fame.
this.addMouseListener(new MouseListener() {
Please check with '||' not '|', don't use the && because it is not possible to press both buttons at the same time
if(SwingUtilities.isLeftMouseButton(e) || SwingUtilities.isRightMouseButton(e))
You will get result
both
left
both
left
both
right

Related

Move jcomponent to back without using JLayeredPane

I have written a DragAndDrop MouseListener.
Component A is a "background" image that is behind Component B. Both are located on a JPanel.
I have made the image draggable. However, I want the image to remain behind component B as I drag it.
However, every time I drag the image, I suppose Java gives it focus or something, so it gets brought to the forefront.
Is there a method that can keep the image in the back even as I am dragging it?
I know I can use a JLayeredPane and use the moveToBack method every time I drag, but I would rather not use a JLayeredPane and just use a JPanel. Is there a moveToBack equivalent for JPanel?
Or is there a way to make the component preserve the current layer (maybe "not gain focus") so that I can drag it within its current layer?
HERE IS AN EXAMPLE
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class OverlapTester extends JFrame {
public static final long serialVersionUID = 172L;
public static void main(String[] args) {
OverlapTester frame = new OverlapTester();
frame.initialize();
}
public void initialize() {
setLayout(null);
JButton bottom = new JButton("bottom");
JButton top = new JButton("top");
bottom.setBounds(0,0,100,100);
top.setBounds(0,0,50,50);
add(top);
add(bottom);
int bottomZOrder = 0;
bottom.addMouseListener(new MouseListener(){
#Override
public void mouseEntered(MouseEvent e) {
e.getComponent().getParent().setComponentZOrder(e.getComponent(), bottomZOrder);
}
#Override
public void mouseExited(MouseEvent e) {
e.getComponent().getParent().setComponentZOrder(e.getComponent(), bottomZOrder);
}
#Override
public void mouseReleased(MouseEvent e) {
e.getComponent().getParent().setComponentZOrder(e.getComponent(), bottomZOrder);
}
#Override
public void mousePressed(MouseEvent e) {
e.getComponent().getParent().setComponentZOrder(e.getComponent(), bottomZOrder);
}
#Override
public void mouseClicked(MouseEvent e) {
e.getComponent().getParent().setComponentZOrder(e.getComponent(), bottomZOrder);
}
});
bottom.addMouseMotionListener(new MouseMotionListener(){
#Override
public void mouseDragged(MouseEvent e) {
e.getComponent().getParent().setComponentZOrder(e.getComponent(), bottomZOrder);
}
#Override
public void mouseMoved(MouseEvent e) {
e.getComponent().getParent().setComponentZOrder(e.getComponent(), bottomZOrder);
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setExtendedState(MAXIMIZED_BOTH);
setVisible(true);
}
}
Component A is a "background" image that is behind Component B. Both are located on a JPanel.
Swing components are painted based on their ZOrder. The highest ZOrder is painted first.
The ZOrder is assigned as a component is added to the panel. So the first component added is given ZOrder 0, and the second component ZOrder 1.
So add your "background" image to the panel last and it will always have the highest ZOrder which means it is painted first, so other components will be painted on top of it.
For example:
panel.add(someOtherComponent);
panel.add(background);
Or you can use the:
Container.setComponentZOrder(...)
method to change the ZOrder dynamically after a component has been added.
Edit:
The answer I provided was you was an either/or solution. You only need to choose one approach.
You implemented the first approach correctly, which is to add the "background" component last. So there is no need to then play with the ZOrder. This approach works great when ZOrder will remain fixed.
The second approach is when you want the ZOrder to dynamically change, say when you have multiple components and you want to change it as you interact with each component. However you implemented this approach incorrectly. You used "0" for the ZOrder, which means the component will always be painted last and therefore on on top of any other component.
In your current example there is no need to change the ZOrder dynamically, so you can remove all related code.
As you can see the bottom button is consistently being added to the forefront
Swing painting is optimized to assume components don't overlap.
However, in the case of a JButton, it has automatic repaint logic when you hover over the button to repaint the Border. So when the button is repainted it still paints over top of the other component.
So you need to tell Swing to NOT optimize the painting. Therefore when one component is repainted they will all be repainted when they overlap. You do this by overriding the isOptimizedDrawingEnabled(...) method as demonstrated below:
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JButton;
import javax.swing.*;
public class OverlapTester extends JFrame {
public static void main(String[] args) {
OverlapTester frame = new OverlapTester();
frame.initialize();
}
public void initialize() {
JPanel contentPane = new JPanel(null)
{
#Override
public boolean isOptimizedDrawingEnabled()
{
return false;
}
};
add(contentPane);
// setLayout(null);
JButton bottom = new JButton("bottom");
JButton top = new JButton("top");
bottom.setBounds(0,0,100,100);
top.setBounds(0,0,50,50);
// add(top);
// add(bottom);
contentPane.add(top);
contentPane.add(bottom);
// int bottomZOrder = 0;
int bottomZOrder = 1;
bottom.addMouseListener(new MouseListener(){
#Override
public void mouseEntered(MouseEvent e) {
e.getComponent().getParent().setComponentZOrder(e.getComponent(), bottomZOrder);
}
#Override
public void mouseExited(MouseEvent e) {
e.getComponent().getParent().setComponentZOrder(e.getComponent(), bottomZOrder);
}
#Override
public void mouseReleased(MouseEvent e) {
e.getComponent().getParent().setComponentZOrder(e.getComponent(), bottomZOrder);
}
#Override
public void mousePressed(MouseEvent e) {
e.getComponent().getParent().setComponentZOrder(e.getComponent(), bottomZOrder);
}
#Override
public void mouseClicked(MouseEvent e) {
e.getComponent().getParent().setComponentZOrder(e.getComponent(), bottomZOrder);
}
});
bottom.addMouseMotionListener(new MouseMotionListener(){
#Override
public void mouseDragged(MouseEvent e) {
e.getComponent().getParent().setComponentZOrder(e.getComponent(), bottomZOrder);
}
#Override
public void mouseMoved(MouseEvent e) {
e.getComponent().getParent().setComponentZOrder(e.getComponent(), bottomZOrder);
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setExtendedState(MAXIMIZED_BOTH);
setVisible(true);
}
}

cannot always load the picture on click and hover

I am making a launcher application, i wish to change the button depending on the action taken, right now i want to change the button when the following actions happen, either the player hovers over the image, clicks on the image, releases the image click or exits the hover. The issue i have is that it doesn't always load correctly, which is odd.
package LostStory;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.SystemTray;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
#SuppressWarnings("serial")
public class LaunchPanel extends JComponent {
Image image;
JButton play;
SystemTray tray;
ImageIcon icon = new ImageIcon("res/images/buttons/playNonHover.png");
ImageIcon iconHover = new ImageIcon("res/images/buttons/playHover.png");
ImageIcon iconClick = new ImageIcon("res/images/buttons/playClick.png");
public LaunchPanel(Image img) {
this.image = img;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
init();
}
public void init() {
play = new JButton("Play Lost Story", icon);
play.setBounds(85, 210 - 75, getWidth() - 165, 50);
play.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
Runtime run = Runtime.getRuntime();
try {
run.exec("notepad");
if (SystemTray.isSupported()) {
tray = SystemTray.getSystemTray();
play.setIcon(play.getIcon());
// Main.getClient().getJFrame().setVisible(false);
// tray.add(img);
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
play.addMouseListener(new MouseListener() {
#Override
public void mouseClicked(MouseEvent arg0) {
} // no use
#Override
public void mouseEntered(MouseEvent e) {
if (iconHover.getImageLoadStatus() == 8) {
play.setIcon(iconHover);
play.setIcon(play.getIcon());
System.out.println("Entered");
}
System.out.println(iconHover.getImageLoadStatus());
} // use
#Override
public void mouseExited(MouseEvent arg0) {
if (icon.getImageLoadStatus() == 8) {
play.setIcon(icon);
play.setIcon(play.getIcon());
System.out.println("Exited");
}
System.out.println(icon.getImageLoadStatus());
} // use
#Override
public void mousePressed(MouseEvent arg0) {
if (iconClick.getImageLoadStatus() == 8) {
play.setIcon(iconClick);
play.setIcon(play.getIcon());
System.out.println("Pressed");
}
System.out.println(iconClick.getImageLoadStatus());
} // use
#Override
public void mouseReleased(MouseEvent arg0) {
if (icon.getImageLoadStatus() == 8) {
play.setIcon(icon);
play.setIcon(play.getIcon());
System.out.println("Released");
}
System.out.println(icon.getImageLoadStatus());
} // use
});
add(play);
}
}
The first button always loads (because it is the default one) it returned an '8' when i tried using the IconImage.getImageLoadStatus() so i expected that to be '8' if it was succesfully loaded. (This is not the case though, it will always return 8 as far as i know).
So TL;DR: How do i make sure the ImageIcons are always loaded and will apply correctly?
Don't try to manipulate the icon using the setIcon() method.
A JButton has methods to set the icon for various states setRolloverIcon, setPressedIcon, etc.
Read the API for other properties.
Also, a painting method is for painting only. Never create components from a painting method. Get rid of the init() method from your paintComponent() method.
Components should be created and added to the panel in the constructor of the panel.

How to react on special mouse keys in Java (forward/backward keys)

I´m using KeyBindings and MouseListener to react on keyboard/mouse inputs. Many mouses have a back/forward button (for example to go a page back and forth in a webbrowser). How can I react on these buttons in Java?
Special mouse keys are usually bound by the mouse's vendor software to virtual keystrokes. Try implementing a KeyListener, set a breakpoint inside the keyPressed method, debug and watch which keyCode do you get when you press a special button on your mouse. This way, you would likely also handle special keys on keyboards which often also provide the same functionality.
Just implement a MouseListener and have a look at the mousePressed() event.
Quick and dirty program to test mouse buttons:
package stackoverflowanswer;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class MouseListenerApp{
public static void main(String[] args) {
Runnable r = new Runnable(){
#Override
public void run() {
JFrame frame = new JFrame("mouselistener");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addMouseListener(new MouseListener() {
#Override
public void mouseClicked(MouseEvent e) {
}
#Override
public void mousePressed(MouseEvent e) {
System.out.println(e.getButton());
}
#Override
public void mouseReleased(MouseEvent e) {
}
#Override
public void mouseEntered(MouseEvent e) {
}
#Override
public void mouseExited(MouseEvent e) {
}
});
frame.setSize(200,200);
frame.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
For me forward/backward corresponds to key 4/5.

Return to previous control when pressing BACKSPACE

I have looked at many sites and did not get a result. I have some JTextFields that only accept one character. What I need is that when I press BACKSPACE that character is cleared and the focus returns to the previous JTextField and so on. I added a KeyListener to all JTextFields and try KeyTyped, KeyReleased and KeyPressed events but none worked. The code below:
jt.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent evt) {
char c = evt.getKeyChar();
if(Character.isLetter(c) || Character.isISOControl(c))
{
evt = evt;
} else
evt.consume();
}
public void keyReleased(KeyEvent evt) {
if(evt.getKeyChar() == KeyEvent.VK_BACK_SPACE)
{
robot.keyPress(KeyEvent.VK_BACK_SPACE);
robot.keyRelease(KeyEvent.VK_BACK_SPACE);
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_SHIFT);
}
}
});
Thanks for any help.
Don't use a KeyListener. Swing was designed to be used with Key Bindings.
Don't use the Robot. The API has a method that allows you to transfer focus.
Here is the basic code for the Key Binding:
textField.getInputMap().put(KeyStroke.getKeyStroke("BACK_SPACE"), "backspace");
textField.getActionMap().put("backspace", new AbstractAction()
{
#Override
public void actionPerformed(ActionEvent e)
{
Component c = (Component)e.getSource();
c.transferFocusBackward();
}
});
I have some JTextFields that only accept one character.
You may also want to look at Text Field Auto Tab. It is a class that will allow you to automatically tab to the next text field when a character is entered.
I've quickly made a small app that hopefully solved your problem, here's the entire code:
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import javax.swing.GroupLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class BackspaceTest extends JFrame implements KeyListener {
// In this example I've used 4 Text Fields, each of which takes 1 character
private JTextField text1, text2, text3, text4;
// I've stored the Text Fields in a ArrayList so we can easily shuffle trough them later
private ArrayList<JTextField> fieldList = new ArrayList<>();
// Numerical value for tracking the currently focused TextField
private int focusIndex;
// This constructor is the first thing that gets called from main
public BackspaceTest() {
init();
}
private void init() {
// Initialize the text fields with 1 character column size
text1 = new JTextField(1);
text2 = new JTextField(1);
text3 = new JTextField(1);
text4 = new JTextField(1);
// Key listeners for implementing 1 character limitations and Backspace functionality
text1.addKeyListener(this);
text2.addKeyListener(this);
text3.addKeyListener(this);
text4.addKeyListener(this);
// The focus listener keeps track of currently focused Text Fields
text1.addFocusListener(new TextFocusListener());
text2.addFocusListener(new TextFocusListener());
text3.addFocusListener(new TextFocusListener());
text4.addFocusListener(new TextFocusListener());
// The default focus is on the first field
text1.requestFocus();
fieldList.add(text1);
fieldList.add(text2);
fieldList.add(text3);
fieldList.add(text4);
// Puts the items on the screen
createLayout(text1, text2, text3, text4);
setTitle("Backspace Test");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private void createLayout(JComponent... arg) {
JPanel panel = (JPanel) getContentPane();
GroupLayout gl = new GroupLayout(panel);
panel.setLayout(gl);
gl.setAutoCreateContainerGaps(true);
gl.setAutoCreateGaps(true);
gl.setHorizontalGroup(gl.createSequentialGroup()
.addComponent(arg[0])
.addComponent(arg[1])
.addComponent(arg[2])
.addComponent(arg[3])
);
gl.setVerticalGroup(gl.createParallelGroup()
.addComponent(arg[0])
.addComponent(arg[1])
.addComponent(arg[2])
.addComponent(arg[3])
);
gl.linkSize(text1, text2, text3, text4);
pack();
}
class TextFocusListener implements FocusListener {
#Override
public void focusGained(FocusEvent e) {
focusIndex = fieldList.indexOf(getFocusOwner());
System.out.println(focusIndex);
}
#Override
public void focusLost(FocusEvent e) { }
}
#Override
public void keyTyped(KeyEvent e) { }
#Override
public void keyPressed(KeyEvent e) {
JTextField focusedText = fieldList.get(focusIndex);
// Check if BACKSPACE is pressed and if Text Field is empty and if it's not the first field
if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE &&
focusedText.getText().equals("") &&
focusIndex > 0) {
fieldList.get(--focusIndex).requestFocus();
}
}
#Override
public void keyReleased(KeyEvent e) {
// This limits the amount of characteres to 1. I'm not fully pleased with it but it works for this demonstration
if (text1.getText().length() >= 1)
text1.setText(text1.getText().substring(0, 1));
if (text2.getText().length() >= 1)
text2.setText(text2.getText().substring(0, 1));
if (text3.getText().length() >= 1)
text3.setText(text3.getText().substring(0, 1));
if (text4.getText().length() >= 1)
text4.setText(text4.getText().substring(0, 1));
}
// Here's where the program starts
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new BackspaceTest().setVisible(true);
}
});
}
});
If you don't have time to run it, look at the line 96, I think the Focus Listener is a good way to solve your problem. Hope this helps you out, good luck!

Removing new line function when enter button is pressed Java

I have a text area that I would like to become blank when the enter button is pressed. I know this would normally be done with a setText method. However when I do this, the text is removed but the new line function created by the return key being pressed. My question is, is the anyway of stopping this default action from happening?
thanks
Are you listening for the ENTER key on the text area and then clearing it? The following works for me:
final JTextArea ta = new JTextArea();
ta.addKeyListener(new KeyListener() {
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER){
ta.setText("");
}
}
#Override
public void keyPressed(KeyEvent e) {
}
});
Hi
I have a text area that I would like to become blank when the enter button is pressed.
This, I understand. Here's how you can do that:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main {
public static void main(String[]args) {
final JFrame frame = new JFrame();
final JTextArea area = new JTextArea();
area.setPreferredSize(new Dimension(200, 200));
area.addKeyListener(new KeyAdapter(){
#Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER) {
area.setText("");
}
}
});
frame.add(area);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
I know this would normally be done with a setText method. However when I do this, the text is removed but the new line function created by the return key being pressed. My question is, is the anyway of stopping this default action from happening?
That, I don't understand.
The problem is probably that you are not consuming the keystroke event, and although the text area is cleared, the normal processing of the keystroke ends up inserting a newline.
Rather than trapping the keystroke event (which isn't necessarily portable) I would recommend using a DocumentFilter. There is a tutorial here that shows you how to write one. Implement the filter so that when there is a 'newline' in the insert or replace string, replace the entire contents of the document with "".
However this approach can't tell the difference between a newline typed at the keyboard and one pasted into the text area.
Before you clear the text you need to remove the new line code the return button left. You do that with the consume() method.
So to clear your text:
yourkeyevent.consume();
yourTextObject.setText("");
Instead, you can also use:
yourTextarea.setText(null);
yourTextarea.setCaretPosition(-1);
I solved your problem overriding the code in the method "public void keyTyped(KeyEvent e)" instead of "public void keyPressed(KeyEvent e)" and it works.
Here the code:
package versione1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class WhiteEnter {
final JFrame frame = new JFrame();
private JTextArea area = new JTextArea();
public static void main(String[]args) {
WhiteEnter prova = new WhiteEnter();
prova.run();
}
public void run(){
area.setPreferredSize(new Dimension(200, 200));
area.addKeyListener(new PressEnterKeyListener());
frame.add(area);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public class PressEnterKeyListener implements KeyListener{
#Override
public void keyPressed(KeyEvent e) {
}
#Override
public void keyReleased(KeyEvent e) {
}
#Override
public void keyTyped(KeyEvent e) {
if (e.getKeyChar() == (KeyEvent.VK_ENTER)){
try{
area.setText(null);
area.setCaretPosition(0);
} catch(Exception ex){
ex.printStackTrace();
}
}
}
}
}

Categories

Resources