org.eclipse.swt.widgets.Table 列选中背景色

huhai123 2009-11-19 10:18:26
现在已经实现选中一列中的任意一项都可以改变整列的背景色,然而这是系统自己的颜色,我什么改成自己想要的颜色


import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

/**
*
*
*
*/
public class TableSelection {

public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final Table table = new Table(shell, SWT.MULTI);
table.setHeaderVisible(true);
table.setLinesVisible(true);
final TableColumn column1 = new TableColumn(table, SWT.NONE);
column1.setText("Column 1");
final TableColumn column2 = new TableColumn(table, SWT.NONE);
column2.setText("Column 2");
table.addListener(SWT.MouseDown, new Listener() {
public void handleEvent(Event event) {
Rectangle clientArea = table.getClientArea();
//table.getRegion();
Point pt = new Point(event.x, event.y);
int index = table.getTopIndex();
//int a=table.getItemCount();
//System.out.print(a);
while (index < table.getItemCount()) {
boolean visible = false;
TableItem item = table.getItem(index);
for (int i = 0; i < table.getColumnCount(); i++) {
Rectangle rect = item.getBounds(i);
if (rect.contains(pt)) {
table.setSortColumn(table.getColumn(i));
//table.GET
//table.setBackground( new Color(""));
table.setSortDirection(( SWT.DOWN));
String ss=table.getColumn(i).getText();
System.out.print(i+"----"+ss);
//item.setBackground(new Color(null, 0, 255, 0));
}
if (!visible && rect.intersects(clientArea)) {
visible = true;
}
}
if (!visible)
return;
index++;
}
}
});

Listener sortListener = new Listener() {
public void handleEvent(Event e) {
TableColumn column = (TableColumn) e.widget;
table.setSortColumn(column);
}
};
TableItem item = new TableItem(table, SWT.NONE);
item.setText(new String[] { "a", "3" });
item = new TableItem(table, SWT.NONE);
item.setText(new String[] { "b", "2" });
item = new TableItem(table, SWT.NONE);
item.setText(new String[] { "c", "1" });
column1.setWidth(100);
column2.setWidth(100);
column1.addListener(SWT.Selection, sortListener);
column2.addListener(SWT.Selection, sortListener);
table.setSortColumn(column1);
table.setSortDirection(SWT.UP);
shell.setSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, 300);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}

}



...全文
780 17 打赏 收藏 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
huhai123 2009-11-19
  • 打赏
  • 举报
回复
急待中
justinavril 2009-11-19
  • 打赏
  • 举报
回复
已经有了啊...
healer_kx 2009-11-19
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 justinavril 的回复:]
晕 我运行了你的程序 给我报错了~~
Java codeException in thread"main" java.lang.UnsatisfiedLinkError: no swt-gtk-3550 or swt-gtk in swt.library.path, java.library.path or the jar file
[/Quote]
要swt的dll或者so.
justinavril 2009-11-19
  • 打赏
  • 举报
回复
晕 我运行了你的程序 给我报错了~~
Exception in thread "main" java.lang.UnsatisfiedLinkError: no swt-gtk-3550 or swt-gtk in swt.library.path, java.library.path or the jar file
healer_kx 2009-11-19
  • 打赏
  • 举报
回复
这个状态不一定是swt提供你的,
你的程序结构,最好要形成一种典型的数据和视图分离的模型。
也就是,每一个cell的数据(包含它的背景色在内),要和显示分开。
然后refresh你的table,就会调用每一个cell的Provider了。。。这样你就有机会change那些cell的color了。
huhai123 2009-11-19
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 healer_kx 的回复:]
列选中的颜色,稍微复杂一点。因为最直接提供的是针对行的Provider。
其实也好解决,就是把每一行的那一个cell,改变它的状态,
swt会遍历一下每一个Provider,然后提供给正确的颜色,

我这么说你明白不?
[/Quote]

改变 table.getColumn(i) 状态?
healer_kx 2009-11-19
  • 打赏
  • 举报
回复
列选中的颜色,稍微复杂一点。因为最直接提供的是针对行的Provider。
其实也好解决,就是把每一行的那一个cell,改变它的状态,
swt会遍历一下每一个Provider,然后提供给正确的颜色,

我这么说你明白不?
阁楼上的伟哥 2009-11-19
  • 打赏
  • 举报
回复
帮顶
huhai123 2009-11-19
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 closewbq 的回复:]
象征性的改了下,你自己琢磨琢磨!
[/Quote]

还是 不行,是不是有什么办法让这一列单元格 边框变粗点呢
closewbq 2009-11-19
  • 打赏
  • 举报
回复
象征性的改了下,你自己琢磨琢磨!
closewbq 2009-11-19
  • 打赏
  • 举报
回复

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

public class TableEditoExamples {
public static void main(String[] args) {

Display display = new Display();
final Color blue = display.getSystemColor ( SWT.COLOR_BLUE ) ;
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final Table table = new Table(shell, SWT.MULTI);
table.setHeaderVisible(true);
table.setLinesVisible(true);
final TableColumn column1 = new TableColumn(table, SWT.NONE);
column1.setText("Column 1");
final TableColumn column2 = new TableColumn(table, SWT.NONE);
column2.setText("Column 2");
table.addListener(SWT.MouseDown, new Listener() {
public void handleEvent(Event event) {
Rectangle clientArea = table.getClientArea();
//table.getRegion();
Point pt = new Point(event.x, event.y);
int index = table.getTopIndex();
//int a=table.getItemCount();
//System.out.print(a);
while (index < table.getItemCount()) {
boolean visible = false;
TableItem item = table.getItem(index);
for (int i = 0; i < table.getColumnCount(); i++) {
Rectangle rect = item.getBounds(i);
if (rect.contains(pt)) {
table.setSortColumn(table.getColumn(i));
//table.GET
//table.setBackground( new Color(""));
table.setSortDirection(( SWT.DOWN));
String ss=table.getColumn(i).getText();
System.out.print(i+"----"+ss);
//item.setBackground(new Color(null, 0, 255, 0));
}
if (!visible && rect.intersects(clientArea)) {
visible = true;
}
}
if (!visible)
return;
index++;
}
}
});

TableItem item = new TableItem(table, SWT.NONE);
item.setText(new String[] { "a", "3" });
item = new TableItem(table, SWT.NONE);
item.setText(new String[] { "b", "2" });
item = new TableItem(table, SWT.NONE);
item.setText(new String[] { "c", "1" });
column1.setWidth(100);
column2.setWidth(100);
Listener sortListener = new Listener() {
public void handleEvent(Event e) {
TableColumn column = (TableColumn) e.widget;
table.setSortColumn(column);
table.getItem(0).setBackground(0,blue);
table.getItem(1).setBackground(0,blue);
table.getItem(2).setBackground(0,blue);
}
};
column1.addListener(SWT.Selection, sortListener);
column2.addListener(SWT.Selection, sortListener);
table.setSortColumn(column1);
table.setSortDirection(SWT.UP);
shell.setSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, 300);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}



}
justinavril 2009-11-19
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 huhai123 的回复:]
引用 11 楼 justinavril 的回复:
想调试都不行  哎

eclipse 插件开发 遇到
[/Quote]
你去看看八神给的链接吧 我这没办法了 环境限制
healer_kx 2009-11-19
  • 打赏
  • 举报
回复
? 我这几天做Notes插件呢,和Eclipse是一样的。
再发你一篇文章,看看你能用上不?
http://www.ibm.com/developerworks/lotus/library/notes8-sidebar/
huhai123 2009-11-19
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 justinavril 的回复:]
想调试都不行  哎
[/Quote]
eclipse 插件开发 遇到
justinavril 2009-11-19
  • 打赏
  • 举报
回复
想调试都不行 哎
dingqiaowu 2009-11-19
  • 打赏
  • 举报
回复
说真的程序有点乱
老张-AI 2009-11-19
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 justinavril 的回复:]
晕 我运行了你的程序 给我报错了~~
Java codeException in thread"main" java.lang.UnsatisfiedLinkError: no swt-gtk-3550 or swt-gtk in swt.library.path, java.library.path or the jar file
[/Quote]

....路过 顶下

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧