Hi, I'm working on a project that displays data in a JTable (int, String, double, and a JTextfield). Everything is working fine, but I can't seem to get the JTextField to work correctly... it just shows up as "javax.swing.JTextField..." and I can't seem to get it working. I've tried reading the tutorial on the Java site but I only found info about changing the editor to something like a combo box or a checkbox... nothing about having it display a text field from the start. Can anyone help? Thanks!
I'm confused. The data that your JTable's TableModel holds tends to be, well, data. Types like String, Integer, Date, currency values, Boolean. I don't undestand why the data value would be a JTextField versus a String. Don't confuse the data with how you might *edit* the data.
So why do you think your data should be a JTextField?
JTable's editor is a JTextField by default, so you don't have to do anything to have that happen. Just put a String into your table model. Putting a JTextField into your table model is, well, just weird.
Ok, I did that but I set up an ActionListener for a button to try it out, but the cell's value doesn't change unless you click the cell. I'd like it to change when the button is pressed. I'm using setValueAt and this is what the method looks like:
EDIT: Nevermind, I got it! I just had to add in "fireTableCellUpdated(row, col);" to the setValueAt method, and now it works great! [ May 16, 2006: Message edited by: Al Swensen ]
Sorry for the double post, but I didn't want to start a new topic about this. I just hit a roadblock while working on this... I want to be able to get a value from one cell in the table (which is a double in the array), and divide another variable (another double) by it. But apparently all values in the cells are Objects so I can't. How would I divide one variable by a cell's value?
It's not just JTable that's giving you problems, there's other issues.
The entries in the table model are Objects in the sense that the getValue() method returns an Object reference. But that's just because it can't know what type of Object you actually put in there. You put a Double in column 3 (number 2 in Java's oddball numbering system) so it's still a Double when you get it out. So just cast it:Now to convert that to something you can do arithmetic with, you need to make it a double (the primitive type corresponding to the object reference type Double) by calling the Double's doubleValue() method:Finally you'll need to convert that back into an object reference type to put it into column 4:
Bah, one more question. It's kinda related to my last one... Now I'm trying to take the value of the first cell in each row and compare it to a variable when the "calculate" button gets pressed. If the variable is smaller than the value in the cell, the cell should become red. I've set up a CustomTableCellRenderer which sorta works, but it sets the background before the variable can be set and I'm not sure how to make it so that it works how I'd like it to.
Your cell renderer can and will be called at any time. It will be called if you change the cell being rendered, it will even be called if you minimize and then restore your application.
So if you're going to use the renderer to set different background colours, the code should always set a background colour. As it is, your renderer can change a cell's colour to red, but it has no way of changing it back if the cell's value changes. You just need another linethat goes before the code that might set the background to red. That way the renderer always reliably sets the background colour of the cell.
Who knew that furniture could be so violent? Put this tiny ad out there to see what happens: