gpt4 book ai didi

java - JTable:选择一行时从数据库中获取值

转载 作者:搜寻专家 更新时间:2023-10-30 21:44:17 24 4
gpt4 key购买 nike

我有三个文件,TopicData、TopicView、TopicTableModel。我的程序使用数据库中的值显示一个表。现在,当我单击一行时,会打印该行的索引。 我想修改代码,改为打印我数据库中的 topicID。topicID 的值存储在 ArrayList 中但不显示在表中,因此我不能使用 JTable.getValueAt( ).

请指教如何修改我的代码。提前致谢。

更多信息:

  1. TopicData 从数据库中获取数据并将其存储在 ArrayList 中。

  2. 然后将 ArrayList 传递给 TopicTableModel,其中数据适合在 JTable 中显示。

  3. TopicView 创建一个 JTable 并接收 TopicTableModel 以生成 JTable。

主题数据.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;
}

主题表模型.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
}

}

主题 View .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;
}

最佳答案

您正在以艰难的方式做到这一点。无需将 TopicData 转换为数组,只需让您的 TableModel 直接读取 TopicData 对象的 ArrayList,因此每个 TopicData 对应一行。

主题表模型.java:

import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

public class TopicTableModel extends AbstractTableModel {

private static final long serialVersionUID = 1L;
private String[] columnNames = {"Name", "Date", "User"};
private ArrayList<TopicData> data;

public TopicTableModel(ArrayList<TopicData> listOfObjects) {
data = listOfObjects;
}

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

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

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

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
switch (column) {
case 0:
return data.getName();
case 1:
return data.getDate();
case 2:
return data.getUser();
default:
throw new ArrayIndexOutOfBoundsException();
}
}

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

public TopicData getTopic(int row) {
return data.get(row);
}

}

通过对 TopicView.java 进行一些小的修改,您现在可以获取所选行的 TopicData 并打印其 ID。

主题 View .java:

import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;


public class TopicView {
JTable table;

private JTable getTable() {
if (table == null) {
TopicData topic= new TopicData();
final 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);
table.setDefaultRenderer(TopicData.class, new TopicDataTableCellRenderer());

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

int row = table.getSelectedRow();
if (row < 0) {
System.out.println("No rows are selected.");
}
else {
System.out.println("id " + tableModel.getTopic(row).getId() + " is now selected.");
}
}
});
}
return table;
}
}

关于java - JTable:选择一行时从数据库中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14284378/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com