problems with jtables in netbeans

Hi,

I am trying to use netbeans to create a jtable. After goggling the topic.. it seems the most common response is "don't use netbeans to create jtables"

I am not really happy with that answer.. I would like to see if anyone can point me in the right direction.

So in netbeans, I click on MODEL.. and that lets me change the column names and the number of rows I have. That works fine.

However during execution of my program, I plan to read a text file and slice up each row, and put it in this jTable. The problem is.. you can't call addRow() on a jTable, you must call it on a model. So I am looking and looking through the netbean created code.. and it never gives the model a name.

In all the sun code.. they create a new model().. and then use that to addRow to. But if my netbeans code doesn't have a way to do this.. it makes netbeans useless.

I know there is a way around it, but I surely can't figure it out. Any ideas?

I have a simple table with 3 columns.. each one takes strings.. the table is called jTable2.. and I need to insert three strings, say "aaa", "bbb", and "ccc". How do I do this?

[1159 byte] By [TunaBooa] at [2007-11-27 9:22:16]
# 1
I don't use NetBeans to create JTables ;-)(There probably aren't a lot of people here who will know either - the members of this forum, from my experience, are not very endeared to NetBeans, but you may eventually get an answer.)
Navy_Codera at 2007-7-12 22:16:38 > top of Java-index,Java Essentials,New To Java...
# 2
Its not the 90s anymore, I can't be arsed to lay out UIs by hand. So its either J# or netbeans.. have both installed, was trying to give java the benefit of the doubt...
TunaBooa at 2007-7-12 22:16:38 > top of Java-index,Java Essentials,New To Java...
# 3

> Its not the 90s anymore, I can't be arsed to lay out

> UIs by hand. So its either J# or netbeans.. have

> both installed, was trying to give java the benefit

> of the doubt...

If you're going to go with .NET, then go with C#. J# is ancient history (talk about mid 90s...)

Navy_Codera at 2007-7-12 22:16:38 > top of Java-index,Java Essentials,New To Java...
# 4
Well J# actually has a decent IDE that lets you fully use a common feature such as a table ;P(neatbeans prolly does, but I am not finding the option)
TunaBooa at 2007-7-12 22:16:38 > top of Java-index,Java Essentials,New To Java...
# 5
> Well J# actually has a decent IDE that lets you fully> use a common feature such as a table ;P> > (neatbeans prolly does, but I am not finding the> option)Hold on, let me take a look...
Navy_Codera at 2007-7-12 22:16:38 > top of Java-index,Java Essentials,New To Java...
# 6

Okay - I just looked at it.

In NetBeans 6 ... I added a JTable to a JScrollPane.

Clicked on model [...]

Which brought up a dialog box with two tabs, "Table Settings" and "Default Values"

Table Settings lets me customize my columns.

Default values lets me adjust the number of rows, etc.

Isn't that what you're trying to do?

Navy_Codera at 2007-7-12 22:16:38 > top of Java-index,Java Essentials,New To Java...
# 7

Well, I already did that. The columns are fixed, and I got them set up. What I want to do is adjust the row count as I go.

What I was trying to do is as I read a text file.. dynamically add a row for each row I came across.

I found a semi hack for this.. I can do

jTable2.setValueAt("hi",1,1); for example to set the value at 1,1 to Hi.

So if I make this table have 20,000 rows.. I can just go element by element and set this. But

1) This seems like it will be slow as ****

2) what if the file has 20,001 elements

Maybe if I can just find a way to add a blank row.. I can uee lots of setValueAt to set the table...

hum

TunaBooa at 2007-7-12 22:16:38 > top of Java-index,Java Essentials,New To Java...
# 8

> Its not the 90s anymore, I can't be arsed to lay out

> UIs by hand. So its either J# or netbeans.. have

> both installed, was trying to give java the benefit

> of the doubt...

I agree, use netbeans when you want to quickly produce a prototype app, but if you want to do a finished polished project like the professionals do, well just ask how many of the pros use netbeans for their finished product.

Then tell us if you don't want to also learn how to do swing by hand.

petes1234a at 2007-7-12 22:16:39 > top of Java-index,Java Essentials,New To Java...
# 9

I can create apps using the win32ai or even x86 assembly code.. where it may take 100 and 600 lines, respectively, to make a window with a big HI in the middle.

But if this is an app to used by a secretary.. why would I not just use C# or something and be done with it in 6 seconds?

The fact that the IDE for java has shortcomings only hurts the language. But anywho the suckiness of netbeans is not the point of the post ;P

TunaBooa at 2007-7-12 22:16:39 > top of Java-index,Java Essentials,New To Java...
# 10
You can do it in NetBeans, this is all nonsense. Find the initialization code in the Properties and create your own custom initialization code that uses your own TableModel object.
ejpa at 2007-7-12 22:16:39 > top of Java-index,Java Essentials,New To Java...
# 11
more details please ;)
TunaBooa at 2007-7-12 22:16:39 > top of Java-index,Java Essentials,New To Java...
# 12
Open the NetBeans Properties window of the JTable. Find the Code properties tab. Change the initialization code to Custom code as suggested above.
ejpa at 2007-7-12 22:16:39 > top of Java-index,Java Essentials,New To Java...
# 13

Hum I don't want to do it on form load though, only later when someone hits a certain button. Doesn't that initialization code make in run as soon as the form loads?

For a form that already has a jTable created with netbeans.. how do I add a blank row.. if I can figure that out, I can make it work.

The normal code to do this, would be

JTable table = new JTable(..);

DefaultTableModel tableModel =(DefaultTableModel) table.getModel();

tableModel.addRow(rowData);

trying to make this work.. we'll see

Message was edited by:

TunaBoo

TunaBooa at 2007-7-12 22:16:39 > top of Java-index,Java Essentials,New To Java...
# 14
Create your own model, maybe empty. Set the custom table initialization code to use your model. Update your model when you load files.
ejpa at 2007-7-12 22:16:39 > top of Java-index,Java Essentials,New To Java...
# 15

I got this code to work... using some vectors...

DefaultTableModel tableModel =(DefaultTableModel)jTable2.getModel();

Vector vec = new Vector();

vec.addElement("hi");

vec.addElement("hi");

vec.addElement("hi");

tableModel.addRow(vec);

Adds a new row with values of hi, hi, hi

TunaBooa at 2007-7-21 22:59:35 > top of Java-index,Java Essentials,New To Java...