Re: Displaying the conten
To: comp.lang.java.gui
I've added this 'change listener' now to the code, but it seems to actvery
slowly, and sometimes doesnt act at all...
For instance I'll make an entry in one of the cells, and then press Enter,
some 10- 20 seconds later then the change is seemingly registered. Any idea
why?
The code now appears 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 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.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e) {
System.out.println("change");
}
});
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);
}
}
"Thomas A. Russ" <tar@sevak.isi.edu> wrote in message
news:ymiwsccptdy.fsf@blackcat.isi.edu...
"Jeremy Watts" <rturytr@jhfhgfd.com> writes:
Actually also, how would you recommend that I am able to check whether
all
cells are filled by the user? Is there some sort of listener for this
eventuality?
There isn't any direct listener for "all cells filled", so you would
have to write your own code to check for that. Now, if you wanted to be
clever, you could probably fold that functionality into your own custom
table model.
But for starters, you could register a TableModelListener on your table
model and just iterate over all entries and make sure that they have
values. You don't have to be all that clever about it, you could just
do this every time anything changes. Since users will be entering the
information by hand, I don't imagine that the matrices will be all that
big.
And then you should, of course, link that criterion to the
enabled/disabled state of your "declare it" button. Only enable the
button if all the data is present.
--
Thomas A. Russ, USC/Information Sciences Institute
---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24