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)
{
// ...
}
});
Related
This question already has answers here:
Java 8 Lambda Expressions - what about multiple methods in nested class
(6 answers)
Java idiom for lambdas with non-SAM interfaces
(4 answers)
Closed 3 years ago.
The following code adds Listeners to several SWT Text elements. The only difference is the code inside the Listeners method. Is there a way to make this code less repetitive by finding the correct method to use dynamically?
In this example a FocusListener is used, but it's not relevant.
private void addFocusLostListeners() {
nameText.addFocusListener(new FocusListener() {
#Override
public void focusGained(FocusEvent e) {}
#Override
public void focusLost(FocusEvent e) {
myDataObject.setName(nameText.getText());
}
});
ageText.addFocusListener(new FocusListener() {
#Override
public void focusGained(FocusEvent e) {}
#Override
public void focusLost(FocusEvent e) {
myDataObject.setAge(ageText.getText());
}
});
emailText.addFocusListener(new FocusListener() {
#Override
public void focusGained(FocusEvent e) {}
#Override
public void focusLost(FocusEvent e) {
myDataObject.setEmail(emailText.getText());
}
});
...
}
You could make a helper method (you need to replace TextField with the actual class of nameText, ageText, etc.):
private static void addFocusListener(TextField field, Consumer<? super String> setter) {
field.addFocusListener(new FocusListener() {
#Override
public void focusGained(FocusEvent e) {}
#Override
public void focusLost(FocusEvent e) {
setter.accept(field.getText());
}
});
}
Which you then could call:
private void addFocusLostListeners() {
addFocusListener(nameText, myDataObject::setName);
addFocusListener(ageText, myDataObject::setAge);
addFocusListener(emailText, myDataObject::setEmail);
}
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.
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() {
// ...
}
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.
cmbCategory.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
//Do things
}
});
The above code works just fine when I make selection using the control itself, but does not fire when I try changing the index programmatically.
Can anyone help me?
Maybe this could work:
cmbCategory.addModifyListener(new ModifyListener(){
#Override
public void modifyText(ModifyEvent event) {
//Do things
}
});