简体   繁体   中英

How do I automatically update Java SWT table cells that already have information?

I am trying to update this table automatically with a syncexec. The code I am pasting is a test code that produces the same symptoms I am experiencing, but instead of live data from an external source, I am using the random number generating utility.

I have 2 columns. One column is a place holding number 1-9. The second column represents live data and will frequently change. I want that second column to update.

I am currently just adding rows which do indicate live data, but I would like to have only 9 rows total, not 9 added rows ever few milliseconds. I know it's doing this because I am populating the table over and over in my syncexec. How do I just update the table that I established in the UI, with my syncexec?

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import java.util.Random;

public class TableAutoUpdate
{
int u = 10;     
int i;  
int r;
int d = 10;
int c = 1;
int v = u+1;

boolean a = false;

private Label label; //Must import org.eclipse.swt.widgets.Label; then     declare it in the class to use it.
private Label status;
private Display display;
private Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER );

public TableAutoUpdate(Display display)
{
    try
    {
    initUI(display);
    invalidateUiData();
    }

    catch (NoClassDefFoundError e)
    {
        status.setText(e.toString());
    }
}


private void initUI(Display display)
{
    shell.setLayout(new GridLayout(3, false));
    String RegisterLabels[] = new String[u];
    status = new Label(shell, SWT.BORDER); 
    GridData gd2 = new GridData(SWT.FILL, SWT.FILL, true, true);   
    status.setLayoutData(gd2);
    gd2.widthHint = 40;
    gd2.heightHint = 15;
    gd2.horizontalSpan = 3;

    Table table = new Table(shell, SWT.BORDER);
    table.setHeaderVisible(true);
    table.setLinesVisible(true);

    TableColumn tc1 = new TableColumn(table, SWT.CENTER);
    TableColumn tc2 = new TableColumn(table, SWT.CENTER);


    tc1.setText("Register Number");
    tc2.setText("Register Value");


    tc1.setWidth(110);
    tc2.setWidth(110);

    int b = 1;

    Random randomGen = new Random();
    int randomInt = randomGen.nextInt(32000);

    for(i=1; i<u; i++)
    {
    status.setText("Valid numbers");
    TableItem item1 = new TableItem(table, SWT.NONE);
    RegisterLabels[i] = Integer.toString(b);
    item1.setText(new String[] {RegisterLabels[i],     item1.getText(randomInt)});
    b++;
    }


    shell.setSize(300, 300);
    status.getText();    
    shell.setText("Modbus Tags");
    centerWindow(shell);
    shell.open();   


    new Thread() 
    {
        @Override
        public void run() 
        {
            for (final int l = 1; l <= l; ) 
            {
            try 
            {
                Thread.sleep (100);
            } 
            catch (Throwable th) 
            {

            }
                if (display.isDisposed()) return;
                display.syncExec(new Runnable() 
                {
                    @Override
                    public void run()
                    {  

                            try
                            {
                                int b = 1;
                                for (r = 1; r < u; r++)
                                {
                                    Random randomGen = new Random();
                                    int randomInt =     randomGen.nextInt(32000);
                                    status.setText("Random Number Table     Update");
                                    TableItem item1 = new TableItem(table,     SWT.NONE);
                                    RegisterLabels[r] = Integer.toString(b);
                                    item1.setText(new String[]     {RegisterLabels[r], Integer.toString(randomInt)});
                                    b++;
                                }
                            }
                            catch (Exception e)
                            {
                                status.setText("Invalid Number");
                                try 
                                {
                                    Thread.sleep (1000);
                                } 
                                catch (Throwable th) 
                                {

                                }
                                invalidateUiData();
                            }
                        }
                    });
            }
        }
    }.start();


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


void invalidateUiData()
{
   status.setText("Unable to numbers.");
}

private void centerWindow(Shell shell) {

    Rectangle bds = shell.getDisplay().getBounds();

    Point p = shell.getSize();

    int nLeft = (bds.width - p.x) / 2;
    int nTop = (bds.height - p.y) / 2;

    shell.setBounds(nLeft, nTop, p.x, p.y);
}


@SuppressWarnings("unused")
public static void main(String[] args) {

    Display display = new Display();
    TableAutoUpdate ex = new TableAutoUpdate(display);
    display.dispose();
}
}

Create the 9 TableItems for the rows at the beginning.

Then use:

TableItem item = table.get(rowNumber);

item.setText(text);

to get the existing items and set its text. Note that the rowNumber starts at 0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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