wood burning stoves*
The moose likes Swing / AWT / SWT and the fly likes JTable Trouble Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable Trouble" Watch "JTable Trouble" New topic
Author

JTable Trouble

Al Swensen
Greenhorn

Joined: May 02, 2006
Posts: 17

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!
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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?


There is no emoticon for what I am feeling!
Al Swensen
Greenhorn

Joined: May 02, 2006
Posts: 17

True, but having a textfield hold data kinda makes it stand out (of course, if it's a different color than the rest of the table). Basically, I'm trying to redo an old project that I started with JavaScript by using Java instead. It's supposed to be a calculator-like application that does a calculation based on the "double" values in the array and outputs it into the textfield... I'll still be able to do that without a textfield, right?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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.
Al Swensen
Greenhorn

Joined: May 02, 2006
Posts: 17

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 ]
Al Swensen
Greenhorn

Joined: May 02, 2006
Posts: 17

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?

Here's my test array:


and here's the loop that I'm currently trying to use which gets the value in the third cell over, divides a variable by it, and then sets the value of the fourth cell over:


Sorry if it's a stupid question, this whole JTable thing is kinda confusing!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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:
Al Swensen
Greenhorn

Joined: May 02, 2006
Posts: 17

Thanks! I tried that and the program compiled, but when I run it I get a " java.lang.IllegalArgumentException: Cannot format given Object as a Number" exception... why would that happen?

EDIT: Figured that out. Stupid me, I put an extra "i++" in my for loop.

Thanks for your help!
[ May 18, 2006: Message edited by: Al Swensen ]
Al Swensen
Greenhorn

Joined: May 02, 2006
Posts: 17

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.

Here's the class that I have:

Thanks again for any help!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JTable Trouble