JSpinner button focus listener

I want to add focus listener to buttons of JSpinner. How can I add focus listener to JSpinner buttons.If anyone knows please help me, its urgent.
[159 byte] By [PawanPa] at [2007-11-27 9:24:40]
# 1
set your own UI, where you override createNext/PreviousButton(), and addyour focusListener there
Michael_Dunna at 2007-7-12 22:20:44 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thank you.

But it does not work.

My UI class looks like:

public class MySpinnerUI extends BasicSpinnerUI implements FocusListener{

public MySpinnerUI()

{

super();

}

protected Component createNextButton() {

Component nextButton = super.createNextButton();

nextButton.setBackground(new Color(240,240,240));

nextButton.addFocusListener(this);

return nextButton;

}

protected Component createPreviousButton()

{

Component previousButton = super.createPreviousButton();

previousButton.setBackground(new Color(240,240,240));

previousButton.addFocusListener(this);

return previousButton;

}

public void focusGained(FocusEvent evt)

{

System.out.println("focus gained");

}

public void focusLost(FocusEvent evt)

{

System.out.println("focus lost");

}

}//end of class

And I use following lines to set UI of spinner:

--

JSpinner spinner = new JSpinner(new SpinnerNumberModel(1, 1, 1440, 1));

spinner.setUI(new MySpinnerUI());

If I miss something please help me.

PawanPa at 2007-7-12 22:20:44 > top of Java-index,Desktop,Core GUI APIs...
# 3

> But it does not work.

quite right.

works OK for actionListeners, mouseListeners, but it seems the focusListener

is consumed.

what is it you're trying to do - perhaps there's a different way, rather than playing

around with modifying the BasicSpinnerUI source code

Michael_Dunna at 2007-7-12 22:20:44 > top of Java-index,Desktop,Core GUI APIs...
# 4
The super implementation that you're calling returns an unfocusable button. Make it focusable and it will work.
kirillga at 2007-7-12 22:20:44 > top of Java-index,Desktop,Core GUI APIs...
# 5

It still don't work.

I just set focusable to true in above code but again focus listener did not work.

protected Component createNextButton() {

Component nextButton = super.createNextButton();

nextButton.setBackground(new Color(240,240,240));

nextButton.setFocusable(true);

nextButton.addFocusListener(this);

return nextButton;

}

Thanks

PawanPa at 2007-7-12 22:20:44 > top of Java-index,Desktop,Core GUI APIs...
# 6

I am using JSpinner as JTable cell editor. When I set focuslistener to spinner

its textfield only works well but button does not works . What I want to do is that when user change data in spinner and go out of this table, I want to know that focus has been lost from spinner(button and textfield of spinner) to find if data in cell has been changed.

Thanks Michael_Dunn

PawanPa at 2007-7-12 22:20:44 > top of Java-index,Desktop,Core GUI APIs...
# 7

I'd tried setting the button focusable earlier, but it made no difference.

after kirill's post, I thought it might be a 1.6 thing. just fired up an old box with 1.4,

same result there.

anyway, looks like the culprit is BasicArrowButton's isFocusTraversable() which returns false.

focusListener now works in this, but I don't like it :

click a couple of ups, go too far, then click down to go back, focusLost will fire.

if editable, if the textfield shows a cursor after clicking, focusLost wil fire

isFocusTraversable() is false for a reason, changing to true will probably

cause some other problem.

import javax.swing.*;

import javax.swing.border.*;

import javax.swing.plaf.*;

import java.awt.*;

import java.awt.event.*;

class Testing

{

public void buildGUI()

{

JPanel p = new JPanel();

JSpinner spinner = new JSpinner(new SpinnerNumberModel(50, 0, 100, 5));

spinner.setUI(new MyUI());

p.add(spinner);

JFrame f = new JFrame();

f.getContentPane().add(p,BorderLayout.NORTH);

f.getContentPane().add(new JTextField("to take focus",5),BorderLayout.SOUTH);

f.pack();

f.setLocationRelativeTo(null);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

}

public static void main(String[] args)

{

SwingUtilities.invokeLater(new Runnable(){

public void run(){

new Testing().buildGUI();

}

});

}

}

class MyUI extends javax.swing.plaf.basic.BasicSpinnerUI

{

protected Component createNextButton()

{

Component c = createArrowButton(SwingConstants.NORTH);

c.setName("Spinner.nextButton");

installNextButtonListeners(c);

((JButton)c).setRequestFocusEnabled(true);

c.addFocusListener(new FocusAdapter(){

public void focusLost(FocusEvent fe){

System.out.println("Next Button Focus Lost");

}

});

return c;

}

protected Component createPreviousButton()

{

Component c = createArrowButton(SwingConstants.SOUTH);

c.setName("Spinner.previousButton");

installPreviousButtonListeners(c);

((JButton)c).setRequestFocusEnabled(true);

c.addFocusListener(new FocusAdapter(){

public void focusLost(FocusEvent fe){

System.out.println("Previous Button Focus Lost");

}

});

return c;

}

private Component createArrowButton(int direction)

{

//JButton b = new BasicArrowButton(direction);

JButton b = new MyBasicArrowButton(direction);//<changed here

Border buttonBorder = UIManager.getBorder("Spinner.arrowButtonBorder");

if (buttonBorder instanceof UIResource) {

// Wrap the border to avoid having the UIResource be replaced by

// the ButtonUI. This is the opposite of using BorderUIResource.

b.setBorder(new CompoundBorder(buttonBorder, null));

} else {

b.setBorder(buttonBorder);

}

b.setInheritsPopupMenu(true);

return b;

}

}

class MyBasicArrowButton extends javax.swing.plaf.basic.BasicArrowButton

{

public MyBasicArrowButton(int direction)

{

super(direction);

}

public boolean isFocusTraversable() {

return true;//<changed to true

}

}

Michael_Dunna at 2007-7-12 22:20:44 > top of Java-index,Desktop,Core GUI APIs...
# 8
Thank you Michael_Dunn. Your code works.
PawanPa at 2007-7-12 22:20:44 > top of Java-index,Desktop,Core GUI APIs...