Question : KeyListener in JTable

Why is the KeyListener ignored in this example?
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
package jtablekeylistener;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;

public class MyTable extends JTable implements KeyListener{

	/**
	 * constructor
	 */
	private MyTable() {
		
		this.setTableHeader(null); // no header
		
	}

	/**
	 * constructor
	 * 
	 * @param defaultTableModel
	 */
	public MyTable(DefaultTableModel defaultTableModel) {
		this();

		this.setModel(defaultTableModel);
		
	}
	
	/**
	 * 
	 * @param defaultTableModel
	 */
	public void setDefaultTableModel(DefaultTableModel defaultTableModel) {
		
		this.setModel(defaultTableModel);
		
	}

//	// not editable
//	@Override
//	public boolean isCellEditable(int row, int column) {
//		return false;
//	}

	@Override
	public void keyPressed(KeyEvent e) {			
		System.out.println ("keyPressed");

		// Returns the integer code for the key on the keyboard and if keyCode is equal to VK_C (ASCII C)... 
		if(e.getKeyCode() == KeyEvent.VK_C) 	{
			System.out.println ("ctrl-c");
		}


	}

	@Override
	public void keyReleased(KeyEvent e) {
		System.out.println ("keyReleased");

	}

	@Override
	public void keyTyped(KeyEvent e) {
		System.out.println ("keyTyped");

	}
	
	
    /**
     * 
     * @param args
     */
    public static void main(String[] args) {

        try{
            UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e){
            e.printStackTrace();
        }

        List<String> list1=new ArrayList<String>();
        list1.add("1a");
        list1.add("1b");
        list1.add("1c");
        list1.add("1d");

		String[][] stringArray = new String[list1.size()][1];
		int index = 0;
		for (String string: list1) {
			stringArray[index][0] = string;
			index++;
		}

        final MyTable myTable = new MyTable(new DefaultTableModel(stringArray, new Object[] { "" }));
        final JPanel myPanel = new JPanel();
        myPanel.add(myTable);
        final JFrame frame = new JFrame();
        frame.add(myPanel, BorderLayout.CENTER);
        frame.setTitle("Test JTable KeyListener" );
        frame.setPreferredSize(new Dimension(200, 100));
        frame.addWindowListener(new WindowAdapter(){
        	@Override
        	public void windowClosing(WindowEvent e) {
        	}
        });
        
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLocation(200, 100);
        frame.pack();
        frame.setVisible(true);

    }
}

Answer : KeyListener in JTable

addKeyListener not called
Random Solutions  
 
programming4us programming4us