【问题标题】:SWT - Transparent Radio Button inside TableSWT - 表格内的透明单选按钮
【发布时间】:2014-11-29 15:07:14
【问题描述】:

我这辈子都不知道如何让 SWT 单选按钮单元格使用当前的底层机制来确定表格中的“交替行”颜色和“选定行”颜色。

表格(默认情况下?)已经有交替/选定的行颜色工作并且所有其他列/行正确显示它们。注意 - 我一直在研究代码,但无法弄清楚备用/选定行颜色是如何设置的。有人会认为它是 ColumnLabelProvider,但事实并非如此。我在下面的链接中看到了如何手动设置交替颜色的示例,但我想避免这样做,因为我不会使用已经在后台使用的任何架构。

下面提供了一个代码示例。任何帮助将不胜感激!

private Map<Object, Button> buttons = new HashMap<>();
...

private void createColumns(final TableViewer viewer) {
    final TableViewerColumn column0 = new TableViewerColumn(viewer, SWT.CENTER);
    column0.getColumn().setWidth(50);
    column0.getColumn().setText("MyColumnTitle");
    column0.setLabelProvider(new ColumnLabelProvider() {

        @Override
        public void update(final ViewerCell cell) {
            cell.setImage(null);
            cell.setText(null);

            Button button;
            if (buttons.containsKey(cell.getElement())) {
                button = buttons.get(cell.getElement());
            } else {
                final TableItem item = (TableItem) cell.getItem();
                button = new Button((Composite) cell.getViewerRow().getControl(), SWT.RADIO);
                buttons.put(cell.getElement(), button);

                // Make the radio button clickable
                final TableEditor editor = new TableEditor(item.getParent());
                editor.horizontalAlignment = SWT.LEFT;
                editor.grabHorizontal = true;
                editor.minimumWidth = 50;                    
                editor.setEditor(button, item, cell.getColumnIndex());
                editor.layout();
            }

// Some things I have tried without success                
// button.setBackground(null);
// button.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
// cell.getViewerRow().getControl().getParent().setBackgroundMode(SWT.INHERIT_NONE);
// cell.setBackground(cell.getNeighbor(ViewerCell.RIGHT, true).getBackground());
// cell.setForeground(cell.getNeighbor(ViewerCell.RIGHT, true).getForeground());
        }
    });

    // This is a column that works fine (shows alternating rows/highlighting as expected)
    final TableViewerColumn column = new TableViewerColumn(viewer, SWT.LEFT);
    column.getColumn().setWidth(240);
    column.getColumn().setText("ColumnName");
    column.setLabelProvider(new ColumnLabelProvider() {
        @Override
        public String getText(final Object element) {
            if (element instanceof MyObject) {
                return MyObject.getText();
            }
            return super.getText(element);
        }
    });

【问题讨论】:

  • 我首先会转储 JFace 并从纯 SWT 开始。这将帮助您更好地了解事物是如何联系在一起的。
  • 不幸的是,这是不可行的,因为这是使用 JFace 的更大项目的一部分
  • 但是您可以先尝试纯 SWT,如果您在那里找到了解决方案,请将其应用于 JFace。您正在运行什么操作系统?

标签: java swt


【解决方案1】:

如果平台提供了交替行颜色,可以^设置表格的背景模式为SWT.INHERIT_FORCE

table.setBackgroundMode( SWT.INHERIT_FORCE );

如果通过标签提供程序设置行颜色,则需要设置按钮的背景颜色以匹配按钮所在的行。

这是一个简单的 SWT sn-p,我认为您正在寻找它。每个偶数行都有一个绿色背景,并且单选按钮在被激活之前被分配了所选项目的背景。

  public static void main( String[] args ) {
    Display display = new Display();
    Shell shell = new Shell( display );
    shell.setLayout( new FillLayout() );
    Table table = new Table( shell, SWT.NONE );
    TableColumn column = new TableColumn( table, 0 );
    column.setWidth( 100 );
    for( int i = 0; i < 10; i++ ) {
      TableItem item = new TableItem( table, SWT.NONE );
      item.setText( "item " + i );
      if( i % 2 == 0 ) {
        item.setBackground( display.getSystemColor( SWT.COLOR_GREEN ) );
      }
    }
    final Button button = new Button( table, SWT.RADIO );
    button.setText( "radio" );
    table.addSelectionListener( new SelectionAdapter() {
      public void widgetSelected( SelectionEvent event ) {
        TableItem item = ( TableItem )event.item;
        TableEditor editor = new TableEditor( item.getParent() );
        button.setText( item.getText() );
        button.setBackground( item.getBackground() );
        editor.horizontalAlignment = SWT.LEFT;
        editor.grabHorizontal = true;
        editor.setEditor( button, item, 0 );
        editor.layout();
      }
    } );
    shell.pack();
    shell.open();
    while( !shell.isDisposed() ) {
      if( !display.readAndDispatch() ) {
        display.sleep();
      }
    }
    display.dispose();
  }

【讨论】:

  • 你能贴出负责交替行颜色的代码吗?或者澄清它们来自哪里(您在哪个窗口系统上运行)?
  • 我在 Linux 操作系统中运行。我不知道负责交替行颜色的代码在哪里(根据我的原始帖子,我猜它应该是 ColumnLabelProvider 的一部分,但我在那里看不到它)。
  • ColumnLabelProvider 不会自行更改背景颜色。您是否尝试过其他操作系统?我发布的代码是什么样的?
猜你喜欢
  • 1970-01-01
  • 2016-01-01
  • 2019-03-18
  • 1970-01-01
  • 1970-01-01
  • 2011-05-24
  • 2012-05-14
  • 1970-01-01
  • 2011-09-15
相关资源
最近更新 更多