Difference between actionPerformed and keyPressed in Netbeans - java

I have started a project in Jframe Netbeans.
One thing which bothers me is this option where on one side I have actionPerformed which contains the action which we do when the key is pressed but again we have another option of key Pressed which does the same.
I know these two cannot perform the same function but what possibly could be the difference?

actionPerformed is called when the user triggers any event. It can be when a user clicks a button, selects a menu item, or presses enter in a text field.
Documentation:
https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html
keyPressed is more specific and is for when a user presses a key. There are other methods available when implementing a KeyListener such as keyTyped or keyReleased which gives you more control over what the user is doing with the keyboard specifically.
Documentation:
https://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html

Related

Enter key on JFrame

So what I've been attempting to do is essentially have it set to when the enter key is pressed on my JFrame in java it detects it so I can do an action like start a game by switching to the next screen. So I was wondering if anyone could help me figure out how to do this in particular. Just being able to have something that constantly checks to see if the Enter Key has been pressed on the JFrame
have it set to when the enter key is pressed on my JFrame in java it detects it so I can do an action like start a game by switching to the next screen.
How do people know to use the Enter key?
Typically you would have a button like "Start Game" for the user to click.
You can easily make this the default button by using:
frame.getRootPane().setDefaultButton( startGameButton );
Now the user will be able to either:
click on the button
use the Enter key to invoke the button.

How to display Image when button1 is pressed

How can i display and image when button1 is pressed, this is the code im using to check for a button press:
public class clientFrameButtons {
public void frameClient(){........
....
button1.addActionListener(new ActionListener(){ //ActionListener checks for button press
public void actionPerformed(ActionEvent e){
// if button is pressed the following will happen
I've tried implementing an if statement and looked at several sources online but cannot find a answer to fit my needs.
Thanks.
ActionEvent is a logical event those intention is to signal the fact that a button was actioned. So it doesn't provide any information about the user inputs that have lead to the action (it may be a shortcut, a mouse press, your mouse could have been set to left-handed, etc). If you want to care about mouse, then you must catch mouse events, with an appropriate MouseListener.
So your problem can be expressed in two ways:
you want to display an image when a GUI button is actioned then use ActionListener.
you want to make different things depending on the mouse button pressed while over a GUI button, then use MouseListener.

Can I change combobox value when a button is clicked in java?

By using doClick() method, the action of a button can be triggered. Is there such a way to change combobox value when a button is clicked in java? Thank You.
As you didn't state the graphical framework I assume you use Swing, as it contains a doClick function.
The doClick() function is only used to emulate a button click programmatically, for example in testing. This will fire all actions that are normally associated with that button, however you need to register those yourself, using a listener.

How to distinguish between mouseDragged and mousePressed?

I want my program to behave diffrently when the user presses a component, and when a user drags the mouse over a component, the problem is that on mouse click both of these methods are being called (and it seems like mouseDragged is called after mousePressed), so how will i know if the user dragged his mouse or just pressed it?
The correct answer is to use mouseClicked instead of combination of mousePressed+mouseReleased in case you want to distinguish between a click and a drag.

Keyboard shortcuts and action listeners

Wondering how to link a keyboard key to a JButton in netbeans, Downloaded KeyEventDemo program from oracle that lets you press a key and it tells you the keycode, Now im wondering how to implement that, working on a calculator project and I already have the ability to click on any button and have it either +,-,/,* or enter so now I am wondering how to link the 2 together, essentially I'm trying to figure out how to check which key is being pressed in a method, and then run a method depending on what they pressed! thanks for any info!
See How to Use Key Bindings. Basically you bind a KeyStroke to an Action. The tutorial also has a section on How to Use Actions.

Categories

Resources