adding a 'key press listener' to a small swing GUI

J

Jeremy Watts

Hi,

I've programmed a small GUI that displays a JTable of entries that allows a
user to enter a mathematical matrix of values. Once all the cells of the 2D
table are filled, the user may then press the 'declare' button on the JFrame
that then 'sends' the data back to the main method.

I've successfully added a 'listener' for the button that seems to work ok,
but cant seem to do the same for the keys being pressed. Basically I am
after a listener that will be able to detect when one of the fields has been
filled, or typed in. Have trieda few things but nothing seems to work.

What I have done is as below:-

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.event.*;



public class TestTable {
public static void main(String args[]) {
int rowNumber = 4;
int columnNumber = 7;
String[][] result = new String[rowNumber + 1][columnNumber + 1];
result = increment(rowNumber, columnNumber);
}

public static String[][] increment(int rowNumber, int columnNumber) {
// Set X,Y location
int rowHeight = 25;
String[][] contents = new String[rowNumber + 1][columnNumber + 1];
JFrame frame = new JFrame("Define your [" +
(String.valueOf(rowNumber)) + ", " + (String.valueOf(columnNumber)) + "]" +
" matrix");
JPanel bottomPanel = new JPanel();
JButton declare = new JButton("Declare it");
final JTable table = new JTable(rowNumber, columnNumber);

bottomPanel.setLayout(new FlowLayout());
table.setSelectionBackground(Color.pink);
frame.setLocation(50, 50);
frame.setSize(columnNumber * 100, 100 + ((rowNumber - 1) *
rowHeight));
table.setRowHeight(rowHeight);
frame.add(table);
bottomPanel.add(declare, BorderLayout.SOUTH);
frame.add(bottomPanel, BorderLayout.SOUTH);
frame.setVisible(true);





declare.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try {
Object str = table.getValueAt(1, 1);
if (str == null) {
System.out.println("table[0,0] = null");
}
else {
System.out.println(str);
}
} catch (Exception e) {
System.err.println("Error: " + e);
e.printStackTrace();
}
}
});


return (contents);
}

}


Many thanks
Jeremy Watts
 
J

John B. Matthews

[...]
I've programmed a small GUI that displays a JTable of entries that allows a
user to enter a mathematical matrix of values. Once all the cells of the 2D
table are filled, the user may then press the 'declare' button on the JFrame
that then 'sends' the data back to the main method.

I've successfully added a 'listener' for the button that seems to work ok,
but cant seem to do the same for the keys being pressed. Basically I am
after a listener that will be able to detect when one of the fields has been
filled, or typed in. Have trieda few things but nothing seems to work.

What I have done is as below:-
[...]

As Thomas A. Russ helpfully suggested in this thread:

http://groups.google.com/group/comp.lang.java.gui/browse_frm/thread/d881f
ce23550a166

you want to implement the TableModelListener interface. There's an
example here:

http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#mod
elchange

You can then loop through the table elements as RGB thoughtfully showed
you. You might then enable or disable your button accordingly. When
that's working, you should revisit the issue of the table model as
Michael Rauscher and I suggested.

[Please don't multi-post.]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top