In my code, org.eclipse.swt.widgets.ExpandBar contains multiple ExpandItems. The ExpandBar is setup to scroll. How do I programmetically scroll the ExpandBar? I looked for examples and API but no luck.
Alright, wrap your ExpandBar in a ScrolledComposite and let it handle scrolling.
The advantage of this is that ScrolledComposite has a method called .setOrigin(int, int) which you can use to scroll to a position.
Here is some example code:
public static void main(String[] args)
{
final Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.setText("ExpandBar Example");
final ScrolledComposite scrolledComp = new ScrolledComposite(shell, SWT.V_SCROLL);
final ExpandBar bar = new ExpandBar(scrolledComp, SWT.NONE);
for (int i = 0; i < 3; i++)
{
Composite composite = new Composite(bar, SWT.NONE);
composite.setLayout(new GridLayout());
for (int j = 0; j < 10; j++)
new Label(composite, SWT.NONE).setText("Label " + i + " " + j);
ExpandItem item = new ExpandItem(bar, SWT.NONE, 0);
item.setText("Item " + i);
item.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
item.setControl(composite);
}
bar.getItem(1).setExpanded(true);
bar.setSpacing(8);
/* Make sure to update the scrolled composite when we collapse/expand
* items */
Listener updateScrolledSize = new Listener()
{
#Override
public void handleEvent(Event arg0)
{
display.asyncExec(new Runnable()
{
#Override
public void run()
{
scrolledComp.setMinSize(bar.computeSize(SWT.DEFAULT, SWT.DEFAULT));
}
});
}
};
bar.addListener(SWT.Expand, updateScrolledSize);
bar.addListener(SWT.Collapse, updateScrolledSize);
scrolledComp.setContent(bar);
scrolledComp.setExpandHorizontal(true);
scrolledComp.setExpandVertical(true);
scrolledComp.setMinSize(bar.computeSize(SWT.DEFAULT, SWT.DEFAULT));
shell.setSize(400, 200);
shell.open();
/* Jump to the end */
scrolledComp.setOrigin(0, scrolledComp.getSize().y);
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
Looks like this after running:
As you can see it's scrolled to the end.
Update
Ok, if you want to jump to the position of a specific item, do the following:
Add a Button to test the functionality. Inside the Listener, get the y position and scroll to it:
Button jumpTo = new Button(shell, SWT.PUSH);
jumpTo.setText("Jump to item");
jumpTo.addListener(SWT.Selection, new Listener()
{
private int counter = 0;
#Override
public void handleEvent(Event e)
{
int y = getYPosition(bar, counter);
/* Increment the counter */
counter = (counter + 1) % bar.getItemCount();
/* Scroll into view */
scrolledComp.setOrigin(0, y);
}
});
Use this method to get the y position:
private static int getYPosition(ExpandBar bar, int position)
{
/* Calculate the position */
int y = 0;
for(int i = 0; i < position; i++)
{
/* Incorporate the spacing */
y += bar.getSpacing();
/* Get the item (On LINUX, use this line) */
ExpandItem item = bar.getItem(bar.getItemCount() - 1 - i);
/* Get the item (On WINDOWS, use this line) */
//ExpandItem item = bar.getItem(i);
/* Add the header height */
y += item.getHeaderHeight();
/* If the item is expanded, add it's height as well */
if(item.getExpanded())
y += item.getHeight();
}
return y;
}
Related
I'm generating a dialog with a lot of content. I want to pack the dialog to the childrens height, and I want to center it on screen, so I'm doing this after adding all the childrens:
shell.open();
shell.layout();
shell.pack(); //for wrap the dialog size to it's content width and height.
//the dialog must be centered after doing shell.pack();
Rectangle parentSize = getParent().getBounds();
Rectangle shellSize = shell.getBounds();
int x = parentSize.x + (parentSize.width - shellSize.width) / 2;
int y = (int) (parentSize.y + (parentSize.height - shellSize.height) / 3.5);
shell.setLocation(new Point(x, y));
The problem is that for some milliseconds the dialog is being visible with a lot of width and on the top left corner of the screen. I tried doing setVisible(false) and (true) to shell to do the trick but it doesn't works.
How can I avoid to see the dialog on the top left corner and with a lot of width for some milliseconds?
This is the full code:
public class SelectTwoMatrixNumbersDialog extends Dialog {
protected ArrayList<Integer> matrixNumbers1;
protected ArrayList<Integer> matrixNumbers2;
protected Shell shell;
private JointBet jointBet;
private boolean result;
NumbersMatrixWidget numbersMatrixWidget1;
NumbersMatrixWidget numbersMatrixWidget2;
/**
* Create the dialog.
* #param parent
* #param style
*/
public SelectTwoMatrixNumbersDialog(Shell parent, int style, JointBet jointBet) {
super(parent, style);
setText("Elegir Números");
this.jointBet = jointBet;
}
/**
* Open the dialog.
* #return the result
*/
public boolean open() {
createContents();
matrixNumbers1 = new ArrayList<Integer>();
matrixNumbers2 = new ArrayList<Integer>();
GridLayout gl_shell = new GridLayout(1, false);
gl_shell.marginWidth = 20;
gl_shell.marginHeight = 20;
shell.setLayout(gl_shell);
Composite contentComposite = new Composite(shell, SWT.NONE);
contentComposite.setLayout(new GridLayout(1, false));
contentComposite.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1));
Label lblNumbers = new Label(contentComposite, SWT.NONE);
lblNumbers.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
lblNumbers.setText("Números:");
Composite numbersContainerComposite = new Composite(contentComposite, SWT.NONE);
numbersContainerComposite.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
//variable values depending of type of game
boolean firstCellEmpty = jointBet.getGameType().getNumbersMatrixArray()[0].isFirstCellEmpty();
int matrix1Rows = jointBet.getGameType().getNumbersMatrixArray()[0].getRows();
int matrix1Columns = jointBet.getGameType().getNumbersMatrixArray()[0].getColumns();
int matrix1MinNumber = jointBet.getGameType().getNumbersMatrixArray()[0].getMinNumber();
int matrix1MaxNumber = jointBet.getGameType().getNumbersMatrixArray()[0].getMaxNumber();
GridLayout gl_numbersContainerComposite = new GridLayout(2, false);
gl_numbersContainerComposite.horizontalSpacing = 15;
numbersContainerComposite.setLayout(gl_numbersContainerComposite);
numbersMatrixWidget1 = new NumbersMatrixWidget(numbersContainerComposite, SWT.BORDER, false, jointBet.getGameType().getNumbersMatrixArray()[0].getNumbers(), matrix1Rows, matrix1Columns, matrix1MinNumber, matrix1MaxNumber, firstCellEmpty);
//variable values depending of type of game
firstCellEmpty = jointBet.getGameType().getNumbersMatrixArray()[1].isFirstCellEmpty();
int matrix2Rows = jointBet.getGameType().getNumbersMatrixArray()[1].getRows();
int matrix2Columns = jointBet.getGameType().getNumbersMatrixArray()[1].getColumns();
int matrix2MinNumber = jointBet.getGameType().getNumbersMatrixArray()[1].getMinNumber();
int matrix2MaxNumber = jointBet.getGameType().getNumbersMatrixArray()[1].getMaxNumber();
numbersMatrixWidget2 = new NumbersMatrixWidget(numbersContainerComposite, SWT.BORDER, false, jointBet.getGameType().getNumbersMatrixArray()[1].getNumbers(), matrix2Rows, matrix2Columns, matrix2MinNumber, matrix2MaxNumber, firstCellEmpty);
numbersMatrixWidget2.setLayoutData(new GridData(SWT.LEFT, SWT.BOTTOM, false, false, 1, 1));
Composite fillNumbersComposite = new Composite(shell, SWT.NONE);
fillNumbersComposite.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
fillNumbersComposite.setLayout(new GridLayout(1, false));
Composite compositeFillMatrix1 = new Composite(fillNumbersComposite, SWT.NONE);
compositeFillMatrix1.setLayout(new GridLayout(4, false));
Label lblFillMatrix1WithMostCommonNumbers1 = new Label(compositeFillMatrix1, SWT.NONE);
lblFillMatrix1WithMostCommonNumbers1.setText("Rellenar Matriz 1 con los");
Spinner matrix1NumbersSpinner = new Spinner(compositeFillMatrix1, SWT.BORDER);
matrix1NumbersSpinner.setMaximum(jointBet.getGameType().getNumbersMatrixArray()[0].getPossibleNumbersCount());
matrix1NumbersSpinner.setMinimum(1);
Label lblFillMatrix1WithMostCommonNumbers2 = new Label(compositeFillMatrix1, SWT.NONE);
lblFillMatrix1WithMostCommonNumbers2.setText("números mas repetidos:");
Button btnFillMatrix1 = new Button(compositeFillMatrix1, SWT.NONE);
btnFillMatrix1.setText("Rellenar");
btnFillMatrix1.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
String gameType = jointBet.getGameType().getType();
int matrix = 1;
int amount = Integer.parseInt(matrix1NumbersSpinner.getText());
numbersMatrixWidget1.setNumbers(StatisticsManager.getInstance().getMostUsedNumberKeys(gameType, matrix, amount));
}
});
Composite compositeFillMatrix2 = new Composite(fillNumbersComposite, SWT.NONE);
compositeFillMatrix2.setLayout(new GridLayout(4, false));
Label lblFillMatrix2WithMostCommonNumbers1 = new Label(compositeFillMatrix2, SWT.NONE);
lblFillMatrix2WithMostCommonNumbers1.setText("Rellenar Matriz 2 con los");
Spinner matrix2NumbersSpinner = new Spinner(compositeFillMatrix2, SWT.BORDER);
matrix2NumbersSpinner.setMaximum(jointBet.getGameType().getNumbersMatrixArray()[1].getPossibleNumbersCount());
matrix2NumbersSpinner.setMinimum(1);
Label lblFillMatrix2WithMostCommonNumbers2 = new Label(compositeFillMatrix2, SWT.NONE);
lblFillMatrix2WithMostCommonNumbers2.setText("números mas repetidos:");
Button btnFillMatrix2 = new Button(compositeFillMatrix2, SWT.NONE);
btnFillMatrix2.setText("Rellenar");
btnFillMatrix2.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
String gameType = jointBet.getGameType().getType();
int matrix = 2;
int amount = Integer.parseInt(matrix2NumbersSpinner.getText());
numbersMatrixWidget2.setNumbers(StatisticsManager.getInstance().getMostUsedNumberKeys(gameType, matrix, amount));
}
});
Composite buttonsComposite = new Composite(shell, SWT.NONE);
FillLayout fl_buttonsComposite = new FillLayout(SWT.HORIZONTAL);
fl_buttonsComposite.marginHeight = 10;
fl_buttonsComposite.spacing = 40;
buttonsComposite.setLayout(fl_buttonsComposite);
GridData gd_buttonsComposite = new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1);
gd_buttonsComposite.heightHint = 45;
buttonsComposite.setLayoutData(gd_buttonsComposite);
Button acceptButton = new Button(buttonsComposite, SWT.NONE);
acceptButton.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
//matrix 1
matrixNumbers1.clear();
for (Button b : numbersMatrixWidget1.getButtons()) {
if (b.getSelection()) {
matrixNumbers1.add(Integer.parseInt(b.getText()));
}
}
//matrix 2
matrixNumbers2.clear();
for (Button b : numbersMatrixWidget2.getButtons()) {
if (b.getSelection()) {
matrixNumbers2.add(Integer.parseInt(b.getText()));
}
}
if (matrixNumbers1.size()<jointBet.getGameType().getNumbersMatrixArray()[0].getNumbersPerBet() || matrixNumbers2.size()<jointBet.getGameType().getNumbersMatrixArray()[1].getNumbersPerBet()) {
ErrorDialog dialog = new ErrorDialog(shell, SWT.DIALOG_TRIM | SWT.PRIMARY_MODAL, "El mínimo de números es "+jointBet.getGameType().getNumbersMatrixArray()[0].getNumbersPerBet()+" + "+jointBet.getGameType().getNumbersMatrixArray()[1].getNumbersPerBet());
dialog.open();
}else {
Collections.sort(matrixNumbers1);
Collections.sort(matrixNumbers2);
jointBet.setNumbers(0, matrixNumbers1);
jointBet.setNumbers(1, matrixNumbers2);
DataManager.getInstance().saveData();
result = true;
shell.close();
}
}
});
acceptButton.setBounds(0, 0, 75, 25);
acceptButton.setText("Aceptar");
Button cancelButton = new Button(buttonsComposite, SWT.NONE);
cancelButton.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
result = false;
shell.close();
}
});
cancelButton.setBounds(0, 0, 75, 25);
cancelButton.setText("Cancelar");
shell.setDefaultButton(acceptButton);
shell.open();
shell.layout();
shell.pack(); //for wrap the dialog size to it's content width and height.
//the dialog must be centered after doing shell.pack();
Rectangle parentSize = getParent().getBounds();
Rectangle shellSize = shell.getBounds();
int x = parentSize.x + (parentSize.width - shellSize.width) / 2;
int y = (int) (parentSize.y + (parentSize.height - shellSize.height) / 3.5);
shell.setLocation(new Point(x, y));
Display display = getParent().getDisplay();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
return result;
}
/**
* Create contents of the dialog.
*/
private void createContents() {
shell = new Shell(getParent(), getStyle());
shell.setText(getText());
}
}
Just move the shell.open() to the end of the size calculations - after the shell.setLocation.
i am a beginner in E4 and at all in JAVA.
i created a partHandler and i want to display table which is created in other class in this part. table is cteated in TreeTableCreation class. it would be nice if you can help.
now it creates a Tab, but no table inside, and throws Nullpointer exception. tahnk you.
public class DynamicPartHandlerCode {
#Execute
public void execute(EModelService modelService, MApplication application, final IEclipseContext context,
#Named(IServiceConstants.ACTIVE_SHELL) final Shell shell) {
EPartService partService = context.get(EPartService.class);
// create new part
MPart mPart = modelService.createModelElement(MPart.class);
String id = "org.testeditor.ui.uidashboard.partdescriptor.0";
mPart = partService.findPart(id);
if (mPart == null) {
mPart = partService.createPart(id);
}
List<MPartStack> stacks = modelService.findElements(application, null, MPartStack.class, null);
stacks.get(2).getChildren().add(mPart);
((TreeTableCreation) mPart.getObject()).createTable();
partService.showPart(mPart, PartState.ACTIVATE);
}
}
here class to create table
public class TreeTableCreation {
// Injected services
#Inject
#Named(IServiceConstants.ACTIVE_SHELL)
Shell shell;
#Inject
MPart mPart;
public void createTable() {
// public static void main(String[] args) {
// Shell shell;
Display display = new Display();
// final Shell shell = new Shell(display);
// shell = new Shell(Display.getCurrent());
// shell.setSize(500, 500);
shell.setLayout(new FillLayout());
final Tree tree = new Tree(shell, SWT.BORDER | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL);
tree.setHeaderVisible(true);
tree.setLinesVisible(true);
final TreeViewer v = new TreeViewer(tree);
// Header der
// Tabelle*********************************************************************
String[] titles = { "Datum ", "Testname", "Erfolgreich", "Durchgefallen", "Dauer", "Läufe" };
for (int i = 0; i < titles.length; i++) {
TreeColumn column = new TreeColumn(tree, SWT.CENTER);
column.setText(titles[i]);
column.setWidth(150);
}
v.setLabelProvider(new MyLabelProvider());
v.setContentProvider(new MyContentProvider());
v.setInput(TestResultTest.getData());
// // selecting cells getting
// // items******************************************************
tree.addListener(SWT.MouseDoubleClick, new Listener() {
final int columnCount = 6;
public void handleEvent(Event event) {
Point pt = new Point(event.x, event.y);
TreeItem item = tree.getItem(pt);
int index = tree.indexOf(item);
System.out.println("Item Index-" + index);
if (item == null)
return;
for (int i = 0; i < columnCount; i++) {
Rectangle rect = item.getBounds(i);
if (rect.contains(pt)) {
TreeTableCreation2 anothershell = new TreeTableCreation2();
DrawMultipleLine grafshell = new DrawMultipleLine();
anothershell.open();
System.out.println("Läufe gewählt");
grafshell.open();
System.out.println("Dauer gewählt");
}
}
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
You can't just convert a standalone SWT program like this. You need to read a tutorial on writing e4 programs such as this one
You must not create a new Display, e4 already has one.
You must not mess around with the current Shell, e4 is in charge of that and has many other objects already in the Shell.
Do not call shell.pack, shell.open or use a display dispatch loop.
Do not dispose of the display.
Do not do ((TreeTableCreation) mPart.getObject()).createTable();. Use the #PostConstruct method of the part.
So something like:
public class TreeTableCreation {
#PostConstruct
public void createTable(Composite parent) {
final Tree tree = new Tree(parent, SWT.BORDER | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL);
tree.setHeaderVisible(true);
tree.setLinesVisible(true);
final TreeViewer v = new TreeViewer(tree);
// Header der
// Tabelle*********************************************************************
String[] titles = { "Datum ", "Testname", "Erfolgreich", "Durchgefallen", "Dauer", "Läufe" };
for (int i = 0; i < titles.length; i++) {
TreeColumn column = new TreeColumn(tree, SWT.CENTER);
column.setText(titles[i]);
column.setWidth(150);
}
v.setLabelProvider(new MyLabelProvider());
v.setContentProvider(new MyContentProvider());
v.setInput(TestResultTest.getData());
// // selecting cells getting
// // items******************************************************
tree.addListener(SWT.MouseDoubleClick, new Listener() {
final int columnCount = 6;
public void handleEvent(Event event) {
Point pt = new Point(event.x, event.y);
TreeItem item = tree.getItem(pt);
int index = tree.indexOf(item);
System.out.println("Item Index-" + index);
if (item == null)
return;
for (int i = 0; i < columnCount; i++) {
Rectangle rect = item.getBounds(i);
if (rect.contains(pt)) {
TreeTableCreation2 anothershell = new TreeTableCreation2();
DrawMultipleLine grafshell = new DrawMultipleLine();
anothershell.open();
System.out.println("Läufe gewählt");
grafshell.open();
System.out.println("Dauer gewählt");
}
}
}
});
}
I have a Text in SWT:
final Text textArea = new Text(parent, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
textArea.setVisible(false);
textArea.setEditable(false);
textArea.setEnabled(false);
textArea.setText("Scheduler Info");
I have a listener. Once the listener is fired, I would like some data to overwrite again and again in the text area. Is there anyway I can retain the "Scheduler Info" Header in the text area. I do not want the first line to be overwritten. I want the rest of the area to be overwritten.
There are two ways you can do this:
Just use Text#setText(String) with your new String and prepend the original string.
Select everything after the original string and Text#insert(String) your new stuff.
Here is an example with both methods:
private static final String INITIAL_TEXT = "Scheduler Info";
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new FillLayout());
final Text text = new Text(shell, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
text.setEditable(false);
text.setEnabled(false);
text.setText(INITIAL_TEXT);
Button replace = new Button(shell, SWT.PUSH);
replace.setText("Replace");
replace.addListener(SWT.Selection, new Listener()
{
private int counter = 1;
#Override
public void handleEvent(Event arg0)
{
String replace = INITIAL_TEXT;
for(int i = 0; i < counter; i++)
replace += "\nLine " + i;
text.setText(replace);
counter++;
}
});
Button insert = new Button(shell, SWT.PUSH);
insert.setText("Insert");
insert.addListener(SWT.Selection, new Listener()
{
private int counter = 1;
#Override
public void handleEvent(Event arg0)
{
text.setSelection(INITIAL_TEXT.length(), text.getText().length());
String newText = "";
for(int i = 0; i < counter; i++)
newText += "\nLine " + i;
text.insert(newText);
counter++;
}
});
shell.pack();
shell.setSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT).x, 300);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
I have two SWT Tables in a Shell.
When the user clicks on a row in one table, I want the corresponding row in the next Table to be selected too.
However, in the current setup (I'm demonstrating with a Snippet I found online) when one row is clicked, the corresponding row is highlighted too but it's in grey. I'd like the selection to be the same purple everywhere.
The Source Code:
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
public class Snippet234 {
public static void main (String [] args) {
int rowCount = 40;
int columnCount = 15;
final Display display = new Display ();
Shell shell = new Shell (display);
shell.setLayout(new FillLayout());
Composite parent = new Composite(shell, SWT.BORDER);
GridLayout layout = new GridLayout(2, false);
layout.marginWidth = layout.marginHeight = layout.horizontalSpacing = 0;
parent.setLayout(layout);
final Table leftTable = new Table(parent, SWT.MULTI | SWT.FULL_SELECTION);
leftTable.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true));
leftTable.setHeaderVisible(true);
final Table rightTable = new Table(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
rightTable.setHeaderVisible(true);
GridData table2Data = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 2);
rightTable.setLayoutData(table2Data);
// Create columns
TableColumn column1 = new TableColumn(leftTable, SWT.NONE);
column1.setText("Name");
column1.setWidth(150);
for (int i = 0; i < columnCount; i++) {
TableColumn column = new TableColumn(rightTable, SWT.NONE);
column.setText("Value "+i);
column.setWidth(200);
}
// Create rows
for (int i = 0; i < rowCount; i++) {
TableItem item = new TableItem(leftTable, SWT.NONE);
item.setText("item "+i);
item = new TableItem(rightTable, SWT.NONE);
for (int j = 0; j < columnCount; j++) {
item.setText(j, "Item "+i+" value # "+j);
}
}
// Make selection the same in both tables
leftTable.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
rightTable.setSelection(leftTable.getSelectionIndices());
}
});
rightTable.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
leftTable.setSelection(rightTable.getSelectionIndices());
}
});
// On Windows, the selection is gray if the table does not have focus.
// To make both tables appear in focus, draw the selection background here.
// This part only works on version 3.2 or later.
/*
Listener eraseListener = new Listener() {
public void handleEvent(Event event) {
event.detail &= ~SWT.HOT;
if((event.detail & SWT.SELECTED) != 0) {
GC gc = event.gc;
Rectangle rect = event.getBounds();
gc.setForeground(display.getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
gc.setBackground(display.getSystemColor(SWT.COLOR_LIST_SELECTION));
gc.fillRectangle(rect);
event.detail &= ~SWT.SELECTED;
}
}
};
leftTable.addListener(SWT.EraseItem, eraseListener);
rightTable.addListener(SWT.EraseItem, eraseListener);
*/
// Make vertical scrollbars scroll together
ScrollBar vBarLeft = leftTable.getVerticalBar();
vBarLeft.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
rightTable.setTopIndex(leftTable.getTopIndex());
}
});
ScrollBar vBarRight = rightTable.getVerticalBar();
vBarRight.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
leftTable.setTopIndex(rightTable.getTopIndex());
}
});
// Horizontal bar on second table takes up a little extra space.
// To keep vertical scroll bars in sink, force table1 to end above
// horizontal scrollbar
ScrollBar hBarRight = rightTable.getHorizontalBar();
Label spacer = new Label(parent, SWT.NONE);
GridData spacerData = new GridData();
spacerData.heightHint = hBarRight.getSize().y;
spacer.setVisible(false);
parent.setBackground(leftTable.getBackground());
shell.setSize(600, 400);
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
Ok, I threw something together for you. Don't mind the crappy way of handling the selection, that's not what I want to show. The important part is where I draw the background of the item, i.e. the MyLabelProviders paint() method:
public static void main(String[] args)
{
Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new FillLayout());
final TableViewer first = new TableViewer(shell);
addColumn(first);
first.setContentProvider(ArrayContentProvider.getInstance());
final List<MyObject> firstInput = getInput();
first.setInput(firstInput);
first.setLabelProvider(new MyLabelProvider());
final TableViewer second = new TableViewer(shell);
addColumn(second);
second.setContentProvider(ArrayContentProvider.getInstance());
final List<MyObject> secondInput = getInput();
second.setInput(secondInput);
second.setLabelProvider(new MyLabelProvider());
first.addSelectionChangedListener(new ISelectionChangedListener()
{
#Override
public void selectionChanged(SelectionChangedEvent e)
{
int index = first.getTable().getSelectionIndex();
second.getTable().setSelection(index);
setSelection(firstInput, index);
setSelection(secondInput, index);
}
});
second.addSelectionChangedListener(new ISelectionChangedListener()
{
#Override
public void selectionChanged(SelectionChangedEvent e)
{
int index = second.getTable().getSelectionIndex();
first.getTable().setSelection(index);
setSelection(firstInput, index);
setSelection(secondInput, index);
}
});
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private static void setSelection(List<MyObject> input, int index)
{
for(MyObject obj : input)
{
obj.setSelected(false);
}
input.get(index).setSelected(true);
}
private static List<MyObject> getInput()
{
List<MyObject> result = new ArrayList<MyObject>();
result.add(new MyObject("First"));
result.add(new MyObject("Second"));
result.add(new MyObject("Third"));
return result;
}
private static void addColumn(TableViewer viewer)
{
TableViewerColumn col = new TableViewerColumn(viewer, SWT.NONE);
col.getColumn().setWidth(200);
col.getColumn().setText("Name:");
col.setLabelProvider(new ColumnLabelProvider()
{
#Override
public String getText(Object element)
{
MyObject value = (MyObject) element;
return value.getName();
}
});
}
private static class MyObject
{
private String name;
private boolean isSelected;
public MyObject(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public boolean isSelected()
{
return isSelected;
}
public void setSelected(boolean isSelected)
{
this.isSelected = isSelected;
}
#Override
public String toString()
{
return name;
}
}
private static class MyLabelProvider extends StyledCellLabelProvider
{
#Override
protected void paint(Event event, Object element)
{
int width = 1000;
int x = event.x - 2;
int y = event.y;
int height = event.height;
GC gc = event.gc;
MyObject object = (MyObject) element;
Color oldBackground = gc.getBackground();
Color color = object.isSelected ? Display.getCurrent().getSystemColor(SWT.COLOR_LIST_SELECTION) : Display.getCurrent().getSystemColor(SWT.COLOR_LIST_BACKGROUND);
gc.setBackground(color);
gc.fillRectangle(x, y, width, height);
gc.setBackground(oldBackground);
super.paint(event, element);
}
}
Here are two screenshots after selecting the first and then the third row (doesn't matter which TableViewer):
I'm adding column one by one to my swt table. with
int totalwidth=0;
for(String s:phoneErrs){
TableColumn tblclmnError = new TableColumn(tablephone, SWT.CENTER);
tblclmnError.setText(s);
tblclmnError.pack();
totalwidth+=tblclmnError.getWidth();
}
and after this I want to add a last column, that should fill the rest of the
space in table header. Now that I have the total width of the added columns
already, I should be able to calculate the width of my last column and specify
it right? but how? I tried
TableColumn tblclmnComment = new TableColumn(tablephone, SWT.CENTER);
tblclmnComment.setWidth(tablephone.getSize().x-totalwidth);
tblclmnComment.setText("Comment");
but it's not working. the getSize() return 0.
You can achieve this by adding listener on SWT.Resize event type.
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, true));
TableViewer viewer1 = getViewer(shell);
List<String> rows = new ArrayList<String>();
rows.add("Row 1");
rows.add("Row 2");
viewer1.setInput(rows);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
private static TableViewer getViewer(final Shell shell) {
TableViewer viewer = new TableViewer(shell, SWT.FULL_SELECTION
| SWT.H_SCROLL | SWT.V_SCROLL | SWT.NONE);
viewer.getTable().addListener(SWT.Resize, new Listener() {
#Override
public void handleEvent(Event event) {
Table table = (Table)event.widget;
int columnCount = table.getColumnCount();
if(columnCount == 0)
return;
Rectangle area = table.getClientArea();
int totalAreaWdith = area.width;
int lineWidth = table.getGridLineWidth();
int totalGridLineWidth = (columnCount-1)*lineWidth;
int totalColumnWidth = 0;
for(TableColumn column: table.getColumns())
{
totalColumnWidth = totalColumnWidth+column.getWidth();
}
int diff = totalAreaWdith-(totalColumnWidth+totalGridLineWidth);
TableColumn lastCol = table.getColumns()[columnCount-1];
//check diff is valid or not. setting negetive width doesnt make sense.
lastCol.setWidth(diff+lastCol.getWidth());
}
});
viewer.setContentProvider(ArrayContentProvider.getInstance());
viewer.getTable().setLayoutData(
new GridData(SWT.FILL, SWT.FILL, true, true));
TableViewerColumn col = new TableViewerColumn(viewer, SWT.NONE);
col.getColumn().setWidth(100);
col.getColumn().setText("Text Column");
col.setLabelProvider(new ColumnLabelProvider() {
#Override
public void update(ViewerCell cell) {
cell.setText((String) cell.getElement());
}
});
col = new TableViewerColumn(viewer, SWT.NONE);
col.getColumn().setWidth(100);
col.getColumn().setText("Second Text Column");
col.setLabelProvider(new ColumnLabelProvider() {
#Override
public void update(ViewerCell cell) {
cell.setText((String) cell.getElement());
}
});
viewer.getTable().setHeaderVisible(true);
return viewer;
}