在 JTable 的行上自定义导航
Custom navigation over rows of a JTable
JTable在第一行按Up键如何转到最后一行,在最后一行按Down键如何转到第一行?就像 Enter 键一样,当我们在最后一行按 Enter 键时,它将转到第一行。
我已经完成了这个编码,但它只是将数据显示到文本字段:
private void jtKeyReleased(java.awt.event.KeyEvent evt) {
if(evt.getKeyCode()==KeyEvent.VK_DOWN ||evt.getKeyCode()==KeyEvent.VK_UP){
int row=jt.getSelectedRow();
String TableClick=(jt.getModel().getValueAt(row,0).toString());
try{
String sql="select Product,Roo,TotalStock from pro where
Product='"+TableClick+ "'";
PreparedStatement pst = (PreparedStatement)
conn.prepareStatement(sql);
ResultSet res = pst.executeQuery();
if(res.next()){
String add1=res.getString("Product");
proo.setText(add1);
// String add2=res.getString("Id");
//idd.setText(add2);
String add3=res.getString("Roo");
rooo.setText(add3);
String add4=res.getString("TotalStock");
stkk.setText(add4);
abc=res.getString("TotalStock");
}
} catch(Exception e) {
} //catch
} // if
}
您需要创建两个自定义操作:
- 一个动作从第一行到底部换行并且
- 另一个从底部到顶部环绕的动作。
最简单的方法是利用 JTable 中定义的现有操作。向上键一次向上移动一行,向下键一次向下移动一行。您还可以使用 CTRL+HOME 转到第一行,然后使用 CTRL_END 转到最后一行。
所以我建议从 UP Action 开始,修改它以实现 CTRL+END Action。最简单的方法是利用 Wrapping Actions 的概念。此 class 是现有操作的包装器 class,允许您添加自定义代码以增强操作。
import java.awt.event.*;
import javax.swing.*;
public class UpAction extends WrappedAction implements ActionListener
{
private JTable table;
private Action endAction;
/*
* Specify the component and KeyStroke for the Action we want to wrap
*/
public UpAction(JTable table, KeyStroke keyStroke)
{
super(table, keyStroke);
this.table = table;
endAction = table.getActionMap().get("selectLastRow");
}
/*
* Provide the custom behaviour of the Action
*/
public void actionPerformed(ActionEvent e)
{
if (table.getSelectedRow() == 0)
endAction.actionPerformed( e );
else
invokeOriginalAction( e );
}
private static void createAndShowGUI()
{
JTable table = new JTable(7, 5);
new UpAction(table, KeyStroke.getKeyStroke("UP"));
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new JScrollPane(table) );
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
您需要为 DOWN 功能创建一个类似的操作。请注意,用于选择第一行的操作字符串名称是:selectFirstRow
。查看 Key Bindings 以获取给定组件使用的所有操作的列表。
JTable在第一行按Up键如何转到最后一行,在最后一行按Down键如何转到第一行?就像 Enter 键一样,当我们在最后一行按 Enter 键时,它将转到第一行。
我已经完成了这个编码,但它只是将数据显示到文本字段:
private void jtKeyReleased(java.awt.event.KeyEvent evt) {
if(evt.getKeyCode()==KeyEvent.VK_DOWN ||evt.getKeyCode()==KeyEvent.VK_UP){
int row=jt.getSelectedRow();
String TableClick=(jt.getModel().getValueAt(row,0).toString());
try{
String sql="select Product,Roo,TotalStock from pro where
Product='"+TableClick+ "'";
PreparedStatement pst = (PreparedStatement)
conn.prepareStatement(sql);
ResultSet res = pst.executeQuery();
if(res.next()){
String add1=res.getString("Product");
proo.setText(add1);
// String add2=res.getString("Id");
//idd.setText(add2);
String add3=res.getString("Roo");
rooo.setText(add3);
String add4=res.getString("TotalStock");
stkk.setText(add4);
abc=res.getString("TotalStock");
}
} catch(Exception e) {
} //catch
} // if
}
您需要创建两个自定义操作:
- 一个动作从第一行到底部换行并且
- 另一个从底部到顶部环绕的动作。
最简单的方法是利用 JTable 中定义的现有操作。向上键一次向上移动一行,向下键一次向下移动一行。您还可以使用 CTRL+HOME 转到第一行,然后使用 CTRL_END 转到最后一行。
所以我建议从 UP Action 开始,修改它以实现 CTRL+END Action。最简单的方法是利用 Wrapping Actions 的概念。此 class 是现有操作的包装器 class,允许您添加自定义代码以增强操作。
import java.awt.event.*;
import javax.swing.*;
public class UpAction extends WrappedAction implements ActionListener
{
private JTable table;
private Action endAction;
/*
* Specify the component and KeyStroke for the Action we want to wrap
*/
public UpAction(JTable table, KeyStroke keyStroke)
{
super(table, keyStroke);
this.table = table;
endAction = table.getActionMap().get("selectLastRow");
}
/*
* Provide the custom behaviour of the Action
*/
public void actionPerformed(ActionEvent e)
{
if (table.getSelectedRow() == 0)
endAction.actionPerformed( e );
else
invokeOriginalAction( e );
}
private static void createAndShowGUI()
{
JTable table = new JTable(7, 5);
new UpAction(table, KeyStroke.getKeyStroke("UP"));
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new JScrollPane(table) );
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
您需要为 DOWN 功能创建一个类似的操作。请注意,用于选择第一行的操作字符串名称是:selectFirstRow
。查看 Key Bindings 以获取给定组件使用的所有操作的列表。