You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java SWT中如何为每个TableItem右侧添加浏览按钮

在SWT Table的每个项右侧添加浏览按钮

要实现每个TableItem右侧带有浏览按钮的效果,你可以使用SWT的TableEditor控件,它允许在TableItem的单元格中嵌入其他控件(比如Button)。以下是完整的实现代码:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
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.widgets.TableEditor;

public class TableWithBrowseButtons {
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("Table with Browse Buttons");
        shell.setLayout(new FillLayout());
        
        // 创建Table,设置样式并添加列
        Table table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
        table.setHeaderVisible(true);
        
        // 第一列:项名称
        TableColumn itemColumn = new TableColumn(table, SWT.NONE);
        itemColumn.setText("项目");
        itemColumn.setWidth(150);
        
        // 第二列:浏览按钮列
        TableColumn buttonColumn = new TableColumn(table, SWT.NONE);
        buttonColumn.setText("操作");
        buttonColumn.setWidth(100);
        
        String[] modDisp = {"ITEM1", "ITEM2", "ITEM3"};
        
        for (String itemName : modDisp) {
            TableItem item = new TableItem(table, SWT.NONE);
            item.setText(itemName);
            
            // 创建浏览按钮
            Button browseBtn = new Button(table, SWT.PUSH);
            browseBtn.setText("浏览");
            
            // 使用TableEditor将按钮嵌入到TableItem的第二列
            TableEditor editor = new TableEditor(table);
            editor.grabHorizontal = true;
            editor.grabVertical = true;
            editor.setEditor(browseBtn, item, 1);
            
            // 为按钮添加选择事件,打开文件选择对话框
            browseBtn.addSelectionListener(new SelectionAdapter() {
                @Override
                public void widgetSelected(SelectionEvent e) {
                    FileDialog fileDialog = new FileDialog(shell, SWT.OPEN);
                    String selectedFilePath = fileDialog.open();
                    if (selectedFilePath != null) {
                        // 这里可以处理选中的文件路径,比如更新TableItem的文本或存储到变量中
                        System.out.println(itemName + " 选中的文件:" + selectedFilePath);
                        // 示例:将选中的路径显示在第一列后面
                        item.setText(itemName + " - " + selectedFilePath);
                    }
                }
            });
        }
        
        // 调整列宽以适应内容
        itemColumn.pack();
        buttonColumn.pack();
        
        shell.setSize(300, 300);
        shell.open();
        
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
}

关键说明:

  • 使用TableEditor将Button控件嵌入到TableItem的指定单元格中,通过setEditor方法关联按钮、TableItem和列索引。
  • 设置editor.grabHorizontal和editor.grabVertical为true,确保按钮能填满单元格空间。
  • 每个按钮绑定SelectionListener,点击时打开FileDialog选择文件,后续可根据需求处理选中的文件路径。

内容的提问来源于stack exchange,提问作者vmaya003

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 14:42:15