• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

JTable Trouble

 
Greenhorn
Posts: 17
jQuery Firefox Browser PHP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Al Swensen
Greenhorn
Posts: 17
jQuery Firefox Browser PHP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 28323
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
jQuery Firefox Browser PHP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
jQuery Firefox Browser PHP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 28323
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
jQuery Firefox Browser PHP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
jQuery Firefox Browser PHP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 28323
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Whoever got anywhere by being normal? Just ask this exceptional tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic