简体   繁体   English

JTable获取所有正在编辑的行

[英]JTable get all rows that are being edited

I have a JTable which has 2 columns 我有一个JTable有2列

  • column 0 username 第0列用户名
  • column 1 password. 第1栏的密码。

for the password column it will be encrypted to SHA256. 对于密码列,它将被加密为SHA256。

Basically what I want to achieve is it will update all the rows in my password column to SHA256 that I have edited after my button is pressed. 基本上,我想要实现的是它将在按下按钮后将密码列中的所有行更新为我已编辑的SHA256。

so.. 所以..

I have a RowData class, this will store the text being edited and the position of the text being edited(rows,columns). 我有一个RowData类,它将存储正在编辑的文本和正在编辑的文本的位置(行,列)。

public class RowData {
    int rows = 0, columns = 0;
    String text = " ";  
    public RowData(String text,int rows, int columns) {
        setEditedRows(rows);
        setEditedColumns(columns);
        setEditedText(text);
    }

    public int getEditedRows() {
        return rows;
    }

    public int getEditedColumns() {
        return columns;
    }

    public String getEditedText() {
        return text;
    }

    public void setEditedRows(int rows) {
        this.rows = rows;
    }

    public void setEditedColumns(int columns) {
        this.columns = columns;
    }

    public void setEditedText(String text) {
        this.text = text;
    }
}

I wrote a TableModelListener.. I have an List to store the text and the rows and columns after the table has changed 我写了一个TableModelListener。我有一个List,用于在表更改后存储文本以及行和列

 table.getModel().addTableModelListener(new TableModelListener() {
            @Override
            public void tableChanged(TableModelEvent e) {
                int row = e.getFirstRow();
                int column = e.getColumn();
                TableModel model = (TableModel) e.getSource();
                //System.out.println(model.getValueAt(row, column));
                if(column == 1) {
                    String data = (String) model.getValueAt(row, column);
                    System.out.println(data);
                    dataList.add(new RowData(data,row,column));
                }

            }
        });

In my button I loop through the list and retrieve the rows, and columns and text and set the password to SHA256 to the JTable. 在我的按钮中,我遍历列表并检索行,列和文本,并将JTable的密码设置为SHA256。

updateBtn.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            if (table.getCellEditor() != null) {
                table.getCellEditor().stopCellEditing();    
                for(int i = 0; i < dataList.size(); i++) {
                    String text = dataList.get(i).getEditedText();
                    int rows = dataList.get(i).getEditedRows();
                    int columns =  dataList.get(i).getEditedColumns();
                    //System.out.println(dataList.get(i).getEditedText() + " " +  dataList.get(i).getEditedRows() + dataList.get(i).getEditedColumns());
                    table.setValueAt(convertPassword.convertToSHA256(text), rows ,columns);
                }
            }
        }
    }); 

The result I get is I will keep printing the password endlessly in my console. 我得到的结果是我将不断在控制台中不断打印密码。 So I think that my logic is wrong and needed to be corrected. 所以我认为我的逻辑是错误的,需要纠正。

table.setValueAt(convertPassword.convertToSHA256(text), rows ,columns);

When you change the TableModel the TableModelListener will be invoked again. 更改TableModel时,将再次调用TableModelListener。 The TableModelListener is invoked whether you change the data by using the JTable or by updating the TableModel directly. 无论您是通过使用JTable还是通过直接更新TableModel来更改数据,都将调用TableModelListener。

The solution would be to remove the TableModelListener when you click on your button, at the start of your ActionListener. 解决方案是在ActionListener的开头单击按钮时删除TableModelListener。 You would then need to add the TableModelListener back to the TableModel at the end of the code in case the user make further changes. 然后,您需要在代码末尾将TableModelListener添加回TableModel,以防用户进行进一步的更改。

Another solution is to have 3 columns in the TableModel, username, password and sha256Password. 另一个解决方案是在TableModel中具有3列,用户名,密码和sha256Password。 Then you can use the JTable to display only the first two columns. 然后,您可以使用JTable仅显示前两列。 See the removeColumn() method of JTable. 请参见JTable的removeColumn()方法。 Then your conversion code would update the TableModel using: 然后,您的转换代码将使用以下方法更新TableModel:

table.getModel().setValueAt(value, row, 2);

Now the code in your TableModel will be invoked, but because you check for updates to column 1, nothing will happen when you update column 2. 现在,将调用TableModel中的代码,但是由于您检查第1列的更新,因此更新第2列不会发生任何事情。

Then when you save the data you save the data from the TableModel. 然后,当您保存数据时,您将从TableModel中保存数据。

Edit: 编辑:

I must click into another cell before I can press my button to edit. 我必须先单击另一个单元格,然后才能按我的按钮进行编辑。

You need to stop the cell editing. 您需要停止单元格编辑。 See Table Stop Editing for a couple of solutions. 有关几种解决方案,请参见表停止编辑

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM