Java call "addActionListener" from "FocusListener" - java

I have JTextField. I need to save the changes, if user writes something in it and then lost the focus(like click some where else)
mMaxLabelLength = new JTextField();
mMaxLabelLength.addActionListener(this);
public void focusGained(FocusEvent fe)
{
System.out.println("4");
mMaxLabelLength.addActionListener(this);
}
#Override
public void focusLost(FocusEvent fe)
{
System.out.println("5");
mMaxLabelLength.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
//Do something
}
The problem is I am not able to call "actionPerformed" from "focusLost/focusGain". I need to keep the "actionPerformed" as separate method as I am calling it from another places also.

So, you want to do exactly the same thing when the focus is lost as what you're already doing in actionPerformed(), right, right. So, do just that:
#Override
public void focusLost(FocusEvent fe) {
doSomething();
}
public void actionPerformed(ActionEvent e){
doSomething();
}
private void doSomething() {
// ...
}

Related

Making an Action Listener for a JButton as a method?

How can I make this ActionListener into a method for a specific JButton?
(im aware its possible to throw it all in an method but yeah..hm.)
myJButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e){
//do stuff
}
});
thx y'all,
edit: thanks everyone for the quick responses, my explanation wasn't very clear.
I looked into the use of lambdas and it was pretty much what I was thinking of, though the other ways are great to know aswell.
myButton.addActionListener(e -> myButtonMethod());
public void myButtonMethod() {
// code
}
Thank you again, everyone.
I'll try to be more clear and quicker next time.
Again, your question remains unclear. Your code above has a method, one that code can be put into:
button1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// you can call any code you want here
}
});
Or you could call a method of the outer class from that method:
button1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
button1Method();
}
});
// elsewhere
private void button1Method() {
// TODO fill with code
}
Or you could call a method of the inner anonymous class from that code
button1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
button1Method();
}
private void button1Method() {
// TODO fill with code
}
});
Or you could use lambdas:
button2.addActionListener(e -> button2Method());
// elsewhere
private void button2Method() {
// TODO fill with code
}
Or you could use a method reference:
button3.addActionListener(this::button3Method);
// elsewhere
private void button3Method(ActionEvent e) {
// TODO fill with code
}
Up to you to try to be clear on what exactly it is you're trying to do and what's preventing you from doing it.

How to add MousePressed Eventhandler to addItem new JButton

addItem = new JButton("Add");
gc.gridwidth = GridBagConstraints.RELATIVE;
gc.weightx = 1.0;
panel.add(addItem, gc);
Am I able to make it into something like that:
addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
Instead of this I would want that button, but I have no idea what does that addItem do, since it does not let me to add a name there.
Is there a way how I can do this without modifying the 4 rows of code given at the beginning of the question?
what you can do is to add ActionListener to the button click or if you want you can add MouseListener , actually it depends on what you want to do
addItem.addActionListener(new ActtionListener() {...});
Code using an inner class instead of EventHandler.
addItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//do something
}
});
you can get more information on this in java official website.
Refer: http://docs.oracle.com/javase/7/docs/api/java/beans/EventHandler.html
if you add just the mouse listener you will not get the 'press' event if using keyboard. So, if your requirement is strictly bound to mouse press then use below snippet :)
addItem.addMouseListener(new MouseListener() {
#Override
public void mouseReleased(MouseEvent e) {
// do something
}
#Override
public void mousePressed(MouseEvent e) {
// do something
}
#Override
public void mouseExited(MouseEvent e) {
// do something
}
#Override
public void mouseEntered(MouseEvent e) {
// do something
}
#Override
public void mouseClicked(MouseEvent e) {
// do something
}
});

How do I simplify MouseListener so that I don't have all these unused methods?

Below I have the following code, so that when someone clicks on the "Close", the window will close. Below that is another exit button on the same menu bar, simply for redundancy (it'll be changed later to be something else, but the point stands as follows). My question is, is there any way to make this more simplistic? I mean there are four unused methods for every menu, and I'm going to need to do a few more. Any ideas on how to fix this?
closeFile.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent arg0) {
System.exit(0);
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
});
exit.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent arg0) {
System.exit(0);
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
});
Also, ActionListener wouldn't work for me, so I can't use that (don't believe I'm supposed to either).
Use a MouseAdapter and override the methods that you want.
closeFile.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent arg0) {
System.exit(0);
}
});
closeFile.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
//your code
}
});
Note: You dont have to write 'implements MouseListener' during class definition.
For more information, search for adapter classes, more specifically for MouseAdapter class.

Java - MousePress Hold and Release event listener

hello is there a way mouse even that can Hold the mouse and release cause I can't find it on google.
so for example this image..
When the jTextBox is **** when he click the button, he see the words oops...
then after he release the click of mouse the jTextBox will back to **** again
I know this code already but the mouseevent is I only don't know
Yes. You will want to implement the MouseListener interface with a new class and add this new Listener against your button with the following;
button.addMouseListener(new YourMouseListener());
An example custom MouseListener might look like this.
class YourMouseListener implements MouseListener {
#Override
public void mouseClicked(MouseEvent e) {
}
#Override
public void mousePressed(MouseEvent e) {
// Insert code to show password
}
#Override
public void mouseReleased(MouseEvent e) {
// Insert code to hide password again
}
#Override
public void mouseEntered(MouseEvent e) {
}
#Override
public void mouseExited(MouseEvent e) {
}
}
I hope this helps.
You'll need a Robot object. This can do things as follows:
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
The Mousebutton is pressed until you do this:
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
This should do what you want.

Get if someone is using a JComboBox?

Like the title says, i need to know if someone is using a combobox.
i.e. when the box is dropped down.
Is there any method to get this? Maybe an actionlistener?
Use JComboBox#addPopupMenuListener():
comboBox.addPopupMenuListener(new PopupMenuListener()
{
#Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e)
{
// ...
}
#Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent e)
{
// ...
}
#Override
public void popupMenuCanceled(PopupMenuEvent e)
{
// ...
}
});

Categories