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
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