gpt4 book ai didi

java - jTable 移动到下一个指定列被中断

转载 作者:行者123 更新时间:2023-12-02 02:05:43 25 4
gpt4 key购买 nike

我正在尝试使用以下给定的方法在选定行的表格列中移动:

//on Enter key press
KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
my_table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(enter, "nextColumn");

my_table.getActionMap().put("nextColumn", new AbstractAction() {

@Override
public void actionPerformed(ActionEvent ae) {

if (itemSelected) {
//cost column
if (my_table.editCellAt(my_table.getSelectedRow(), 4)) {
System.out.println("column cost editing...");
}
itemSelected = false;
} else {

if (my_table.isCellEditable(my_table.getSelectedRow(), 4)) {
//qty column
System.out.println("column qty editing...");
my_table.editCellAt(my_table.getSelectedRow(), 6);
//It breaks from here
} else if (my_table.isCellEditable(my_table.getSelectedRow(), 6)) {
//free_qty column
System.out.println("column free_qty editing...");
my_table.editCellAt(my_table.getSelectedRow(), 7);
} else if (my_table.isCellEditable(my_table.getSelectedRow(), 7)) {
//dis % column
System.out.println("column dis % editing...");
my_table.editCellAt(my_table.getSelectedRow(), 8);
} else if (my_table.isCellEditable(my_table.getSelectedRow(), 8)) {
//free_qty column
System.out.println("column free_qty editing...");
DefaultTableModel model = (DefaultTableModel) my_table.getModel();
model.addRow(new Vector());
my_table.setRowSelectionInterval(1, 1);
}
}

}
});

但是,当我在 qty 列 处按 Enter 键时,它不会移动到下一个指定的 free_qty 列

enter image description here

任何建议都会受到重视。

更新

完整的可编译示例

/**
*
* @author Madushan
*/
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.Vector;
import javax.swing.AbstractAction;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.table.DefaultTableModel;

// double click on the Code column to start
public class JTableCreatingDemo {

private static boolean itemSelected = true;

public static void main(String args[]) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JTable table = new JTable();
table.setModel(new DefaultTableModel(
new Object[][]{
{1, "PR000003", "Sample Item", "Grams", 0.00, 0, 0, 0, 0.0, 0.00, 0.00}
},
new String[]{
"#", "Code", "Name", "Unit", "Cost", "Prev. Bal.", "Qty", "Free Qty", "Dis %", "Dis amount", "Amount"
}
) {
boolean[] canEdit = new boolean[]{
true, true, false, false, true, false, true, true, true, true, false
};

public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit[columnIndex];
}
});

KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(enter, "nextColumn");

table.getActionMap().put("nextColumn", new AbstractAction() {

@Override
public void actionPerformed(ActionEvent ae) {

if (itemSelected) {
//cost column
if (table.editCellAt(table.getSelectedRow(), 4)) {
System.out.println("column cost editing...");
}
itemSelected = false;
} else {

if (table.isCellEditable(table.getSelectedRow(), 4)) {
//qty column
System.out.println("column qty editing...");
table.editCellAt(table.getSelectedRow(), 6);
} else if (table.isCellEditable(table.getSelectedRow(), 6)) {
//free_qty column
System.out.println("column free_qty editing...");
table.editCellAt(table.getSelectedRow(), 7);
} else if (table.isCellEditable(table.getSelectedRow(), 7)) {
//dis % column
System.out.println("column dis % editing...");
table.editCellAt(table.getSelectedRow(), 8);
} else if (table.isCellEditable(table.getSelectedRow(), 8)) {
//free_qty column
System.out.println("column free_qty editing...");
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.addRow(new Vector());
table.setRowSelectionInterval(1, 1);
}
}
}
});

JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane, BorderLayout.CENTER);
frame.setSize(800, 200);
frame.setVisible(true);

}
}

最佳答案

} else {

if (table.isCellEditable(table.getSelectedRow(), 4)) {

上面的条件始终为真,因为您总是从第四列开始检查。

您真正想要做的是创建一个循环,迭代表中的所有列,从 table.getSelectedColumn() + 1 开始,直到找到下一个可编辑列。

关于java - jTable 移动到下一个指定列被中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50828154/

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