jTable problem

hello,

I am creating one jFrame , in that I create one jpanel. I am using netbeans.

I put one jTable and one jButton.

for my work, when I click add button, I need to add one row in the jTable. I try it in netbeans. but, table is not added.

please anyone give me some example codes for dynamically adding rows when we click button.

[361 byte] By [rameshsa] at [2007-11-26 23:41:44]
# 1
Are you using DefaultTableModel? If so, use one of its addRow() methods. If not, use whatever equivalent method your model supports, if any.
itchyscratchya at 2007-7-11 15:09:38 > top of Java-index,Desktop,Core GUI APIs...
# 2
Check http://www.exampledepot.com/egs/javax.swing.table/AppendRow.html
it_senthilkumara at 2007-7-11 15:09:38 > top of Java-index,Desktop,Core GUI APIs...
# 3

hello,

actually, I am using"DefaultTableModel model;'.

but in this , addRow only add objects. but I have string value when I get from textfield.

so, i need to convert this string to object and put it in addRow.

how can i convert the string to object.

or is it possible to add the jTextField string to jTable rows one by one.

.

rameshsa at 2007-7-11 15:09:38 > top of Java-index,Desktop,Core GUI APIs...
# 4
A String is an Object.
itchyscratchya at 2007-7-11 15:09:38 > top of Java-index,Desktop,Core GUI APIs...
# 5

hello,

I know about it (string is an object);

but, problem is ,

String LETTERS ;

JTable table;

DefaultTableModel model1;

JPanel buttonPanel;

JTextField tf1, tf2, tf3, tf4;

tf1 = new JTextField(10);

model1.addRow(LETTERS1);

F:\src\src\mainPack\MainFrame.java:149: ';' expected

model1.addRow(LETTERS1);

how can I add textfield string to jtable.

i am new to that gui things, jtable. thats why i have problems

rameshsa at 2007-7-11 15:09:38 > top of Java-index,Desktop,Core GUI APIs...
# 6
No, your problems are more fundamental than that :o)But with only selected irrelevant bits of code to go on I've no idea what they are. Check the line above the "addRow" line you've posted, because it's probably wrong.
itchyscratchya at 2007-7-11 15:09:38 > top of Java-index,Desktop,Core GUI APIs...
# 7

hello sir,

i left one line of code.

LETTERS1 = tf1.getText();

String LETTERS ;

JTable table;

DefaultTableModel model1;

JPanel buttonPanel;

JTextField tf1, tf2, tf3, tf4;

tf1 = new JTextField(10);

LETTERS1 = tf1.getText();

model1.addRow(LETTERS1);

the error it shows,

F:\src\src\mainPack\MainFrame.java:149: ';' expected

model1.addRow(LETTERS1);

whats my problem.

rameshsa at 2007-7-11 15:09:38 > top of Java-index,Desktop,Core GUI APIs...
# 8
Where do you declare the variable "LETTERS1"?(Note also that fully-capitalised names are normally used for static finals and it's bad form to use them for variables)
itchyscratchya at 2007-7-11 15:09:38 > top of Java-index,Desktop,Core GUI APIs...
# 9
Working example: http://forum.java.sun.com/thread.jspa?forumID=57&threadID=577919
camickra at 2007-7-11 15:09:38 > top of Java-index,Desktop,Core GUI APIs...
# 10

hello camickr,

thanks for your valuble example.

I used that concepts, but here problem is, I am getting input from JTextField ad string. I create one btton "add" and click that button, that textfield string input must add in that row one by one. But, when I give in put to string and click "add" button, it throws exception (I thing it from addRow() method).

your code is,

button = new JButton( "Add Row" );

buttonPanel.add( button );

button.addActionListener( new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

model.addRow( createRow() );

int row = table.getRowCount() - 1;

table.changeSelection(row, row, false, false);

table.requestFocusInWindow();

}

});

..

.

.

.

.

private Object[] createRow()

{

Object[] newRow = new Object[2];

int row = table.getRowCount() + 1;

newRow[0] = Integer.toString( row );

newRow[1] = LETTERS.substring(row-1, row);

return newRow;

}

here problem is, addRow only accepts Object[] values. When I add text field string input it give error. I am using netbeans.

shall I need to convert that string object into Object{} array or something.

please, give me some explanations.

i my program, I am assiging textfield getText to the String LETTERS1 and try to add it to the row. but, I get error.

what can i do,sir.

Thank you.

rameshsa at 2007-7-11 15:09:38 > top of Java-index,Desktop,Core GUI APIs...