Java SWT-桌子内部的透明单选按钮
我一辈子都搞不懂如何让SWT单选按钮单元使用当前的底层机制来确定表中的“交替行”颜色和“选定行”颜色 该表(默认情况下?)已经具有交替/选定的行颜色,并且所有其他列/行都正确显示它们。注意-我一直在仔细研究代码,但不知道备用/选定行颜色是如何设置的。有人会假设它是ColumnLabelProvider,但事实并非如此。我在下面的链接中看到了一个如何手动设置交替颜色的示例,但我想避免这样做,因为我不会使用任何已经在引擎盖下使用的架构Java SWT-桌子内部的透明单选按钮,java,swt,Java,Swt,我一辈子都搞不懂如何让SWT单选按钮单元使用当前的底层机制来确定表中的“交替行”颜色和“选定行”颜色 该表(默认情况下?)已经具有交替/选定的行颜色,并且所有其他列/行都正确显示它们。注意-我一直在仔细研究代码,但不知道备用/选定行颜色是如何设置的。有人会假设它是ColumnLabelProvider,但事实并非如此。我在下面的链接中看到了一个如何手动设置交替颜色的示例,但我想避免这样做,因为我不会使用任何已经在引擎盖下使用的架构 下面提供了一个代码示例。任何帮助都将不胜感激 privat
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);
}
});
private Map buttons=new HashMap();
...
私有void createColumns(最终TableViewer查看器){
最终TableViewerColumn column0=新的TableViewerColumn(查看器,SWT.CENTER);
column0.getColumn().setWidth(50);
column0.getColumn().setText(“MyColumnTitle”);
column0.setLabelProvider(新ColumnLabelProvider(){
@凌驾
公共无效更新(最终视口单元格){
cell.setImage(空);
cell.setText(null);
按钮;
if(buttons.containsKey(cell.getElement())){
button=buttons.get(cell.getElement());
}否则{
final TableItem=(TableItem)cell.getItem();
button=新按钮((复合)cell.getViewErrorw().getControl(),SWT.RADIO);
buttons.put(cell.getElement(),button);
//使单选按钮可点击
final TableEditor editor=新的TableEditor(item.getParent());
editor.horizontalAlignment=SWT.LEFT;
editor.grabHorizontal=true;
editor.minimumWidth=50;
setEditor(按钮、项、单元格.getColumnIndex());
编辑器。布局();
}
//有些事情我尝试过但没有成功
//按钮。设置背景(空);
//setBackground(Display.getDefault().getSystemColor(SWT.COLOR\u LIST\u 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());
}
});
//这是一个工作正常的列(按预期交替显示行/高亮显示)
最终TableViewerColumn列=新的TableViewerColumn(查看器,SWT.LEFT);
column.getColumn().setWidth(240);
column.getColumn().setText(“ColumnName”);
column.setLabelProvider(新ColumnLabelProvider(){
@凌驾
公共字符串getText(最终对象元素){
if(MyObject的元素实例){
返回MyObject.getText();
}
返回super.getText(元素);
}
});
如果平台提供了交替行颜色,您可以^将表格的背景模式设置为SWT.INHERIT\u FORCE
table.setBackgroundMode( SWT.INHERIT_FORCE );
如果行颜色是通过标签提供程序设置的,则需要将按钮的背景颜色设置为与放置按钮的行匹配
这是一个简单的SWT代码片段,我认为您正在寻找它。每一个偶数行都有一个绿色背景,单选按钮在激活之前被指定为所选项目的背景
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();
}
publicstaticvoidmain(字符串[]args){
显示=新显示();
外壳=新外壳(显示);
setLayout(新的FillLayout());
表=新表(壳牌,SWT.NONE);
TableColumn column=新的TableColumn(表,0);
列。设置宽度(100);
对于(int i=0;i<10;i++){
TableItem项目=新的TableItem(表,SWT.NONE);
项目.setText(“项目”+i);
如果(i%2==0){
项目背景(display.getSystemColor(SWT.COLOR_GREEN));
}
}
最终按钮=新按钮(表,SWT.RADIO);
按钮设置文本(“收音机”);
table.addSelectionListener(新的SelectionAdapter(){
公共无效widgetSelected(SelectionEvent事件){
TableItem项=(TableItem)event.item;
TableEditor=newTableEditor(item.getParent());
setText(item.getText());
setBackground(item.getBackground());
editor.horizontalAlignment=SWT.LEFT;
editor.grabHorizontal=true;
编辑器.setEditor(按钮,项,0);
编辑器。布局();
}
} );
shell.pack();
shell.open();
而(!shell.isDisposed()){
如果(!display.readAndDispatch()){
display.sleep();
}
}
display.dispose();
}
我首先会抛弃JFace,从纯SWT开始。这将帮助您更好地理解事物是如何结合在一起的。不幸的是,这是不可行的,因为这是使用JFace的大型项目的一部分,但您可以先试用纯SWT,如果您在那里找到了解决方案,请将其应用于JFace。您正在运行的操作系统是什么?您能发布负责交替行颜色的代码吗?或者澄清它们来自何处(您运行的是哪个窗口系统)?我运行的是linux操作系统。我不知道负责交替行颜色的代码在何处(根据我原来的帖子,我猜它应该是ColumnLabelProvider的一部分,但我没有在里面看到它。)ColumnLabelProvider
本身不会改变括号的颜色。你尝试过其他操作系统吗?我发布的代码是什么样子的?