Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

KeyEvent  RSS feed

 
Brandon Jose
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In the JSpinner, I need to know the number keys pressed/typed (Not incremented using up-arrow button). I need to trap the same once key is released. Following code is giving no result. Any will be able to help me?
import javax.swing.*;
import javax.swing.JSpinner;
import javax.swing.event.*;
import java.text.*;
import java.awt.*;
import java.util.*;
import java.awt.event.KeyListener;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.FocusEvent;
/**
*
* @author m_Brandon
*/
public class PriceComponent implements KeyListener {

/** Creates a new instance of PriceComponent */
public PriceComponent() {
JFrame frame = new JFrame("Spinner");
frame.setDefaultCloseOperation(3);

SpinnerNumberModel priceModel = new SpinnerNumberModel(02,0.5,5000.00,00.5);
JSpinner qParam= new javax.swing.JSpinner(priceModel);
qParam.setEditor(new javax.swing.JSpinner.NumberEditor(qParam,"###.##" ));
frame.getContentPane().add(qParam, BorderLayout.CENTER);
qParam.addKeyListener(this);

frame.pack();
frame.show();

}

public void keyPressed(java.awt.event.KeyEvent e) {

System.out.println(e.KEY_PRESSED);


if (e.getKeyCode() == KeyEvent.VK_0) {
System.out.println("\nKey Event is: "+ e.VK_0 + " ZERO");
}
}

public void keyReleased(java.awt.event.KeyEvent e) {
//getComponent().getName().compareTo(


}

public void keyTyped(java.awt.event.KeyEvent e) {
}


/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new PriceComponent();
}

}
 
Ashish Mahajan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Brandon,
instead of adding keyListener to the spinner, add key listener to it's editor's text field like this :-
((JSpinner.DefaultEditor)spi_price.getEditor()).getTextField().addKeyListener(keyListener);

where spi_price is a spinner. At a first glance the type cast may seem as unsafe. Actually spinner's editor type is JComponent but since u r setting the editor and if ur organisation decides that the standard editors provided by Sun r sufficient and that if any custom spinner editor is to be developed in ur org. will extend the default editor then such type casts r safe.
 
Brandon Jose
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ashish,
Thanks for helping me out! I think you have solution for my problem. Actually I am trying to set values in the JSpinner text field like, if user types 1000, then I want to display the value 10.00 as and when user types the values. I also looked at possible creation of customized JSpinner using a JFormattedTextField with our own masking. But I dropped the idea as I could find the way to achieve it. So I decided get the value as and when sure types with keyevent and then divide it and then put the value back using setValue. If you have some solution, it will be of great use. I request you to pass on the same to me.
Thanks,
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!