Hey guys! I need help...

I have a text field and table that I made on NetBeans. I'm working on GUI. Anyway, in this text field, the user inputs his/her name. And the user's name is displayed on the "User 1" heading of the table:

jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"Monday", null, null, null, null},
                {"Tuesday", null, null, null, null},
                {"Wednesday", null, null, null, null},
                {"Thursday", null, null, null, null},
                {"Friday", null, null, null, null},
                {"Saturday", null, null, null, null}
            },
            new String [] {
                "Day", "User 1", "User 2", "Free time", "Duration"
            }

So, instead of seeing "User 1 " as a heading, the name is shown. I was thinking of making a method "getText" which gets the String from the text field, and passing it to the table.. But idk. Can someone please help me?

Get Data from TextField into Vector.. add that vector as row in Table...

Vector rowList = new Vector<Object>(5);// no of columns...
rowList.add(day.getText());
rowList.add(user1.getText());
rowList.add(user2.getText());
rowList.add(free.getText());
rowList.add(duration.getText());
tableModel.addRow(rowList);

How do I add vector as a row? Like this:

jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"Monday", null, null, null, null},
                {"Tuesday", null, null, null, null},
                {"Wednesday", null, null, null, null},
                {"Thursday", null, null, null, null},
                {"Friday", null, null, null, null},
                {"Saturday", null, null, null, null}
            },
            new String [] {
                "Day", "User 1", "User 2", "Free time", "Duration"
            }
Vector rowList = new Vector<Object>(5);// no of columns...
rowList.add(day.getText());
rowList.add(user1.getText());
rowList.add(user2.getText());
rowList.add(free.getText());
rowList.add(duration.getText());
tableModel.addRow(rowList);
}
jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"Monday", null, null, null, null},
                {"Tuesday", null, null, null, null},
                {"Wednesday", null, null, null, null},
                {"Thursday", null, null, null, null},
                {"Friday", null, null, null, null},
                {"Saturday", null, null, null, null}
            },
            new String [] {
                "Day", "User 1", "User 2", "Free time", "Duration"
            }

Instead of this... Make DefaultTableModel separetly

DefaultTableModel tableModel = new DefaultTableModel();

JTable table = new JTable();
table.setModel(tableModel);

then add row in tableModel..

Edited 5 Years Ago by Vaspar: n/a

Ahh ok. So it ends up looking like this:

DefaultTableModel tableModel = new DefaultTableModel();

JTable table = new JTable();
table.setModel(tableModel);
Vector rowList = new Vector<Object>(5);// no of columns...
rowList.add(day.getText());
rowList.add(user1.getText());
rowList.add(user2.getText());
rowList.add(free.getText());
rowList.add(duration.getText());
tableModel.addRow(rowList);

So, how do I accomplish this part of the code:

new Object [][] {
                {"Monday", null, null, null, null},
                {"Tuesday", null, null, null, null},
                {"Wednesday", null, null, null, null},
                {"Thursday", null, null, null, null},
                {"Friday", null, null, null, null},
                {"Saturday", null, null, null, null}
            },
            new String [] {
                "Day", "User 1", "User 2", "Free time", "Duration"
            }

Do I just leave that out as well?

First Define columnList,,,, Means Column Title.. (Caption)

Vector <Object> columnList = new Vector<Object>(5);
columnList.add("Day");
columnList.add("User1");
columnList.add("User2");
columnList.add("Free Time");
columnList.add("Duration");

Then open Your Java File in NotePad...

Remove previous code for creating DefaultTableModel,..

use new Code....

tableModel.setColumnIdentifiers(columnList);

save it and execute....


you will use ComboBox for days??

The combo box will have the times that the user will pick, and will appear under "User1".

This article has been dead for over six months. Start a new discussion instead.