JTable中DefaultTableModel空行及Last_Name列无数据问题求助
Hey there, let's work through this issue step by step. You mentioned that after adding model.setRowCount(0) to fix the empty initial rows, your Last_Name column stopped displaying data. Here are the most likely causes and how to resolve them:
1. Check Column Order & Mismatch with Data Index
First, verify that the column order of your TeacherTable matches the order you're inserting data into the row array.
Your code puts Last_Name at index 2 of the row array:
row[2] = list.get(i).getLast_Name();
But if your TeacherTable's columns are ordered differently (e.g., ID → First_Name → Phone → Last_Name → Experience), then the Last_Name data is being inserted into the wrong column (which might be hidden or labeled incorrectly).
To confirm, add a quick debug print to check your table's column names and their indices:
DefaultTableModel model = (DefaultTableModel)TeacherTable.getModel(); for (int col = 0; col < model.getColumnCount(); col++) { System.out.println("Column " + col + ": " + model.getColumnName(col)); }
If the column index for Last_Name isn't 2, adjust your row array assignment to match the correct index.
2. Verify the getLast_Name() Method in Your Teacher Class
A common culprit is a broken getter method. Let's confirm it's returning valid data:
Add a debug print inside your loop to check what getLast_Name() returns:
for(int i = 0; i<list.size();i++){ // Add this line to check the output System.out.println("Teacher " + i + " Last Name: " + list.get(i).getLast_Name()); row[0] = list.get(i).getID(); row[1] = list.get(i).getFirst_Name(); row[2] = list.get(i).getLast_Name(); row[3] = list.get(i).getPhone(); row[4] = list.get(i).getExperience(); model.addRow(row); }
If the print shows empty strings or null, check:
- Did you correctly assign the
last_Name(orlastName) property when creating Teacher objects? - Is there a typo in the getter method (e.g.,
getLastName()instead ofgetLast_Name())? - Does the getter method actually return the correct property (not a different variable)?
3. Ensure setRowCount(0) is Called at the Right Time
While this might not directly cause the Last_Name issue, it's good practice to clear the model before fetching new data to avoid any leftover state. Adjust your code like this:
public void show_teachers(){ DefaultTableModel model = (DefaultTableModel)TeacherTable.getModel(); // Clear existing rows first model.setRowCount(0); ArrayList<Teacher> list = TeacherList(); Object[] row = new Object[5]; for(int i = 0; i<list.size();i++){ row[0] = list.get(i).getID(); row[1] = list.get(i).getFirst_Name(); row[2] = list.get(i).getLast_Name(); row[3] = list.get(i).getPhone(); row[4] = list.get(i).getExperience(); model.addRow(row); } }
4. Double-Check Table-Model Binding
Make sure your TeacherTable is actually using the DefaultTableModel you're modifying. If you initialized the table with a different model, or changed it elsewhere in your code, your updates won't reflect in the UI. Confirm you have code like this somewhere in your initialization:
String[] columnNames = {"ID", "First Name", "Last Name", "Phone", "Experience"}; DefaultTableModel model = new DefaultTableModel(columnNames, 0); TeacherTable.setModel(model);
Quick Side Note
As a best practice, follow Java's naming conventions: use camelCase for methods (e.g., getLastName() instead of getLast_Name()) and variables. This reduces the chance of typos and makes your code more readable.
内容的提问来源于stack exchange,提问作者Dan

