Methods with event handling giving me errors - java

Sorry for bad title but didn't know what else to say.
So I am trying to add a little piece of code into a game I am making so that you can make the player jump by pressing any key or using the mouse. I pretty much copied the code that makes the player jump by pressing the mouse into the code that makes him jump by keyboard keys ( risky but I like to live on the edge) but it gives me the error:
The method KeyPressed(KeyEvent) from the type new KeyAdapter(){} is never used locally
from this:
this.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent arg0) {
player.jump();
}
});
this.addKeyListener(new KeyAdapter(){
public void KeyPressed(KeyEvent arg0) {
player.jump();
}
});
}
I have searched the whole game but found no other things that have the word 'mouse' in them and I am also new to this. I'm sure i'm missing something obvious.

Related

Key bindings for control release not working

I have a JPanel that I need to check for the control being pressed down so that the user can select multiple things on screen, i had the issues of using a key listener so after research i found that i was supposed to use key bindings, and i finally got it to work for pressing control, but i cant get it to work for releasing control
'''
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_CONTROL,
InputEvent.CTRL_DOWN_MASK), "press");
getActionMap().put("press", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
controlPressed = true;
}
});
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.CTRL_DOWN_MASK
,InputEvent.CTRL_DOWN_MASK,true), "release");
getActionMap().put("release", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("release");
controlPressed = false;
}
});
'''
so pressing ctrl works but releasing does not, any ideas?
Update, I found what I think is the best solution for my problem, the mouse event stores weather or not control is pressed down so there was no need to do any magic with keysListeners or binding keys. here is the line of code if anyone needs it
'''
public void mousePressed(MouseEvent e){
e.isControlDown();
}
'''

Java Swing feedback button on a tablet touch screen

Is it possible obtain a feedback from JButton in a Java Swing application on a tablet?
I am using Windows 8 and if I switch a physical mouse to the device, motion feedback from JButton is in the typical way, but if I use a finger, the feedback disappears.
I have tried overriding methods customizing my inherited JButtons, and a estended etc., but I haven't hoped the goal... I guess it is related with when we touch the screen with a mouse, you only click a point on the screen, but if you touches with a finger, there are several pixels selected.
Any idea?
Thank you so so much!
Im not entirely sure what you mean by feedback, but I THINK the answer to the question your asking is no. Swing was never designed for that sort of interface. However if the feedback you are referring to is something like the button highlighting and swelling when clicked, this is usually something that should happen on its own. If as I suspect you are referring to a hover action being performed when youtouch but dont 'tap' the button, then there is likely no way for you to control that. As an alternative, if your application is not yet mature, you may want to consider switching from swing to JavaFX which uses CSS to give you a large amount of control over things like this.
I've got an acceptable solution. I will try to explain it as simple and complete as possible.
First of all, you have to use a JButton extended class like these:
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
/**
* Customized <code>JButton</code> to obtained a feedback user experience of movement.
* #author Gabriel Moreno.
*/
public class FeedbackTouchScreenButton extends JButton {
public FeedbackTouchScreenButton() {
super();
this.addMouseListener(new MouseListener() {
#Override
public void mouseClicked(final MouseEvent e) {
new Thread(new Runnable() {
#Override
public void run() {
Color bg = e.getComponent().getBackground();
e.getComponent().setBackground(Color.BLUE); // For example
try {
Thread.sleep(100);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
e.getComponent().setBackground(bg);
}
}).start();
} // mouseClicked()
#Override
public void mouseEntered(MouseEvent e) {}
#Override
public void mouseExited(MouseEvent e) {}
#Override
public void mousePressed(MouseEvent e) {}
#Override
public void mouseReleased(MouseEvent e) {}
});
} // FeedbackTouchScreenButton()
} // FeedbackTouchScreenButton
When you customize the action performed of the concerned button, you will have to throw (carefully) another thread. For example:
// ---
btnExample.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(final java.awt.event.ActionEvent evt) {
new Thread(new Runnable() {
#Override
public void run() {
btnExampleActionPerformed(evt);
} // run()
}).start();
}
});
// ---
This example apparently works. But actually only it seems... :-)
The reason is because on the tablet screen the 'onPressed' state of the component doesn't works with a finger like with a mouse pointer.
THE KEY: 'onClick' = 'onPressed' + 'onRelease'.
And 'onClick' is correctlty done on the touch screen. It is the moment when the trick is done, when you release your finger from the device.

Gridlayout and mouse listeners

hi i am trying to build a grid-layout GUI with mouse listener. SO when a particular cell is clicked in a grid ,information would be displayed. I don't know where to start, any help would be good
thankyou
I believe you have a class that inherits from JPanel or JFrame and there is whole GUI in it. Then, this class should implement mouseListener. Then your class should have similar code:
#override
public void mouseClicked(MouseEvent e){}
#override
public void mousePressed(MouseEvent e){}
#override
public void mouseEntered(MouseEvent e){}
#override
public void mouseReleased(MouseEvent e){
/*This method is being called when you release your click. It's better
then mouseClicked because mouseClicked is only called when you press
and release on the same pixel or Object (not sure about it)
*/
}
#override
public void mouseExiteded(MouseEvent e){}
In each method you can get source of
MouseEvent e
using
Object source = e.getSource();
if (source == button1){
//Do sth
}if (source == button2){
//Do sth else
}if (source == radioButton1){
//Do whatever you want
}
Then you have reference to the source, so you can modify what you want.
In your gridlayout, set all grids with some Component such as Button or Label. You can set listeners on the components added and display information when a component is clicked
To use properly a gridbaglayout, you should first work on the gridbagconstraints. Then, you should use the ActionListener interface to handle the mouse clicks. If the cells are of the type Labels, you coud hide the text by using myLabel.setText("") and putting the text by using myLabel.setText("information to display"). If you need more help, just ask :D and +1 if it helps ^^

Java Netbeans - Key Listener doesn't work

After searching on internet why it's happening and trying to change my code in any possible way that I know, it still doesn't work. Basiclly i want to make my JFrame appear when shift is hold. That's my code:
public DesktopMenu() {
initComponents();
setFocusable(true);
//Listening to the mouse movement to change position of the window
this.addMouseMotionListener(new MouseAdapter(){
#Override
public void mouseMoved(MouseEvent e){
xPos = e.getX();
yPos = e.getY();
setLocation((e.getXOnScreen()-xPos),(e.getYOnScreen()-yPos));
}
});
//This should listen to the key, when it's pressed window just appear.
this.addKeyListener(new KeyListener(){
#Override
public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_SHIFT){
setVisible(true);
}else{
setVisible(false);
}
};
#Override
public void keyReleased(KeyEvent e) {
}
#Override
public void keyTyped(KeyEvent e) {
}
});
}
I run the window in standard Java Class with new DesktopMenu().setVisible(false); 'cause i want to make it just appear when SHIFT is pressed and hold. Thanks in advance.
If you've searched KeyListeners, you'll find that one of the most common problems (other than that they usually should be avoided with Swing applications) is that they won't work if the component that is listened to doesn't have focus. Well the problem you're having is similar but on a bigger scale: the KeyListener won't work if the application listened to doesn't have focus and isn't even visible.
In short, what you are trying to do cannot be done in core Java. Consider using another utility that can do this for you, such as AutoIt if you are running in a Windows environment.
It all depends on the details of what you're trying to do, what environment, and so on. But AutoIt can listen to global keypresses. You can tie it into your Java app by one of several ways, but the easiest is to simply send messages to each other via the standard input and output sockets. And then when AutoIt detects the correct keypress, it sends a message to the Java via ConsoleWrite(...), and the Java app responds by reading it in via, say a Scanner.
I do know that I would never use the shift key as the hotkey since that will make the user's computer completely non-functionable.

Java Swing default focus on frame

I am learning java and Swing right now and trying to develop simple programms for education purposes.
So here is the question.
I have gridlayout and fields on my frame with default text
accNumberField = new JTextField("0", 10);
accNumberField.addFocusListener(new FocusListener() {
int focusCounter = 0;
#Override
public void focusGained(FocusEvent arg0) {
// TODO Auto-generated method stub
if (focusCounter > 0)
accNumberField.setText("");
focusCounter++;
}
What I want is that when user click on field for the first time the default text is disappered. So I add focus listener and used accNumberField.setText(""); in focusGained method.
But the problem is that for default first field in my frame getting focus right in time of frame creation. And default text is disappearing from the begining. I used counter as you can see. But that's not what I wanted.
I want that no field would get focus in time of creation and every field would be able to get focus from the time when user would click on one of them.
Sorry if I spelled something wrong. English is not my native language.
Found a thread having a code example of your desired functionality, Java JTextField with input hint. Precisely, you need to provide your own implementation of JTextField which will be holding the "default-text" in a field, specially created for that.
For your second question, you can set the focus to some button or frame itself.
Is there any reason that you use focusListener()? why not use mouseListener() as follow?
accNumberField.addMouseListener(new MouseAdapter()
{
#Override
public void mouseReleased(MouseEvent e)
{
accNumberField.setText("");
}
});
if you want to clear the text for the first click, you can simply use a boolean:
//outside constructor
private boolean isTextCleared = false;
//in constructor
accNumberField.addMouseListener(new MouseAdapter()
{
#Override
public void mouseReleased(MouseEvent e)
{
if (!isTextCleared)
{
accNumberField.setText("");
isTextCleared = true;
}
}
});

Categories