I'm using netbeans and there is a table there. I just want to add data to it using a textfield and a button click. I'm just exploring. Please help.
Asked
Active
Viewed 99 times
-3
-
Have you tried anything? (By searching etc)? – Pradeep Simha Feb 03 '14 at 10:11
-
Yeah. All I can see is about database and so on. – user3260589 Feb 03 '14 at 10:13
-
How about this? http://stackoverflow.com/questions/10904316/how-to-add-data-to-jtable-created-in-design-mode – Pradeep Simha Feb 03 '14 at 10:14
-
Is that applicable on netbeans? – user3260589 Feb 03 '14 at 10:17
-
I just want a simple code. – user3260589 Feb 03 '14 at 10:18
-
Yes, it is applicable on Netbeans. – Pradeep Simha Feb 03 '14 at 10:19
-
Then what can I use? For example: Frame2 f = new Frame2(); f.txt.setText(txt1); Is it like that on JTable? – user3260589 Feb 03 '14 at 10:21
2 Answers
1
Example:
Create the columns:
String[] columnNames = {"School Name",
"Module Name",
"Grade",
"# of Years",
"Graduaded"}
Add data:
Object[][] data = {
{"Some High School", "Computing",
"A", new Integer(5), new Boolean(false)},
{"Some other High school", "Maths"
"A", new Integer(3), new Boolean(true)},
};
Constructor:
JTable table = new JTable(data, columnNames);
I'm not using any IDE. I hope that this gives you an insight though. More info at:http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#simple

Stelios Voskos
- 526
- 5
- 17
1
If you want to add rows dynamically, you want to make use of the DefaultTableModel
. By default on NetBeans, the TableModel
given to JTables
are DefaultTableModel
. You can its methods and API here.
What you want to do is use the methos addRow(...)
, which will update your table for you every time you add a row.
You can use the graphical design view to set the headers.
- Highlight/select the table from design view.
- Go to the properties window on the right.
- Click the ... on the right of the model property
- In the Table Setting tab set the rows to 0 and the number of columns you want. You can click on each column to give it a title header.
When you want to add rows dynamically, say after entering text in a few fields you can do something like this
private void jButton1ActionPerformed(java.awt.event.ActionEvent e) {
DefaultTableModel model = (DefaultTableModel)jTable2.getModel();
String firstName = jTextField1.getText();
String mi = jTextField2.getText();
String lastName = jTextField3.getText();
String[] row = {firstName, mi, lastName};
model.addRow(row);
}

Paul Samsotha
- 205,037
- 37
- 486
- 720
-
Where can I put that DefaultTableModel? Sorry, I'm just a beginner. – user3260589 Feb 03 '14 at 10:59
-
Like I said, it's already the default in Netbeans for your table. You're just retrieving it using this `DefaultTableModel model = (DefaultTableModel)jTbable1.getModel();` – Paul Samsotha Feb 03 '14 at 11:01
-
Do you know how to get the `actionPerformed` method into your code? – Paul Samsotha Feb 03 '14 at 11:01
-
Yep, I already know about event. I tried to copy: DefaultTableModel model = (DefaultTableModel)jTbable1.getModel(); But it doesn't work – user3260589 Feb 03 '14 at 11:03
-
Do you have a `jTbable1`? The spelling is messed up. Look at it. Make sure it matched your `Jtable` variable – Paul Samsotha Feb 03 '14 at 11:04
-
I've changed the name to jTable2 because it's what I have. I really don't know what to do. :( sorry – user3260589 Feb 03 '14 at 11:06
-
-
Nope. There's an error on: DefaultTableModel model = (DefaultTableModel)jTbable1.getModel(); [Red underline on DefaultTableModel] – user3260589 Feb 03 '14 at 11:08
-
-
-
-
-
You probably need to import it. Just hit `Ctrl + Shift + I` to resolve the import – Paul Samsotha Feb 03 '14 at 11:11
-
What happens the that GUI Builder uses the fully qualified name with all its classes like `javax.swing.table.DefaultTableModel` and don't use import statements. So when you try and just use `DefaultTableModel`, it can't find the symbol because the package hasn't been imported. I prefer to use imports. But if you wanted to make it work without the imports, you could also use `javax.swing.table.DefaultTableModel` instead of just `DefaultTableModel` – Paul Samsotha Feb 03 '14 at 11:14
-
Yes! It's working!!!! Thank you so much!! Just want to know, what happens when I hit Ctrl+Shift+I ? There will be a code addition? – user3260589 Feb 03 '14 at 11:15
-
Yes if you know about import statements, all that does is add all the imports for you that you need. So when you do that, you will see `import javax.swing.table.DefaultTableModel;` at the top of your code – Paul Samsotha Feb 03 '14 at 11:16