2016-04-22 67 views
0

我在使用自定義表格模型和自定義鏈接列表創建JTable時遇到問題。我希望有一些簡單的變化,我錯過了。自定義鏈接列表存在問題並應用於JTable

我的主類:

import java.util.ArrayList; 
import java.util.*; 
import java.io.*; 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.table.*; 
public class StudentInterface { 
public static void main(String[] args){ 
     new StudentInterface(); 
    } 
    //create a linked list  
    MyLinkedList studentPack = new MyLinkedList(); 

public StudentInterface(){ 
    //create jFrame 
    JFrame jFrame = new JFrame("Student Information Interface"); 
    jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    jFrame.setSize(400,300); 
    jFrame.setVisible(true); 
    jFrame.setDefaultLookAndFeelDecorated(true); 
    jFrame.setPreferredSize(new Dimension(600, 400)); 

    //create content panes 
    JPanel headerPanel = new JPanel(); 
    JPanel tablePanel = new JPanel(); 
    JPanel buttonPanel = new JPanel(); 
    JPanel combinedPanel = new JPanel(); 
    headerPanel.setLayout(new BorderLayout()); 
    tablePanel.setLayout(new FlowLayout()); 
    buttonPanel.setLayout(new FlowLayout()); 
    combinedPanel.setLayout(new BorderLayout()); 

    //add label to header content panel 
    JLabel headerLabel1 = new JLabel("Welcome to the Student Information Interface"); 
    JLabel headerLabel2 = new JLabel("***Click a cell to edit data***"); 
    JLabel headerLabel3 = new JLabel("***Click a column header to sort***"); 
    headerLabel1.setHorizontalAlignment(JLabel.CENTER); 
    headerLabel2.setHorizontalAlignment(JLabel.CENTER); 
    headerLabel3.setHorizontalAlignment(JLabel.CENTER); 
    headerPanel.add(headerLabel1, BorderLayout.NORTH); 
    headerPanel.add(headerLabel2, BorderLayout.CENTER); 
    headerPanel.add(headerLabel3, BorderLayout.SOUTH); 

    //Where the GUI is created: 
    JMenuBar menuBar; 
    JMenu menu, submenu; 
    JMenuItem exitMenuItem; 
    JMenuItem saveMenuItem; 
    JMenuItem loadMenuItem; 

    //Create the menu bar. 
    menuBar = new JMenuBar(); 

    //Build the main menu. 
    menu = new JMenu("File"); 
    menu.setMnemonic(KeyEvent.VK_A); 
    menu.getAccessibleContext().setAccessibleDescription("The only menu in this program that has menu items"); 
    menuBar.add(menu); 

    //create menu items 
    exitMenuItem = new JMenuItem("Exit",KeyEvent.VK_T); 
    menu.add(exitMenuItem); 
    jFrame.setJMenuBar(menuBar); 

    //create table 
    tableModel model; 
    model = new tableModel(new MyLinkedList()); 
    JTable table = new JTable(model); 

    //add list to list content panel 
    JScrollPane scrollPane = new JScrollPane(table); 
    scrollPane.setPreferredSize(new Dimension(580, 400)); 
    tablePanel.add(scrollPane); 

    //add buttons to button content panel 
    JButton addButton = new JButton("Add"); 
    JButton removeButton = new JButton("Remove"); 
    buttonPanel.add(addButton); 
    buttonPanel.add(removeButton); 

    addButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
     //verify pack is not full 
     int packSize = studentPack.size(); 
     if (packSize >= 10) 
      { 
      JOptionPane.showMessageDialog(null, 
      "The interface is full. Please remove a student first.", 
      "Interface Full", 
      JOptionPane.WARNING_MESSAGE); 
      return; 
      } 

     JPanel p1 = new JPanel(new GridLayout(2,1)); 
     JTextField t1 = new JTextField(); 
     JLabel labell = new JLabel("Please enter the first name of the student you would like to add:"); 
     p1.add(labell); 
     p1.add(t1); 
     JOptionPane.showMessageDialog(null,p1,"First Name",JOptionPane.PLAIN_MESSAGE); 
     String userFirstName = t1.getText(); 

     JPanel p2 = new JPanel(new GridLayout(2,1)); 
     JTextField t2 = new JTextField(); 
     JLabel label2 = new JLabel("Please enter the last name of the student you would like to add:"); 
     p2.add(label2); 
     p2.add(t2); 
     JOptionPane.showMessageDialog(null,p2,"Last Name",JOptionPane.PLAIN_MESSAGE); 
     String userLastName = t2.getText(); 

     JPanel p3 = new JPanel(new GridLayout(2,1)); 
     JTextField t3 = new JTextField(); 
     JLabel label3 = new JLabel("Please enter the major of the student you would like to add:"); 
     p3.add(label3); 
     p3.add(t3); 
     JOptionPane.showMessageDialog(null,p3,"Major",JOptionPane.PLAIN_MESSAGE); 
     String userMajor = t3.getText(); 

     JPanel p4 = new JPanel(new GridLayout(2,1)); 
     JTextField t4 = new JTextField(); 
     JLabel label4 = new JLabel("Please enter the NetID of the student you would like to add:"); 
     p4.add(label4); 
     p4.add(t4); 
     JOptionPane.showMessageDialog(null,p4,"NetID",JOptionPane.PLAIN_MESSAGE); 
     String userNetID = t4.getText(); 

     JPanel p5 = new JPanel(new GridLayout(2,1)); 
     JTextField t5 = new JTextField(); 
     JLabel label5 = new JLabel("Please enter the gender of the student you would like to add:"); 
     p5.add(label5); 
     p5.add(t5); 
     JOptionPane.showMessageDialog(null,p5,"Gender",JOptionPane.PLAIN_MESSAGE); 
     String userGender = t5.getText(); 

     JPanel p6 = new JPanel(new GridLayout(2,1)); 
     JTextField t6 = new JTextField(); 
     JLabel label6 = new JLabel("Please enter the UIN of the student you would like to add:"); 
     p6.add(label6); 
     p6.add(t6); 
     JOptionPane.showMessageDialog(null,p6,"UIN",JOptionPane.PLAIN_MESSAGE); 
     String userUIN = t6.getText(); 

     JPanel p7 = new JPanel(new GridLayout(2,1)); 
     JTextField t7 = new JTextField(); 
     JLabel label7 = new JLabel("Please enter the age of the student you would like to add:"); 
     p7.add(label7); 
     p7.add(t7); 
     JOptionPane.showMessageDialog(null,p7,"Age",JOptionPane.PLAIN_MESSAGE); 
     String userAgeInput = t7.getText(); 
     int userAge = Integer.parseInt(userAgeInput); 

     JPanel p8 = new JPanel(new GridLayout(2,1)); 
     JTextField t8 = new JTextField(); 
     JLabel label8 = new JLabel("Please enter the GPA of the student you would like to add:"); 
     p8.add(label8); 
     p8.add(t8); 
     JOptionPane.showMessageDialog(null,p8,"GPA",JOptionPane.PLAIN_MESSAGE); 
     String userGPAInput = t8.getText(); 
     double userGPA = Double.parseDouble(userGPAInput); 

     //create student Student 
     Student studentStudent = new Student(userFirstName, userLastName, userMajor, userGPA, userUIN, userNetID, userAge, userGender); 

     //store Student in linked list 
     model.addStudent(studentStudent); 
     } 
    }); 

    removeButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
     //verify pack is not full 
     if (studentPack.size() == 0) 
      { 
      JOptionPane.showMessageDialog(null, 
      "The interface is empty. Please add a student first.", 
      "Interface Empty", 
      JOptionPane.WARNING_MESSAGE); 
      return; 
      } 

     JPanel p9 = new JPanel(new GridLayout(2,1)); 
     JTextField t9 = new JTextField(); 
     JLabel label9 = new JLabel("Please enter the first name of the student you would like to remove: **Case Sensitive**"); 
     p9.add(label9); 
     p9.add(t9); 
     JOptionPane.showMessageDialog(null,p9,"First Name",JOptionPane.PLAIN_MESSAGE); 
     String userFirstName = t9.getText(); 

     JPanel p10 = new JPanel(new GridLayout(2,1)); 
     JTextField t10 = new JTextField(); 
     JLabel label10 = new JLabel("Please enter the last name of the student you would like to remove: **Case Sensitive**"); 
     p10.add(label10); 
     p10.add(t10); 
     JOptionPane.showMessageDialog(null,p10,"First Name",JOptionPane.PLAIN_MESSAGE); 
     String userLastName = t10.getText(); 

     //remove student Student in linked list 
     } 
    }); 

    exitMenuItem.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      System.exit(0); 
     } 
    }); 

    //nest button and list panels in combined content panel 
    combinedPanel.add(headerPanel, BorderLayout.NORTH); 
    combinedPanel.add(tablePanel, BorderLayout.CENTER); 
    combinedPanel.add(buttonPanel, BorderLayout.SOUTH); 

    //add combined content panel to frame 
    jFrame.add(combinedPanel); 
    jFrame.pack(); 
    jFrame.setLocationRelativeTo(null); 

    JOptionPane.showMessageDialog(null, 
    "***Click a cell to edit data*** \n ***Click a column header to sort***", "How to edit/sort data", JOptionPane.INFORMATION_MESSAGE); 
    } 

public class tableModel extends AbstractTableModel { 

public final String[] columnNames = { "First Name", "Last Name", "Major:", "GPA", "UIN", "NetID", "Age", "Gender" }; 
private MyLinkedList data; 

public tableModel(MyLinkedList data) { 
    this.data = data; 
} 

@Override 
public int getColumnCount() { 
    return columnNames.length; 
} 

@Override 
public String getColumnName(int column) { 
    return columnNames[column]; 
} 

@Override 
public int getRowCount() { 
    return data.size(); 
} 

public void addStudent(Student student) { 
     int row = data.size(); 
     data.add(student); 
     fireTableRowsInserted(row, row); 
    } 

public void removeStudent(int i) { 
     int row = data.size(); 
     data.remove(i); 
     fireTableRowsDeleted(row, row); 
    } 

@Override 
public Student getValueAt(int rowIndex, int columnIndex) { 
    Object student = data.getNode(rowIndex); 
    if (student == null) { 
     return null; 
    } 
    switch (columnIndex) { 
    case 0: 
     return student.getFirstName(); 
    case 1: 
     return student.getLastName(); 
    case 2: 
     return student.getMajor(); 
    case 3: 
     return student.getGPA(); 
    case 4: 
     return student.getUIN(); 
    case 5: 
     return student.getNetID(); 
    case 6: 
     return student.getAge(); 
    case 7: 
     return student.getGender(); 
    default: 
     return null; 
    } 
} 
} 
} 

這裏是我的學生對象類:

public class Student { 
    // Declare attributes here 
    String firstName; 
    String lastName; 
    String major; 
    double gpa; 
    String uin; 
    String netid; 
    int age; 
    String gender; 

    public Student(String newFirstName, String newLastName, String newMajor, double newGPA, String newUIN, String newNetID, int newAge, String newGender){ 
     setFirstName(newFirstName); 
     setLastName(newLastName); 
     setMajor(newMajor); 
     setGPA(newGPA); 
     setUIN(newUIN); 
     setNetID(newNetID); 
     setAge(newAge); 
     setGender(newGender); 
    } 

    //Mutators 
    public void setFirstName(String newFirstName) { 
     firstName = newFirstName; 
    } 

    public void setLastName(String newLastName) { 
     lastName = newLastName; 
    } 

    public void setMajor(String newMajor) { 
     major = newMajor; 
    } 

    public void setGPA(double newGPA) { 
     gpa = newGPA; 
    } 

    public void setUIN(String newUIN) { 
     uin = newUIN; 
    } 

    public void setNetID(String newNetID) { 
     netid = newNetID; 
    } 

    public void setAge(int newAge) { 
     age = newAge; 
    } 

    public void setGender(String newGender) { 
     gender = newGender; 
    } 

    //Accessors 
     public String getFirstName() { 
     return firstName; 
    } 

     public String getLastName() { 
     return lastName; 
    } 

     public String getMajor() { 
     return major; 
    } 

     public double getGPA() { 
     return gpa; 
    } 

     public String getUIN() { 
     return uin; 
    } 

     public String getNetID() { 
     return netid; 
    } 

     public int getAge() { 
     return age; 
    } 

     public String getGender() { 
     return gender; 
    } 
} 

這裏是我的返工鏈表類:

public class MyLinkedList<Student>{ 
    private Node<Student> lastNode; 
    private int nodeCount; 

    public MyLinkedList(){ 
     this.lastNode = null; 
     this.nodeCount = 0; 
    } 

    public int size() { 
     return nodeCount; 
    } 

    public boolean isEmpty() { 
     return this.nodeCount == 0; 
    } 

    public void add(Student student) { 
     Node<Student> currentNode = new Node<Student>(student); 
     if (this.lastNode != null) { 
      currentNode.index = lastNode.index + 1; 
      currentNode.previousNode = lastNode; 
      lastNode.nextNode = currentNode; 
     }else { 
      currentNode.previousNode = null; 
      currentNode.index = 0; 
     } 
     this.lastNode = currentNode; 
     this.nodeCount++; 
    } 

    public Student get(int index){ 
     //error handling 
     if(this.isEmpty() || index < 0 || index > this.nodeCount){ 
      return null; 
     } 
     // 
     Node<Student> currentNode; 
     int count = lastNode.index - index; 
     currentNode = lastNode; 

     while (count > 0) { 
      currentNode = currentNode.previousNode; 
      count--; 
     } 

     return currentNode.student; 
    } 

    public Node<Student> getNode(int index){ 
     //error handling 
     if(this.isEmpty() || index < 0 || index > this.nodeCount){ 
      return null; 
     } 
     // 
     int count = lastNode.index - index; 
     Node<Student> currentNode = lastNode; 

     while (count > 0){ 
      currentNode = currentNode.previousNode; 
      count--; 
     } 

     return currentNode; 
    } 

    public boolean insert(Student student, int index){ 
     Node<Student> currentNode; 

     if (this.getNode(index) != null) 
      { 
      Node<Student> newNode = new Node<Student>(student); 
      currentNode = this.getNode(index); 
      newNode.index = index; 

      if (null != currentNode.previousNode) 
       { 
       currentNode.previousNode.nextNode = newNode; 
       newNode.previousNode = currentNode.previousNode; 
       } 

      currentNode.previousNode = newNode; 
      newNode.nextNode = currentNode; 
      currentNode = newNode; 

      while (currentNode.nextNode != null) 
       { 
       currentNode = currentNode.nextNode; 
       currentNode.index++; 
       } 

       this.nodeCount++; 
       return true; 
      } 
     else 
      { 
      return false; // error handling 
      } 
    } 

    public boolean remove(int index){ 
     Node<Student> currentNode; 

     if (this.getNode(index) != null){ 
      currentNode = this.getNode(index); 

      if(currentNode.previousNode != null) { 
       currentNode.nextNode.previousNode = currentNode.previousNode; 
       currentNode.previousNode.nextNode = currentNode.nextNode; 
      } else if (currentNode.nextNode != null){ 
       currentNode.nextNode.previousNode = null; 
      } else if (this.isEmpty()){ 
       this.lastNode = null; 
      } 

      while (currentNode.nextNode != null){ 
       currentNode = currentNode.nextNode; 
       currentNode.index--; 
      } 

      this.nodeCount--; 
      return true; 
     }else { 
      return false; 
     } 
    } 

    private static class Node<Student>{ 
    public Node<Student> nextNode = null; 
    public Node<Student> previousNode = null; 
    public int index; 
    public Student student; 

    public Node(Student student){ 
     this.student = student; 
    } 
    } 
} 
+0

請編輯您的問題我包括一個單獨關注單個問題的[mcve]。 – trashgod

回答

1

這...

public class tableModel extends AbstractTableModel { 
    //...  
    @Override 
    public Student getValueAt(int rowIndex, int columnIndex) { 
     Object student = data.getNode(rowIndex); 
     if (student == null) { 
      return null; 
     } 
     switch (columnIndex) { 
      case 0: 
       return student.getFirstName(); 
      case 1: 
       return student.getLastName(); 
      case 2: 
       return student.getMajor(); 
      case 3: 
       return student.getGPA(); 
      case 4: 
       return student.getUIN(); 
      case 5: 
       return student.getNetID(); 
      case 6: 
       return student.getAge(); 
      case 7: 
       return student.getGender(); 
      default: 
       return null; 
     } 
    } 
} 

給我一個編譯器錯誤,因爲studentObjectObject沒有任何您嘗試使用的方法。

顯而易見的解決辦法是使用...

@Override 
public Student getValueAt(int rowIndex, int columnIndex) { 
    Student student = data.getNode(rowIndex); 

好了,現在有兩個問題。首先getValueAt不應該返回Student,它應該返回Object和兩個getNode回報Node<Student>

所以相反,它可能應該更多的東西一樣......

@Override 
public Object getValueAt(int rowIndex, int columnIndex) { 
    Student student = data.get(rowIndex); 

好了,但現在這是造成一個編譯器錯誤,但爲什麼? get返回一個Student

我將問題追查到public class MyLinkedList<Student> { ......相反,如果我們將其作爲public class MyLinkedList {,所有錯誤都會消失。

MyLinkedList實際上並不需要一般的簽名<Student>時,編譯器將其視爲「通用」標記,並在你的代碼Object替換它,所以get方法成爲public Object get(int index)作爲MyLinkedList實例沒」噸仿製藥提供要從中開始基本對象...是啊:P

現在,一些可以產生最終...

Show me the table

+0

是的!非常感謝您通過此MadProgrammer與我保持聯繫! – JimBob117