3

テーブルのデータから配列リストを作成しようとしています。表示されている列から値を取得する必要がありますが、テーブルに表示されていない列からも値を取得する必要があります。Table Viewer で SWT を使用していますが、テーブルに列を表示しない方法がわかりません。また、列名を指定してテーブルからデータを取得する方法もわかりません。

私はずっとSwingを使ってきたので、テーブルモデルクラスをずっと使ってきました。Swing では、列を作成し、非表示にし、そこからデータを取得するのは非常に簡単です。

これは、以前の Swing プロジェクトで行った方法です。

私のテーブルモデルクラスでは:

public String getColumnName(int column) {
  String s = null;

  switch (column) {
     case ITEMID_COL: {
        s = "ItemId";
        break;
     }

そうしてgetValueAt()

 public Object getValueAt(int row, int column) {
  Object o = null;

  try {
     switch (column) {
        case ITEMID_COL: {
           o = rds.get(row).rev.getItem().getStringProperty("item_id");
           break;
        }

そのため、他のクラスのテーブルからデータが必要になったとき、私がしなければならなかったのは

Object item_id = SingletonSelectTable.getInstance().getValueAt(i, SingletonSelectTable.getInstance().ITEMID_COL);

を設定することで、列を簡単に非表示にすることもできますMAX_COLUMNS

質問:

  1. テーブルビューアを使用して、表示されないが値を含む列をテーブルに追加する方法を学ぶ必要があります。

  2. テーブルから値にアクセスする方法を学ぶ必要があるため、列から可視データと非可視データの配列を作成できます。

  3. これは、Table Viewer を使用しても可能ですか?

4

2 に答える 2

9

大丈夫:

を非表示にするTableColumnには、基本的にその幅を に設定し、0サイズ変更を防止します。幅を何かに設定して非表示にし、>= 0サイズ変更を有効にします。

ModelProvider でを使用しているTableViewerため、コンテンツにアクセスするときに列が非表示になっていてもかまいません。モデルからオブジェクトを取得し、そこから情報を取得するだけです。

以下は、列を非表示/非表示にして、現在選択されている人物を印刷できる例です。

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

    final TableViewer viewer = new TableViewer(shell, SWT.READ_ONLY);

    // First column is for the name
    TableViewerColumn col = createTableViewerColumn("Name", 100, 0, viewer);
    col.setLabelProvider(new ColumnLabelProvider() {
        @Override
        public String getText(Object element) {
            if(element instanceof Person)
            {
                System.out.println("1");
                return ((Person)element).getName();
            }
            return "";
        }
    });

    // First column is for the location
    TableViewerColumn col2 = createTableViewerColumn("Location", 100, 1, viewer);
    col2.setLabelProvider(new ColumnLabelProvider() {
        @Override
        public String getText(Object element) {
            if(element instanceof Person)
            {
                System.out.println("2");
                return ((Person)element).getLocation();
            }
            return "";
        }
    });

    final Table table = viewer.getTable();
    table.setHeaderVisible(true);
    table.setLinesVisible(true);
    GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
    data.horizontalSpan = 2;
    table.setLayoutData(data);

    /* This button will hide/unhide the columns */
    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("Hide / Unhide");

    button1.addListener(SWT.Selection, new Listener() {

        @Override
        public void handleEvent(Event arg0) {
            for(final TableColumn column : table.getColumns())
            {
                if(column.getWidth() == 0)
                {
                    column.setWidth(100);
                    column.setResizable(true);
                }
                else
                {
                    column.setWidth(0);
                    column.setResizable(false);
                }
            }
        }
    });

    /* This button will print the currently selected Person, even if columns are hidden */
    Button button2 = new Button(shell, SWT.PUSH);
    button2.setText("Print");

    button2.addListener(SWT.Selection, new Listener() {

        @Override
        public void handleEvent(Event arg0) {
            IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
            Person person = (Person) selection.getFirstElement();

            System.out.println(person);
        }
    });

    viewer.setContentProvider(ArrayContentProvider.getInstance());

    final Person[] persons = new Person[] { new Person("Baz", "Loc"),
            new Person("BazBaz", "LocLoc"), new Person("BazBazBaz", "LocLocLoc") };

    viewer.setInput(persons);

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

private static TableViewerColumn createTableViewerColumn(String title, int bound, final int colNumber, TableViewer viewer) {
    final TableViewerColumn viewerColumn = new TableViewerColumn(viewer, SWT.NONE);
    final TableColumn column = viewerColumn.getColumn();
    column.setText(title);
    column.setWidth(bound);
    column.setResizable(true);
    column.setMoveable(false);

    return viewerColumn;
}

public static class Person {
    private String name;
    private String location;

    public Person(String name, String location) {
        this.name = name;
        this.location = location;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String toString()
    {
        return name + " " + location;
    }
}
于 2012-09-20T17:38:28.847 に答える