我有三个文件,topicdata,topicview,topictablemodel。我的程序使用数据库中的值显示一个表。现在,当我单击一行时,将打印该行的索引。我想修改代码,以便打印数据库中的topicid。topicid的值存储在arraylist中,但不显示在表中,因此我不能使用jtable.getvalueat()。
请告诉我如何修改我的代码。提前谢谢。
更多信息:
topicdata从数据库中获取数据并将其存储在arraylist中。
然后,arraylist被传递给topictablemodel,使数据适合在jtable中显示。
topicview创建一个jtable并接受topictable模型来生成jtable。
topicdata.java

public class TopicData {
int id;
String name;
String date;
String category;
String user;

public TopicData(){
}

public TopicData(int id, String name, String date, String category, String user) {
    this.id = id;
    this.name = name;
    this.date = date;
    this.category = category;
    this.user = user;
}



public TopicData(String name, String date, String category, String user) {
    this.name = name;
    this.date = date;
    this.category = category;
    this.user = user;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getDate() {
    return date;
}

public void setDate(String date) {
    this.date = date;
}

public String getCategory() {
    return category;
}

public void setCategory(String category) {
    this.category = category;
}

public String getUser() {
    return user;
}

public void setUser(String user) {
    this.user = user;
}


public ArrayList<TopicData> getTopicList(){
    ArrayList<TopicData> topicList = new ArrayList<TopicData>();
    ResultSet rs = null;
    DBController db = new DBController();
    db.setUp("myDatabase");
    String dbQuery = "SELECT topicID, topicName, topicDate, topicCategory, topicUser FROM topicTable ORDER BY topicDate";

    rs = db.readRequest(dbQuery);

    try{
        while(rs.next()){
            int id = rs.getInt("topicID");
            String name = rs.getString("topicName");
            String date = rs.getString("topicDate") ;
            String category = rs.getString("topicCategory");
            String user = rs.getString("topicUser");

            TopicData topic = new TopicData (id, name, date, category, user);
            topicList.add(topic);
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    db.terminate();
    return topicList;
}

topictablemodel.java
public class TopicTableModel extends AbstractTableModel {

private static final long serialVersionUID = 1L;
private int rowCount, colCount;
private String[] columnNames = {"Name", "Date", "User"};
private Object [][] data;

public TopicTableModel(ArrayList<TopicData> listOfObjects) {
    rowCount = listOfObjects.size();
    colCount = columnNames.length;
    data = new Object[rowCount][colCount];

    for (int i = 0; i < rowCount; i++) {
        //Copy an ArrayList element to an instance of MyObject
        TopicData topic = (listOfObjects.get(i));
        data[i][0] = topic.getName();
        data[i][1] = topic.getDate();
        data[i][2] = topic.getUser();
    }
}

@Override
public int getColumnCount() {
    // TODO Auto-generated method stub
    return colCount;
}

@Override
public int getRowCount() {
    // TODO Auto-generated method stub
    return rowCount;
}

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

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
    // TODO Auto-generated method stub
    return data[rowIndex][columnIndex];
}

@Override
public boolean isCellEditable(int rowIndex, int colIndex) {
    return false; //Disallow the editing of any cell
}

}

topicview.java网站
private JTable getTable() {
    if (table == null) {
        TopicData topic= new TopicData();
        TopicTableModel tableModel = new TopicTableModel(topic.getTopicList());
        table = new JTable(tableModel);

        table.setShowGrid(false);
        table.setFillsViewportHeight(true);
        table.setBounds(173, 87, 456, 263);
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.getTableHeader().setReorderingAllowed(false);
        table.getTableHeader().setResizingAllowed(false);
        table.getColumnModel().getColumn(0).setPreferredWidth(500);

        ListSelectionModel rowSM = table.getSelectionModel();
        rowSM.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                //Ignore extra messages.
                if (e.getValueIsAdjusting()) return;

                ListSelectionModel lsm = (ListSelectionModel)e.getSource();
                if (lsm.isSelectionEmpty()) {
                    System.out.println("No rows are selected.");
                }
                else {
                    int selectedRow = lsm.getMinSelectionIndex();
                    System.out.println("Row " + selectedRow + " is now selected.");
                }
            }
        });
    }
    return table;
}

最佳答案

你实际上什么都有。
您只需更改TableModel,使其保留ArrayList,而不是将该列表转换为Object[][]
类似这样的东西(可能有一些打字错误):

public class TopicTableModel extends AbstractTableModel {

private static final long serialVersionUID = 1L;
private int rowCount, colCount;
private String[] columnNames = {"ID", "Name", "Date", "User"};
private List<TopicData> listOfObjects;
public TopicTableModel(ArrayList<TopicData> listOfObjects) {
    this.listOfObjects = listOfObjects;
}

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

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

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

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
    TopicData data = listOfObjects.get(rowIndex);
    switch(columnIndex) {
        case 0:
            return data.getId();
        case 1:
            return data.getName();
        case 2:
            return data.getDate();
        case 3:
            return data.getUser();
    }
    return null;
}

@Override
public boolean isCellEditable(int rowIndex, int colIndex) {
    return false; //Disallow the editing of any cell
}

}

副作用:如果您使用Java序列化(或利用TopicData的其他技术),则需要使您的SerializableSerializable

10-04 10:44
查看更多