JtextField setText

Hi guys,

Me yet again!!

I'm trying to set a JTextfield text to a number, the data going in is an int. I did this kind of code a while back, but due to a massive hard disk crash, I lost most of my work.

I know I can't cast it, but I seem to recall it was something to do with parseInt, but I don't remember the syntax for it.

Any help will be greatfully received.

[396 byte] By [Emyra] at [2007-10-3 9:55:57]
# 1
Interger.parseInt();
zadoka at 2007-7-15 5:13:56 > top of Java-index,Desktop,Core GUI APIs...
# 2

Hi Zadok,

Thanks for your reply.

I tried what you suggested, however I can't seem to get it right. I've posted the method code below. I'm trying to add up a set of values in a JTable column, and display them in the textField.

public void addPrices()

{

for(int i = 0; i < 40; i++)

{

String t = "0";

table.getModel().getValueAt(i, 2);

Integer.parseInt(t);

totalofshop.setText(t);

}

}

Many thanks

Emyra at 2007-7-15 5:13:56 > top of Java-index,Desktop,Core GUI APIs...
# 3

Do this instead:

public void addPrices()

{

for(int i = 0; i < 40; i++)

{

String t = "0";

table.getModel().getValueAt(i, 2);

totalofshop.setText(Integer.parseInt(t));

}

}

Aslo what does this line do:

table.getModel().getValueAt(i, 2);

zadoka at 2007-7-15 5:13:56 > top of Java-index,Desktop,Core GUI APIs...
# 4

Hi Zadok,

The only hassle I have (appart from a whopping great big exception coming up) is that i only returns the index, not the value at the index for some reason.

Any ideas? thus far I've drawn a blank. The code in the ResultModel is as follows (Example borrowed from a book)

// how that data rows are stored

private Vector<String[]> datarows = new Vector<String[]>();

public String getValueAt(int row, int column)

{

return datarows.elementAt(row)[column];

}

Message was edited by:

Emyr

Emyra at 2007-7-15 5:13:56 > top of Java-index,Desktop,Core GUI APIs...
# 5
You variable 't' always has a value of "0".> table.getModel().getValueAt(i, 2);Above line does nothing, it should be:t = table.getModel().getValueAt(i, 2);
camickra at 2007-7-15 5:13:56 > top of Java-index,Desktop,Core GUI APIs...
# 6
Ive had to set t to be an object, is there a command to change it to an int?would the parseInt command work?Emyr
Emyra at 2007-7-15 5:13:56 > top of Java-index,Desktop,Core GUI APIs...
# 7

> is there a command to change it to an int? would the parseInt command work?

Assuming you store the data as a String in the TableModel then you would cast the Object to a String before using the Integer.parseInt(...) method.

This is Java basics and has nothing to do with tables.

camickra at 2007-7-15 5:13:56 > top of Java-index,Desktop,Core GUI APIs...
# 8

Hi there folks

I got to the bottom of it in the end, and here's my solution should anyone run into it again.

Basically what it does, is that it takes all the values from the price column of a JTable that I have, and adds them all up.

Thanks to all of you who helped me. Again I am in your debt.

public void addPrices()

{

float g = 0;

Object o = new Object();

for(int i = 0; i < table.getRowCount(); i++)

{

o = table.getModel().getValueAt(i, 2);

g = g + Float.parseFloat(o.toString());

totalofshop.setText(Float.toString(g));

}

}

}

null

Emyra at 2007-7-15 5:13:56 > top of Java-index,Desktop,Core GUI APIs...