JSpinner and KeyListener
Hi...
I have a problem with the JSpinner... How I can add a KeyEvent?
I try this way, but it don't work:((JSpinner.DefaultEditor)spn_Cantidad.getEditor()).getTextField().addKeyListener(new KeyAdapter(){
publicvoid keyTyped(KeyEvent e){
JOptionPane.showMessageDialog(null,e.getSource().toString());
}
});
Please help... I try different ways... but don't work...
Regards...
[633 byte] By [
MetalTuxa] at [2007-11-26 20:10:16]

# 1
works OK for me - optionPane shows when a key is typed> but it don't work:better if you explained what happens (or does not happen)
# 2
I'm working with JDK1.6.0 and NetBeans 5.5...I have the code just Like that, but never show me the JOptionPane dialog...Watt is bad? Please... its urgent...Regards...
# 3
> Watt is bad?
I have no idea, the snippet works OK for me.
you would need to post a compilable sample program that demonstrates the problem
(don't even think about posting netbeans-generated code)
here's a demo program, compile/run from the command line,
just to check it works OK in java 1.6
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Testing
{
public void buildGUI()
{
JSpinner spinner = new JSpinner(new SpinnerNumberModel(0,0,100,1));
((JSpinner.DefaultEditor)spinner.getEditor()).getTextField().addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e){
JOptionPane.showMessageDialog(null,e.getSource().toString());
}
});
JFrame f = new JFrame();
f.getContentPane().add(spinner);
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();
}
});
}
}
# 4
> This is from one of your postings 2 weeks ago:
Sorry if I post a lot of code... but I don't know wath to do... the next Time I made a simple example of my code...
Apparently you haven't learned yet how to post a simple executable example.
> Please... its urgent...
Well, you got an answer 20 minutes after you last asked for help.
Why haven't you yet replied to thank Michael for the working example? I thought the problem was urgent?
Thats why I tend not to help people who say the question is "urgent". They never appreciate the help.
# 5
I try to run the code in a sample, and it's work fine... no problems...
But when I try to put it in my application, it don't work... and I don't know why...
My code is very long... up to 2300 lines... but it's not generated by NetBeans...
The code where I have the problem is this:import java.util.*;
import java.util.Date;
import java.text.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.border.*;
import javax.swing.text.*;
public class Bar_Main extends JDialog implements ActionListener, KeyListener{
JPanel Panel_Main;
JLabel lbl_Fecha;
JLabel lbl_Fecha_Actual;
JLabel lbl_Producto;;
JLabel lbl_Cantidad;
JLabel lbl_Precio;
JLabel lbl_Total;
JComboBox cmb_Prod_Nom, cmb_Prod_Cod;
JSpinner spn_Cantidad;
JTextField txt_Precio;
JTextField txt_Total;
//javax.swing.Timer tmr_Reloj;
java.util.Date Fecha;
DateFormat frmt_Fecha;
//SimpleDateFormat frmt_Hora;
Connection conec;
Statement state;
ResultSet rec;
public Bar_Main(){
setTitle("Consumo de Bar");
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conec = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:/POOL/Data/BaseDat.MDB");
state = conec.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
}catch(Exception e){}
frmt_Fecha = DateFormat.getDateInstance(DateFormat.FULL);
Fecha = new java.util.Date();
Panel_Main = new JPanel(null);
Panel_Main.setBackground(Color.GREEN.darker());
lbl_Fecha = new JLabel("Fecha");
lbl_Fecha.setBounds(20,20,80,20);
lbl_Fecha_Actual = new JLabel(frmt_Fecha.format(Fecha).toUpperCase());
lbl_Fecha_Actual.setBounds(130,20,230,20);
lbl_Producto = new JLabel("Elija Producto");
lbl_Producto.setBounds(20,50,80,20);
cmb_Prod_Nom = new JComboBox(Cargar_Combo_Nom());
cmb_Prod_Nom.setBounds(130,50,230,20);
cmb_Prod_Nom.addActionListener(this);
cmb_Prod_Cod = new JComboBox(Cargar_Combo_Cod());
lbl_Cantidad = new JLabel("Cantidad");
lbl_Cantidad.setBounds(20,80,80,20);
spn_Cantidad = new JSpinner();
spn_Cantidad.setBounds(130,80,50,20);
((JSpinner.DefaultEditor)spn_Cantidad.getEditor()).getTextField().addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e){
JOptionPane.showMessageDialog(null,e.getKeyChar());
}
});
lbl_Precio = new JLabel("Valor Unitario");
lbl_Precio.setBounds(20,110,80,20);
txt_Precio = new JTextField("");
txt_Precio.setBounds(130,110,50,20);
txt_Precio.setHorizontalAlignment(JTextField.RIGHT);
txt_Precio.setEnabled(false);
txt_Precio.setDisabledTextColor(Color.DARK_GRAY);
try{
rec = state.executeQuery("Select * from Productos where prod_id = " + cmb_Prod_Cod.getSelectedItem());
rec.absolute(1);
spn_Cantidad.setModel(new SpinnerNumberModel(1,1,rec.getInt("prod_cant"),1));
txt_Precio.setText(Integer.toString(rec.getInt("prod_precio")));
}catch(Exception e){JOptionPane.showMessageDialog(null,e.getMessage());}
Panel_Main.add(lbl_Fecha); Panel_Main.add(lbl_Fecha_Actual);
Panel_Main.add(lbl_Producto); Panel_Main.add(cmb_Prod_Nom);
Panel_Main.add(lbl_Cantidad); Panel_Main.add(spn_Cantidad);
Panel_Main.add(lbl_Precio); Panel_Main.add(txt_Precio);
setLayout(new BorderLayout());
add(Panel_Main,BorderLayout.CENTER);
setSize(600,400);
setLocationRelativeTo(null);
setModal(true);
setVisible(true);
}
public DefaultComboBoxModel Cargar_Combo_Nom(){
DefaultComboBoxModel model = new DefaultComboBoxModel();
try{
rec = state.executeQuery("Select * from Productos where prod_cant > 0 order by prod_nom");
rec.first();
while(rec.isAfterLast() == false){
model.addElement(rec.getString("prod_nom"));
rec.next();
}
}catch(Exception e){}
return model;
}
public DefaultComboBoxModel Cargar_Combo_Cod(){
DefaultComboBoxModel model = new DefaultComboBoxModel();
try{
rec = state.executeQuery("Select * from Productos where prod_cant > 0 order by prod_nom");
rec.first();
while(rec.isAfterLast() == false){
model.addElement(rec.getString("prod_id"));
rec.next();
}
}catch(Exception e){}
return model;
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource().equals(cmb_Prod_Nom)){
cmb_Prod_Cod.setSelectedIndex(cmb_Prod_Nom.getSelectedIndex());
try{
rec = state.executeQuery("Select * from Productos where prod_id = " + cmb_Prod_Cod.getSelectedItem());
rec.absolute(1);
spn_Cantidad.setModel(new SpinnerNumberModel(1,1,rec.getInt("prod_cant"),1));
txt_Precio.setText(Integer.toString(rec.getInt("prod_precio")));
}catch(Exception e){}
}
}
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
}
Please help me...
Regards...
# 6
> > This is from one of your postings 2 weeks ago:
>
> Sorry if I post a lot of code... but I don't know
> wath to do... the next Time I made a simple example
> of my code...
>
> Apparently you haven't learned yet how to post a
> simple executable example.
>
> > Please... its urgent...
>
> Well, you got an answer 20 minutes after you last
> asked for help.
>
> Why haven't you yet replied to thank Michael for the
> working example? I thought the problem was urgent?
>
> Thats why I tend not to help people who say the
> question is "urgent". They never appreciate the help.
I Really appreciate your help... Sorry If I don't say thanks... Sorry if I say that it's urgent... but it is.... I don't know so much English... so I don't know how to tell you my problems in the best way...
Thanks to all of you... and really sorry...
Regards... Juan Rios P.
# 7
> Sorry if I say that it's urgent... but it isNot to us it isn't.We only want to know information relevant to the problem.I'm guessing that when you change the model a new editor is created so you lose the KeyListener.
# 8
OK... sorry me camickr... I never do that again...
> I'm guessing that when you change the model a new
> editor is created so you lose the KeyListener.
How can I fix it? somebody know?
I try to put spn_Cantidad.addKeyListener(this); every time I change the model, but It don't work...
Regards... and thanks for your time...
# 9
> How can I fix it? somebody know?
Well, we've provent that the concept works.
So, comment out all the code in your class except for the code that adds the spinner to the dialog and see if it works.
If it works then you uncomment 1 block of code at a time to see whats causing the problem. Then once you know what is causing the problem you will be able to fix it or you will be able to post a better demo program that we can execute and test.
We can't do anything with the code you have currently posted since it is not complete and it references a database.
# 10
Ok... sorry for the DataBase... but I fix the problem...
It's not pretty... but it's work...
I do this:import java.util.*;
import java.util.Date;
import java.text.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.crypto.NullCipher;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.border.*;
import javax.swing.text.*;
import javax.crypto.*;
import java.security.*;
public class Bar_Main extends JDialog implements ActionListener, KeyListener{
JPanel Panel_Main;
JLabel lbl_Fecha;
JLabel lbl_Fecha_Actual;
JLabel lbl_Producto;;
JLabel lbl_Cantidad;
JLabel lbl_Precio;
JLabel lbl_Total;
JComboBox cmb_Prod_Nom, cmb_Prod_Cod;
JSpinner spn_Cantidad;
JTextField txt_Precio;
JTextField txt_Total;
java.util.Date Fecha;
DateFormat frmt_Fecha;
////This is new code
private JTextField txt;//////
public Bar_Main(){
setTitle("Consumo de Bar");
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
frmt_Fecha = DateFormat.getDateInstance(DateFormat.FULL);
Fecha = new java.util.Date();
Panel_Main = new JPanel(null);
Panel_Main.setBackground(Color.GREEN.darker());
lbl_Fecha = new JLabel("Fecha");
lbl_Fecha.setBounds(20,20,80,20);
lbl_Fecha_Actual = new JLabel(frmt_Fecha.format(Fecha).toUpperCase());
lbl_Fecha_Actual.setBounds(130,20,230,20);
lbl_Producto = new JLabel("Elija Producto");
lbl_Producto.setBounds(20,50,80,20);
cmb_Prod_Nom = new JComboBox(Cargar_Combo_Nom());
cmb_Prod_Nom.setBounds(130,50,230,20);
cmb_Prod_Nom.addActionListener(this);
cmb_Prod_Cod = new JComboBox(Cargar_Combo_Cod());
lbl_Cantidad = new JLabel("Cantidad");
lbl_Cantidad.setBounds(20,80,80,20);
spn_Cantidad = new JSpinner();
spn_Cantidad.setBounds(130,80,50,20);
///////New code
txt = new JTextField(); ///////////
lbl_Precio = new JLabel("Valor Unitario");
lbl_Precio.setBounds(20,110,80,20);
txt_Precio = new JTextField("");
txt_Precio.setBounds(130,110,50,20);
txt_Precio.setHorizontalAlignment(JTextField.RIGHT);
txt_Precio.setEnabled(false);
txt_Precio.setDisabledTextColor(Color.DARK_GRAY);
spn_Cantidad.setModel(new SpinnerNumberModel(1,1,10,1));
////////New code
txt = ((JSpinner.DefaultEditor)spn_Cantidad.getEditor()).getTextField();
txt.addKeyListener(this);//////////
Panel_Main.add(lbl_Fecha); Panel_Main.add(lbl_Fecha_Actual);
Panel_Main.add(lbl_Producto); Panel_Main.add(cmb_Prod_Nom);
Panel_Main.add(lbl_Cantidad); Panel_Main.add(spn_Cantidad);
Panel_Main.add(lbl_Precio); Panel_Main.add(txt_Precio);
setLayout(new BorderLayout());
add(Panel_Main,BorderLayout.CENTER);
setSize(600,400);
setLocationRelativeTo(null);
setModal(true);
setVisible(true);
}
public DefaultComboBoxModel Cargar_Combo_Nom(){
DefaultComboBoxModel model = new DefaultComboBoxModel();
for (int i = 0; i<2; i++){
model.addElement("Product " + Integer.toString(i));
}
return model;
}
public DefaultComboBoxModel Cargar_Combo_Cod(){
DefaultComboBoxModel model = new DefaultComboBoxModel();
for (int i = 0; i<2; i++){
model.addElement("Code " + Integer.toString(i));
}
return model;
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource().equals(cmb_Prod_Nom)){
cmb_Prod_Cod.setSelectedIndex(cmb_Prod_Nom.getSelectedIndex());
if (cmb_Prod_Cod.getSelectedIndex() == 0){
spn_Cantidad.setModel(new SpinnerNumberModel(1,1,10,1));
txt_Precio.setText(Integer.toString(2500));
}
else{
spn_Cantidad.setModel(new SpinnerNumberModel(1,1,20,1));
txt_Precio.setText(Integer.toString(5000));
}
///////New code
txt = ((JSpinner.DefaultEditor)spn_Cantidad.getEditor()).getTextField();
txt.addKeyListener(this);//////////
}
}
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
if (e.getSource().equals(txt)){
e.consume();
}
}
public static void main(String[] args) throws IOException{
new Bar_Main();
}
}
Thanks to all for your time... and Regards...
