简体   繁体   English

如何获取 Java SWT 表中列的值

[英]How to get the values of Columns in Java SWT Table

I would like to ask on how to get the corresponding values from each column from SWT table.我想问一下如何从SWT表的每一列中获取相应的值。 I'm having a hard time to get this one, I hope you can help me this time.我很难得到这个,希望这次你能帮助我。 So my problem is I have columns and I want to get the values of this columns.所以我的问题是我有列,我想获得这些列的值。 Attached here is the screenshot of the sample table on my program这里附上我的程序中示例表的截图

在此处输入图片说明

In this case, I want to get the values from Column1 - Column6 (1250, 2305, 1120,2450,2312,2134) and get the total of it and store it on a Textbox.在这种情况下,我想从 Column1 - Column6 (1250, 2305, 1120,2450,2312,2134) 中获取值并获取它的总数并将其存储在一个文本框上。 I still have a table where there is a checkbox that when you checked it will automatically display the values as shown.我仍然有一个表格,其中有一个复选框,当您选中它时,它会自动显示如图所示的值。 I tried to get the values but it seems its not working with the code that I have.我试图获取这些值,但它似乎不适用于我拥有的代码。

TotalItem= 0L;
    for (x= 0; x < tblPrice.length; x++) { 
            for (int y = 0; y < tblPrice[x].getColumnCount(); y++){ //columns
                if (tblItems[x].getItem(x).getChecked()) {
                    TotalItem = TotalItem+ Long.parseLong(tblPrice[x].getItem(y).getText());
                }
            } 
        }

It's a bit difficult to tell which bit you're struggling with, but I'm going to assume that you're not getting the value back from the table that you want.很难判断您正在努力解决哪个问题,但我将假设您没有从您想要的表中获得价值。

You're calling TableItem#getText() , which will give you the value of the first column in that row.您正在调用TableItem#getText() ,它将为您提供该行中第一列的值。 If you want to get the text at a specific column, you have to call TableItem#getText(int) with the corresponding table column index.如果要获取特定列的文本,则必须使用相应的表列索引调用TableItem#getText(int)

The following example should illustrate this.下面的例子应该说明这一点。 It shows a table with three columns and when you click on a cell, it prints the value of that cell.它显示一个包含三列的表格,当您单击一个单元格时,它会打印该单元格的值。

public static void main(String[] args)
{
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    Table table = new Table(shell, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER | SWT.FULL_SELECTION);

    int cols = 3;
    for (int c = 0; c < cols; c++)
    {
        TableColumn column = new TableColumn(table, SWT.NONE);
        column.setText("Column " + c);
    }

    int rows = 10;
    for (int r = 0; r < rows; r++)
    {
        TableItem item = new TableItem(table, SWT.NONE);

        for (int c = 0; c < cols; c++)
        {
            item.setText(c, r + " " + c);
        }
    }

    for (int c = 0; c < cols; c++)
        table.getColumn(c).pack();

    table.addListener(SWT.MouseDown, e -> {
        Point pt = new Point(e.x, e.y);
        TableItem item = table.getItem(pt);

        if (item != null)
        {
            for (int c = 0; c < table.getColumnCount(); c++)
            {
                Rectangle rect = item.getBounds(c);
                if (rect.contains(pt))
                {
                    System.out.println(item.getText(c));
                }
            }
        }
    });

    shell.pack();
    shell.open();
    shell.setSize(400, 300);

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
        {
            display.sleep();
        }
    }
    display.dispose();
}

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

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