import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.KeyStroke;public class s extends javax.swing.JFrame { /** Creates new form NewJFrame1 */
public s() {
initComponents();
} @SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); AbstractAction moveForward = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
int rowCount=jTable1.getRowCount();
int colCount=jTable1.getColumnCount(); int selRow=jTable1.getSelectedRow();
int selCol=jTable1.getSelectedColumn();
int yingdonggeshu=0; do{yingdonggeshu=yingdonggeshu+1;
if(selCol==colCount-1)
{
if(selRow==rowCount-1)
{
selCol=-1;
selRow=0;
}
else
{
selCol=-1;
selRow++;
}
} }
while ((jTable1.getModel().isCellEditable(selRow, selCol+yingdonggeshu))==false); jTable1.changeSelection(selRow,selCol+yingdonggeshu,false,false);
jTable1.editCellAt(selRow,selCol+yingdonggeshu);
if(jTable1.getEditorComponent()!=null)
jTable1.getEditorComponent().requestFocus();
}
};
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null, null}
},
new String [] {
"能编辑", "Title 2", "能编辑4", "标题 4", "标题 5", "标题 6", "标题 15", "标题 18", "能编辑9"
}
) {
boolean[] canEdit = new boolean [] {
true, false, true, false, false, true, false, false, true
}; public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jTable1.setColumnSelectionAllowed(true);
jTable1.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0),"moveForward");
jTable1.getActionMap().put("moveForward",moveForward);
jScrollPane1.setViewportView(jTable1);
jTable1.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
jTable1.getColumnModel().getColumn(0).setResizable(false);
jTable1.getColumnModel().getColumn(1).setResizable(false);
jTable1.getColumnModel().getColumn(2).setResizable(false);
jTable1.getColumnModel().getColumn(3).setResizable(false); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 452, Short.MAX_VALUE)
.addGap(314, 314, 314))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 275, Short.MAX_VALUE)
.addGap(25, 25, 25))
); pack();
}// </editor-fold> public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() { public void run() {
new NewJFrame1().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration
}
上面代码东拼西凑 实现了 用回车代替tab 把jtable 单元格的焦点 移动到下一个可编辑的单元格,有点问题 为啥 第一个单元格输入数据 回车 后 就直接能 跳到下个可编辑的单元格,
以后输入数据后就必须在单元格里面点击两次回车 才能跳到下格单元格
里面呢????
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.KeyStroke;public class s extends javax.swing.JFrame { /** Creates new form NewJFrame1 */
public s() {
initComponents();
} @SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); AbstractAction moveForward = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
int rowCount=jTable1.getRowCount();
int colCount=jTable1.getColumnCount(); int selRow=jTable1.getSelectedRow();
int selCol=jTable1.getSelectedColumn();
int yingdonggeshu=0; do{yingdonggeshu=yingdonggeshu+1;
if(selCol==colCount-1)
{
if(selRow==rowCount-1)
{
selCol=-1;
selRow=0;
}
else
{
selCol=-1;
selRow++;
}
} }
while ((jTable1.getModel().isCellEditable(selRow, selCol+yingdonggeshu))==false); jTable1.changeSelection(selRow,selCol+yingdonggeshu,false,false);
jTable1.editCellAt(selRow,selCol+yingdonggeshu);
if(jTable1.getEditorComponent()!=null)
jTable1.getEditorComponent().requestFocus();
}
};
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null, null}
},
new String [] {
"能编辑", "Title 2", "能编辑4", "标题 4", "标题 5", "标题 6", "标题 15", "标题 18", "能编辑9"
}
) {
boolean[] canEdit = new boolean [] {
true, false, true, false, false, true, false, false, true
}; public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jTable1.setColumnSelectionAllowed(true);
jTable1.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0),"moveForward");
jTable1.getActionMap().put("moveForward",moveForward);
jScrollPane1.setViewportView(jTable1);
jTable1.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
jTable1.getColumnModel().getColumn(0).setResizable(false);
jTable1.getColumnModel().getColumn(1).setResizable(false);
jTable1.getColumnModel().getColumn(2).setResizable(false);
jTable1.getColumnModel().getColumn(3).setResizable(false); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 452, Short.MAX_VALUE)
.addGap(314, 314, 314))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 275, Short.MAX_VALUE)
.addGap(25, 25, 25))
); pack();
}// </editor-fold> public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() { public void run() {
new NewJFrame1().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration
}
上面代码东拼西凑 实现了 用回车代替tab 把jtable 单元格的焦点 移动到下一个可编辑的单元格,有点问题 为啥 第一个单元格输入数据 回车 后 就直接能 跳到下个可编辑的单元格,
以后输入数据后就必须在单元格里面点击两次回车 才能跳到下格单元格
里面呢????
jTable1.getEditorComponent().requestFocus();
问题就出在你这个requestFocus上面。你把它去掉就是你要的效果。
至于其他的,我先不说。自己体会一下吧