java jtabel双击_无需用户点击即可编辑JTable停止单元格

我正在尝试用我的程序解决一个奇怪的问题 . 该程序创建了一系列GUI和JTable,使用户能够生成XML文件 . 其中一个表用于创建“语句” . 我不会详细说明,除非说数据存储在多个2D数组中,而这些数组又存储在哈希映射中 .

这是发生了什么 . 当用户进入Statement屏幕时,使用2D阵列中的内容生成JTable . 此数据填充用户可以修改的单元格 . 这些细胞中的一个(并且最重要的)是量 . 他们为行设置的金额与另一个类别的金额非常匹配 .

表格底部是“完成”按钮 . 当用户单击此按钮时,逻辑将检查金额是否匹配 . 如果他们这样做,那么程序将使用任何更改的值更新2D数组并处理JTable .

我的问题是,一旦用户更新单元格并单击“完成”,最后所做的更新就不起作用 . 基本上用户必须先点击表格中的其他位置,然后点击完成 . 我希望这个动作自动发生,以便当用户点击“完成”时,单元格编辑停止 . 这是完成按钮的代码:

finishedButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent ae){

//Creates another table model for the finished button logic.

DefaultTableModel dm = (DefaultTableModel)StatementGUI.tbl.getModel();

//Gets the total number of table rows.

int rows = dm.getRowCount();

//Creates a variable to store the statement transaction total.

double statementTransactionTotal=0;

//For each row in the table.

for(int i = 0; i < dm.getRowCount(); i++){

//Gets the total of all transactions in the table.

String currentTotal = tbl.getValueAt(i, 3).toString();

Double currentTotalDouble = Double.parseDouble(currentTotal);

statementTransactionTotal=statementTransactionTotal+currentTotalDouble;

}

//Creates a decimal format and applies the statement total.

DecimalFormat df = new DecimalFormat("0.00");

String currentTotalDF = df.format(statementTransactionTotal);

//Stops editing on the table so that the data can be used.

if(null != tbl.getCellEditor()){

tbl.getCellEditor().stopCellEditing();

}

//If the statement total matches the transaction total..

if(currentTotalDF.matches(ClearedMainGUI.currentTransactionAmount)){

//For each row in the table..

for(int i = 0; i < dm.getRowCount(); i++){

//Will replace the hash/array value with the table value.

ClearedMainGUI.Transactions.get(ClearedMainGUI.selectedRow)[i][0]=tbl.getValueAt(i, 0).toString();

ClearedMainGUI.Transactions.get(ClearedMainGUI.selectedRow)[i][1]=tbl.getValueAt(i, 1).toString();

ClearedMainGUI.Transactions.get(ClearedMainGUI.selectedRow)[i][2]=tbl.getValueAt(i, 2).toString();

ClearedMainGUI.Transactions.get(ClearedMainGUI.selectedRow)[i][3]=tbl.getValueAt(i, 3).toString();

ClearedMainGUI.Transactions.get(ClearedMainGUI.selectedRow)[i][4]=tbl.getValueAt(i, 4).toString();

ClearedMainGUI.Transactions.get(ClearedMainGUI.selectedRow)[i][5]=tbl.getValueAt(i, 5).toString();

ClearedMainGUI.Transactions.get(ClearedMainGUI.selectedRow)[i][6]=tbl.getValueAt(i, 6).toString();

ClearedMainGUI.Transactions.get(ClearedMainGUI.selectedRow)[i][7]=tbl.getValueAt(i, 7).toString();

}

//For each row in the table..

for(int i = rows - 1; i >=0; i--){

//Removes the current row so the table will be empty next time.

dm.removeRow(i);

}

//Removes the frame and goes back to the previous GUI.

frame.dispose();

//If the statement total and transaction total do not match..

}else{

JOptionPane.showMessageDialog(null, "The statement total entered: $"+statementTransactionTotal+" " +

"does not match the transaction total of: $"+ClearedMainGUI.currentTransactionAmount);

}

}

});

我认为我的问题在于这一行:

if(null != tbl.getCellEditor()){

tbl.getCellEditor().stopCellEditing();

}

只有在用户在编辑单元格后单击表格的另一个区域时,这似乎才有效 .

我很感激帮助!

要更改Swing JTable单元格颜色,您可以使用JTable上的TableCellRenderer接口。这个接口允许您自定义单元格的呈现方式,包括单元格的背景和前景颜色。 下面是一个简单的例子,演示如何更改JTable单元格的颜色: ``` import java.awt.Color; import java.awt.Component; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.DefaultTableModel; public class JTableColorExample extends JFrame { public JTableColorExample() { // 创建表格模型和表格 DefaultTableModel model = new DefaultTableModel(new Object[][]{ {"A", 1}, {"B", 2}, {"C", 3}, {"D", 4} }, new Object[]{"Name", "Value"}); JTable table = new JTable(model); // 创建单元格渲染器 DefaultTableCellRenderer renderer = new DefaultTableCellRenderer() { @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { // 调用父类的方法获取默认的单元格渲染组件 Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); // 如果值为奇数,设置背景为红色,否则为绿色 if ((Integer) value % 2 == 1) { c.setBackground(Color.RED); } else { c.setBackground(Color.GREEN); } return c; } }; // 设置单元格渲染器 table.setDefaultRenderer(Object.class, renderer); // 将表格添加到窗口中 add(new JScrollPane(table)); // 设置窗口属性并显示 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400, 300); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { new JTableColorExample(); } } ``` 在这个例子中,我们创建了一个JTable,其中包含两列数据:名称和值。然后,我们创建了一个DefaultTableCellRenderer的子类,重写了getTableCellRendererComponent方法来更改单元格的背景颜色,使奇数单元格为红色,偶数单元格为绿色。最后,我们将此单元格渲染器设置为表格的默认单元格渲染器。 运行这个例子,您应该看到一个带有不同颜色单元格JTable
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值