-3

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.

user3260589
  • 168
  • 1
  • 4
  • 12

2 Answers2

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.

  1. Highlight/select the table from design view.
  2. Go to the properties window on the right.
  3. Click the ... on the right of the model property
  4. 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
  • Are you getting a compile error or a runtime exception? – Paul Samsotha 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
  • What does the error say when highlight it? – Paul Samsotha Feb 03 '14 at 11:08
  • Did you change the name to `jTable2` instead of `jTbable1`? – Paul Samsotha Feb 03 '14 at 11:09
  • `.. (DefaultTableModel)jTable2.getModel();` – Paul Samsotha Feb 03 '14 at 11:09
  • "Cannot find symbol" Yep, I've already changed it – user3260589 Feb 03 '14 at 11:11
  • 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