我有两个JTable
组件(主表(tableA
)和详细信息表(tableB
),我需要使用向上/向下箭头键在主表中移动并获取详细数据表。
我正在使用的代码如下:
private void jTable1KeyPressed(java.awt.event.KeyEvent evt) {
if (evt.getKeyCode() == KeyEvent.VK_UP ||evt.getKeyCode() == KeyEvent.VK_DOWN) {
MoveMasterdetail();
}
}
private static final String UP = "UP";
private static final String DOWN = "DOWN";
public void MoveMasterdetail() {
int condition = jTable1.WHEN_IN_FOCUSED_WINDOW;
InputMap inputMap = jTable1.getInputMap(condition);
ActionMap actionMap = jTable1.getActionMap();
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), UP);
actionMap.put(UP, new AbstractAction() {
public void actionPerformed(ActionEvent arg0) {
fetchDetail(jTable1.getValueAt(jTable1.getSelectedRow(), 0).toString().trim());
}
});
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), DOWN);
actionMap.put(DOWN, new AbstractAction() {
public void actionPerformed(ActionEvent arg0) {
fetchDetail(jTable1.getValueAt(jTable1.getSelectedRow(), 0).toString().trim());
}
});
}
public void fetchDetail(String autonationalno) {
try {
Connection dbConnection = null;
PreparedStatement preparedStatementSelect = null;
ResultSet rs = null;
dbConnection = getDBConnection();
if (dbConnection != null) {
int i = 0;
//inner join or outer join
String sql =
"select * from tableB";
preparedStatementSelect = dbConnection.prepareStatement(sql);
rs = preparedStatementSelect.executeQuery();
jTable2.setModel(DbUtils.resultSetToTableModel(rs));
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
请为此提供解决方案。
答案 0 :(得分:0)
我需要按上下方向键...
如果用户想要表最后一行的详细信息怎么办?
他们不必为了查看最后一行的详细信息而滚动整个表,尤其是当您对详细数据进行数据库查询时。用户应该能够使用鼠标或键盘来选择他们想要详细信息的行。
这就是<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id = "textDiv"></div>
<div id = "newPlayer">blah blah"</div>
是更好的解决方案的原因。阅读有关How to Write a ListSelectionListener的Swing教程中的部分,以获取入门的实用示例。