简体   繁体   English

从jTable中的几个选定行中获取数据

[英]Getting data from several selected rows in a jTable

I need to get different values from a jTable, according to the selected rows (by the user). 我需要根据所选行(由用户)从jTable获取不同的值。
I guess the code I already have should do that, but it always shows the same nr in all rows. 我想我已经拥有的代码应该做到这一点,但是它总是在所有行中显示相同的nr。
Can you guys get me some help ? 你们能帮我些帮助吗?

int linha  = Tabela.getSelectedRowCount();
Gerar ger = modelo.getGerar(linha);
System.out.println(linha);

for(int j = 0; j < linha; j ++) 
{
    int nr = ger.getNr();
    System.out.println(nr);

    char[] chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHJIKLMNOPQRSTUVWXYZ0123456789".toCharArray();
    StringBuilder sb = new StringBuilder();
    Random random = new Random();
    for (int i = 0; i < 8; i++) 
    {
        char c = chars[random.nextInt(chars.length)];
        sb.append(c);
    }

    String output = sb.toString();
    System.out.println(output);

    Gerar novo = new Gerar(nr, output);
    modelo.adicionaNovoGerar(novo);
}

Update: 更新:

public class Gerar implements Serializable 
{
    private Integer Nr;
    private String Passe;

    public Gerar(Integer Nr, String Passe) 
    {
        this.Nr = Nr;
        this.Passe = Passe;
    }

    public Integer getNr() 
    {
        return Nr;
    }

    public void setNr(Integer Nr) 
    {
        this.Nr = Nr;
    }
}
 int linha  = Tabela.getSelectedRowCount();

This will give the no of selected Rows not the list of selected rows. 这将给出选定行数而不是选定行的列表。

For which you have to go with getSelectedRows 为此,您必须使用getSelectedRows

Tabela.getSelectedRows();

which will returns the indices of all selected rows[return an integer array int[] ] 它将返回所有选定行的索引[返回整数数组int[] ]

And you are assigning the selectedRow count to class ger 并且您正在将selectedRow计数分配给ger

 Gerar ger = modelo.getGerar(linha);

and inside the for loop you are calling 在for循环中

int nr = ger.getNr();

which will return the same value as many times you call, as the value is set before forloop 该值将返回您多次调用的相同值,因为该值是在forloop之前设置的

This is the way I do it: 这是我的方法:

int[] selectedRows = table.getSelectedRows();
if (selectedRows.length != 0) {
    for (int counter : selectedRows) {
        String value= table.getModel().getValueAt(counter, Constants.COLUMN).toString();
        /*
        * do stuff
        */
    }
}

And to give some context, I used this with a JTable that had info about the files in a folder. 为了提供一些上下文,我将其与JTable一起使用,该JTable包含有关文件夹中文件的信息。 The app permitted me to delete files so, I used that piece of code to retrieve the name of the files selected and to delete them one by one. 该应用程序允许我删除文件,因此,我使用那段代码来检索所选文件的名称,然后将它们一个一个地删除。

In your case (i think) this line: 就您而言(我认为),此行:

String value= table.getModel().getValueAt(counter, Constants.COLUMN).toString();

will turn into this: 会变成这样:

Gerar ger = modelo.getGerar(counter);

NB: I strongly recommend the use of a lower case letter as the first letter of an attribute( ex: private Integer Nr; -> private Integer nr;) 注意:我强烈建议使用小写字母作为属性的首字母(例如:private Integer Nr;-> private Integer nr;)

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

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