SWT CTabFolder doesn't display when running from eclipse - eclipse

I am creating an app using WindowBuilder in eclipse 4.4.2. I've added CTabFolders and corresponding CTabItems. The tabs are displayed properly when Click the preview button(Quickly test/preview the window without compiling or running it) in windowbuilder.
Screen Shot===> http://i.imgur.com/Sx9TQrL.jpg
But the real problem is when I run the app,the tabs are not displayed.
Screen Shot===> http://i.imgur.com/OP7WY8W.jpg
Code
public class Dashboard {
public static void main(String[] args) {
Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setImage(SWTResourceManager.getImage(Dashboard.class,
"/com/sun/java/swing/plaf/windows/icons/Computer.gif"));
shell.setBackground(SWTResourceManager
.getColor(SWT.COLOR_WIDGET_BORDER));
shell.setSize(1080, 700);
shell.setLayout(new GridLayout(2, false));
/**
* Base Layout
* **/
Composite content = new Composite(shell, SWT.BORDER);
content.setBackground(SWTResourceManager
.getColor(SWT.COLOR_WIDGET_BACKGROUND));
content.setLayout(new StackLayout());
// content.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false,
// false));
content.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final CTabFolder tabFolder = new CTabFolder(content, SWT.CLOSE);
tabFolder.setTabHeight(28);
tabFolder.setSimple(false);
tabFolder.setSelectionForeground(SWTResourceManager
.getColor(SWT.COLOR_BLACK));
tabFolder.setBackground(SWTResourceManager
.getColor(SWT.COLOR_WIDGET_BACKGROUND));
tabFolder.setSelectionBackground(Display.getCurrent().getSystemColor(
SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT));
CTabItem homeTab = new CTabItem(tabFolder, SWT.NONE);
homeTab.setText("Home");
Composite composite_4 = new Composite(tabFolder, SWT.NONE);
homeTab.setControl(composite_4);
Label lblHome = new Label(composite_4, SWT.NONE);
lblHome.setBounds(185, 105, 55, 15);
lblHome.setText("Home");
final CTabItem empTab = new CTabItem(tabFolder, SWT.CLOSE);
empTab.setImage(SWTResourceManager.getImage(
Dashboard.class, "/resource/icons/employee.png"));
empTab.setText("Employees");
Composite composite_5 = new Composite(tabFolder, SWT.NONE);
empTab.setControl(composite_5);
Label lblEmployees = new Label(composite_5, SWT.NONE);
lblEmployees.setBounds(155, 102, 55, 15);
lblEmployees.setText("Employees");
CTabItem workTab = new CTabItem(tabFolder, SWT.CLOSE);
workTab.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/worksite.png"));
workTab.setText("Work Site");
Composite composite_6 = new Composite(tabFolder, SWT.NONE);
workTab.setControl(composite_6);
Label lblWorkSite = new Label(composite_6, SWT.NONE);
lblWorkSite.setBounds(222, 123, 55, 15);
lblWorkSite.setText("Work site");
CTabItem paymentTab = new CTabItem(tabFolder, SWT.NONE);
paymentTab.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/payments.png"));
paymentTab.setText("Payments");
Composite composite_7 = new Composite(tabFolder, SWT.NONE);
paymentTab.setControl(composite_7);
Label lblPayments = new Label(composite_7, SWT.NONE);
lblPayments.setBounds(185, 176, 55, 15);
lblPayments.setText("Payments");
CTabItem toolsTab = new CTabItem(tabFolder, SWT.NONE);
toolsTab.setImage(SWTResourceManager.getImage(Dashboard.class, "/resource/icons/tools.png"));
toolsTab.setShowClose(true);
toolsTab.setText("Tools");
Composite composite_8 = new Composite(tabFolder, SWT.NONE);
toolsTab.setControl(composite_8);
Label lblTools = new Label(composite_8, SWT.NONE);
lblTools.setBounds(264, 125, 55, 15);
lblTools.setText("Tools");
/**
* MenuBar
* **/
Menu menu = new Menu(shell, SWT.BAR);
shell.setMenuBar(menu);
MenuItem mntmFile = new MenuItem(menu, SWT.CASCADE);
mntmFile.setImage(null);
mntmFile.setText("File");
Menu menu_1 = new Menu(mntmFile);
mntmFile.setMenu(menu_1);
MenuItem mntmExit = new MenuItem(menu_1, SWT.NONE);
mntmExit.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
shell.dispose();
}
});
mntmExit.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/close.png"));
mntmExit.setText("Exit");
MenuItem mntmHelp = new MenuItem(menu, SWT.CASCADE);
mntmHelp.setText("Help");
Menu menu_2 = new Menu(mntmHelp);
mntmHelp.setMenu(menu_2);
MenuItem mntmAbout = new MenuItem(menu_2, SWT.NONE);
mntmAbout.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
About abdialog = new About(shell, 0);
abdialog.open();
}
});
mntmAbout.setText("About");
Composite sidebar = new Composite(shell, SWT.NONE);
sidebar.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1,
1));
sidebar.setBackground(SWTResourceManager.getColor(112, 128, 144));
sidebar.setLayout(new GridLayout(1, false));
ExpandBar expandBar = new ExpandBar(sidebar, SWT.NONE);
expandBar.setBackground(SWTResourceManager.getColor(112, 128, 144));
expandBar.setTouchEnabled(true);
GridData gd_expandBar = new GridData(SWT.CENTER, SWT.CENTER, false,
false, 1, 1);
gd_expandBar.heightHint = 619;
expandBar.setLayoutData(gd_expandBar);
ExpandItem xpndtmEmployeeDetails = new ExpandItem(expandBar, SWT.NONE,
0);
xpndtmEmployeeDetails.setExpanded(true);
xpndtmEmployeeDetails.setImage(SWTResourceManager.getImage(
Dashboard.class, "/resource/icons/employee.png"));
xpndtmEmployeeDetails.setText("Employee Details");
Composite expandbarComposite = new Composite(expandBar, SWT.NONE);
xpndtmEmployeeDetails.setControl(expandbarComposite);
xpndtmEmployeeDetails.setHeight(100);
expandbarComposite.setLayout(new FormLayout());
expandbarComposite.setBackground(SWTResourceManager
.getColor(SWT.COLOR_WIDGET_BACKGROUND));
Button btnAddNew = new Button(expandbarComposite, SWT.NONE);
btnAddNew.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/ic_add.png"));
FormData fd_btnAddNew = new FormData();
fd_btnAddNew.right = new FormAttachment(100, -10);
fd_btnAddNew.left = new FormAttachment(0, 38);
btnAddNew.setLayoutData(fd_btnAddNew);
btnAddNew.setText("Add New");
Button btnEdit = new Button(expandbarComposite, SWT.FLAT | SWT.CENTER);
btnEdit.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
}
});
btnEdit.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/edit.png"));
fd_btnAddNew.bottom = new FormAttachment(btnEdit, -6);
FormData fd_btnEdit = new FormData();
fd_btnEdit.left = new FormAttachment(btnAddNew, 0, SWT.LEFT);
fd_btnEdit.right = new FormAttachment(btnAddNew, 0, SWT.RIGHT);
fd_btnEdit.bottom = new FormAttachment(100);
btnEdit.setLayoutData(fd_btnEdit);
btnEdit.setText("Edit");
/**
* View Employees Button
* **/
Button btnViewEmployees = new Button(expandbarComposite, SWT.NONE);
btnViewEmployees.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
tabFolder.setSelection(empTab);
}
});
FormData fd_btnViewEmployees = new FormData();
fd_btnViewEmployees.right = new FormAttachment(btnAddNew, 0, SWT.RIGHT);
fd_btnViewEmployees.top = new FormAttachment(0, 6);
fd_btnViewEmployees.left = new FormAttachment(btnAddNew, 0, SWT.LEFT);
btnViewEmployees.setLayoutData(fd_btnViewEmployees);
btnViewEmployees.setText("View Employees");
ExpandItem xpndtmWorkSiteDetails = new ExpandItem(expandBar, SWT.NONE);
xpndtmWorkSiteDetails.setImage(SWTResourceManager.getImage(
Dashboard.class, "/resource/icons/worksite.png"));
xpndtmWorkSiteDetails.setText("Work Site Details");
Composite composite = new Composite(expandBar, SWT.NONE);
xpndtmWorkSiteDetails.setControl(composite);
xpndtmWorkSiteDetails.setHeight(100);
composite.setLayout(new FormLayout());
Button btnAddNew_1 = new Button(composite, SWT.NONE);
btnAddNew_1.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/ic_add.png"));
FormData fd_btnAddNew_1 = new FormData();
fd_btnAddNew_1.right = new FormAttachment(100, -10);
fd_btnAddNew_1.top = new FormAttachment(0, 37);
btnAddNew_1.setLayoutData(fd_btnAddNew_1);
btnAddNew_1.setText("Add New");
Button btnEdit_1 = new Button(composite, SWT.NONE);
fd_btnAddNew_1.bottom = new FormAttachment(btnEdit_1, -6);
fd_btnAddNew_1.left = new FormAttachment(btnEdit_1, 0, SWT.LEFT);
btnEdit_1.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/edit.png"));
FormData fd_btnEdit_1 = new FormData();
fd_btnEdit_1.left = new FormAttachment(0, 40);
fd_btnEdit_1.right = new FormAttachment(100, -10);
fd_btnEdit_1.bottom = new FormAttachment(100);
btnEdit_1.setLayoutData(fd_btnEdit_1);
btnEdit_1.setText("Edit");
Button btnViewWorks = new Button(composite, SWT.NONE);
FormData fd_btnViewWorks = new FormData();
fd_btnViewWorks.top = new FormAttachment(0, 6);
fd_btnViewWorks.left = new FormAttachment(btnAddNew_1, 0, SWT.LEFT);
fd_btnViewWorks.right = new FormAttachment(100, -10);
btnViewWorks.setLayoutData(fd_btnViewWorks);
btnViewWorks.setText("View Works");
ExpandItem xpndtmPaymentDetails = new ExpandItem(expandBar, SWT.NONE);
xpndtmPaymentDetails.setImage(SWTResourceManager.getImage(
Dashboard.class, "/resource/icons/payments.png"));
xpndtmPaymentDetails.setText("Payment Details");
Composite composite_1 = new Composite(expandBar, SWT.NONE);
xpndtmPaymentDetails.setControl(composite_1);
xpndtmPaymentDetails.setHeight(100);
composite_1.setLayout(new FormLayout());
Button btnAddNew_2 = new Button(composite_1, SWT.NONE);
btnAddNew_2.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/ic_add.png"));
FormData fd_btnAddNew_2 = new FormData();
fd_btnAddNew_2.left = new FormAttachment(0, 44);
btnAddNew_2.setLayoutData(fd_btnAddNew_2);
btnAddNew_2.setText("Add New");
Button btnEditPayments = new Button(composite_1, SWT.NONE);
fd_btnAddNew_2.bottom = new FormAttachment(btnEditPayments, -6);
fd_btnAddNew_2.right = new FormAttachment(btnEditPayments, 0, SWT.RIGHT);
btnEditPayments.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/edit.png"));
FormData fd_btnEditPayments = new FormData();
fd_btnEditPayments.left = new FormAttachment(0, 44);
fd_btnEditPayments.right = new FormAttachment(100, -10);
fd_btnEditPayments.bottom = new FormAttachment(100);
btnEditPayments.setLayoutData(fd_btnEditPayments);
btnEditPayments.setText("Edit Payments");
Button btnViewPayments = new Button(composite_1, SWT.NONE);
FormData fd_btnViewPayments = new FormData();
fd_btnViewPayments.right = new FormAttachment(btnAddNew_2, 0, SWT.RIGHT);
fd_btnViewPayments.top = new FormAttachment(0, 6);
fd_btnViewPayments.left = new FormAttachment(btnAddNew_2, 0, SWT.LEFT);
btnViewPayments.setLayoutData(fd_btnViewPayments);
btnViewPayments.setText("View Payments");
ExpandItem xpndtmOurTools = new ExpandItem(expandBar, SWT.NONE);
xpndtmOurTools.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/tools.png"));
xpndtmOurTools.setText("Tools Available");
Composite composite_3 = new Composite(expandBar, SWT.NONE);
xpndtmOurTools.setControl(composite_3);
xpndtmOurTools.setHeight(80);
composite_3.setLayout(new FormLayout());
Button btnViewTools = new Button(composite_3, SWT.NONE);
FormData fd_btnViewTools = new FormData();
fd_btnViewTools.top = new FormAttachment(0, 10);
fd_btnViewTools.right = new FormAttachment(100, -10);
fd_btnViewTools.left = new FormAttachment(100, -131);
btnViewTools.setLayoutData(fd_btnViewTools);
btnViewTools.setText("View Tools");
Button btnAddNew_3 = new Button(composite_3, SWT.NONE);
btnAddNew_3.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
System.out.println("Add tools");
}
});
FormData fd_btnAddNew_3 = new FormData();
fd_btnAddNew_3.right = new FormAttachment(btnViewTools, 0, SWT.RIGHT);
fd_btnAddNew_3.top = new FormAttachment(btnViewTools, 6);
fd_btnAddNew_3.left = new FormAttachment(btnViewTools, 0, SWT.LEFT);
btnAddNew_3.setLayoutData(fd_btnAddNew_3);
btnAddNew_3.setText("Add New");
ExpandItem xpndtmReports = new ExpandItem(expandBar, SWT.NONE);
xpndtmReports.setExpanded(true);
xpndtmReports.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/reports.png"));
xpndtmReports.setText("Reports");
xpndtmReports.setHeight(80);
Composite composite_2 = new Composite(expandBar, SWT.NONE);
xpndtmReports.setControl(composite_2);
composite_2.setLayout(new FormLayout());
Button btnGenerateReport = new Button(composite_2, SWT.NONE);
btnGenerateReport.setImage(SWTResourceManager.getImage(Dashboard.class,
"/resource/icons/gen_report.png"));
FormData fd_btnGenerateReport = new FormData();
fd_btnGenerateReport.top = new FormAttachment(0, 10);
fd_btnGenerateReport.left = new FormAttachment(0, 10);
fd_btnGenerateReport.right = new FormAttachment(100, -28);
btnGenerateReport.setLayoutData(fd_btnGenerateReport);
btnGenerateReport.setText("Generate Report");
xpndtmReports.setHeight(100);
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}
Can anyone suggest a solution. Thanks in advance.

And Finally I've figured the problem in the code. The composite in the base layout was assigned with the StackLayout as follows.
Composite content = new Composite(shell, SWT.BORDER);
content.setLayout(new StackLayout());
content.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
When I changed the StackLayout to GridLayout, I got a working solution.
Composite content = new Composite(shell, SWT.BORDER);
content.setLayout(new GridLayout(1, false));
content.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

Related

JFace add a Text box near a RadioGroupFieldEditor

I try to add some preferences in the Eclipse menu and for that I create in JFace a RadioGroupFieldEditor section and near the last radio of the RadioGroupField, I want to have a text area, like in the picture below:
However I am not able to do this. I create a GridLayout with two columns, in one column I want the RadioGroupFieldEditor , in the other I want the text field. The text field is put below the RadioGroupField, not in the second column like I expect.
here is the code:
protected Control createContents(Composite parent) {
Composite fieldEditorParent = new Composite(parent, SWT.NULL);
GridLayout layout = new GridLayout();
layout.numColumns = 1;
layout.marginHeight = 0;
layout.marginWidth = 0;
fieldEditorParent.setLayout(layout);
fieldEditorParent.setFont(parent.getFont());
createAGroup(fieldEditorParent);
return fieldEditorParent;
}
private void createAGroup(Composite parent) {
Group group = new Group(parent, SWT.NONE);
group.setText("Group");
GridLayout layout = new GridLayout();
layout.numColumns = 1;
group.setLayout(layout);
GridData data = new GridData(GridData.FILL_HORIZONTAL);
data.grabExcessHorizontalSpace = true;
group.setLayoutData(data);
GridLayout subLayout = new GridLayout();
subLayout.numColumns = 3;
Composite expertUserComposite1 = new Composite(group, SWT.NONE);
expertUserComposite1.setLayout(subLayout);
new Button(expertUserComposite1, SWT.RADIO | SWT.LEFT);
new Label(expertUserComposite1, SWT.BEGINNING).setText("aaa");
new Text(expertUserComposite1, SWT.BORDER);
Composite expertUserComposite2 = new Composite(group, SWT.NONE);
expertUserComposite2.setLayout(subLayout);
new Button(expertUserComposite2, SWT.RADIO | SWT.LEFT);
new Label(expertUserComposite2, SWT.BEGINNING).setText("bbb");
Composite expertUserComposite3 = new Composite(group, SWT.NONE);
expertUserComposite3.setLayout(subLayout);
new RadioGroupFieldEditor(IExportCSVPreferences.DELIMITER_NAME_CURRENT,
"RadioGroup", 1,
new String[][] { { "line1", "line1" }, { "line2", "line2" }, { "line3", "line3" } },
expertUserComposite3);
new Text(expertUserComposite3, SWT.BORDER);
Composite expertUserComposite4 = new Composite(group, SWT.NONE);
expertUserComposite4.setLayout(subLayout);
new RadioGroupFieldEditor(IExportCSVPreferences.DELIMITER_NAME_CURRENT, "RadioGroup", 1,
new String[][] { { "line1", "line1" }, { "line2", "line2" }, { "line3", "line3" } }, expertUserComposite4,
true);
new Text(expertUserComposite4, SWT.BORDER);
}
Here is the result:

Plugin development: composite of button in a view plugin

I am using zest to create plugin that show graph. I am trying to create button that refresh the plugin. The button is working, and I see the graph in the view also. But, the button is taking a lot of space in the view, and limit the place for the graph.I want that the button want limit the graph place.. How can I fix it? Thanks]
public void createPartControl(Composite parent) {
//create refresh button
Composite container = new Composite(parent, SWT.NONE);
container.setLayout(new GridLayout(1, false));
Button btnMybutton = new Button(container, SWT.PUSH);
btnMybutton.setBounds(0, 10, 75, 25);
btnMybutton.setText("Refresh Graph");
btnMybutton.addSelectionListener(new SelectionListener() {
#Override
public void widgetSelected(SelectionEvent e) {
init();
}
#Override
public void widgetDefaultSelected(SelectionEvent e) {
// TODO Auto-generated method stub
}
});
// Graph will hold all other objects
graph = new Graph(parent, SWT.NONE);
}
If you want to show the button on top of the graph, you should use a FormLayout instead of a GridLayout:
public void createPartControl(Composite parent) {
//create refresh button
Composite container = new Composite(parent, SWT.NONE);
container.setLayout(new FormLayout()); // Use FormLayout instead of GridLayout
Button btnMybutton = new Button(container, SWT.PUSH);
// btnMybutton.setBounds(0, 10, 75, 25); This line is unnecessary
// Assign the right FormData to the button
FormData formData = new FormData();
formData.left = new FormAttachment(0, 5);
formData.top = new FormAttachment(0, 5);
btnMybutton.setLayoutData(formData);
btnMybutton.setText("Refresh Graph");
// Use SelectionAdapter instead of SelectionListener
// (it's not necessary but saves a few lines of code)
btnMybutton.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
init();
}
});
// Graph will hold all other objects
graph = new Graph(container, SWT.NONE); // Note parent changed to container
// Assignt the right FormData to the graph
FormData formData2 = new FormData();
formData2.left = new FormAttachment(0, 0);
formData2.top = new FormAttachment(0, 0);
formData2.right = new FormAttachment(100, 0);
formData2.bottom = new FormAttachment(100, 0);
graph.setLayoutData(formData2);
}

Trying to set the size/location of a button in a tab for SWT

This should be pretty simple but this is the first time I've worked with SWT. This is what I have so far.
public class TabsTest {
private Shell shell;
private CTabFolder folder;
public TabsTest(Display display){
shell = new Shell(display);
shell.setText("TabsTest");
shell.setLayout(new FillLayout());
CTabFolder folder = new CTabFolder(shell, SWT.CLOSE | SWT.BOTTOM);
folder.setUnselectedCloseVisible(false);
folder.setSimple(false);
initUI(folder);
shell.pack();
shell.setBounds(500, 500, 400, 500);
shell.open ();
while(!shell.isDisposed()){
if(!display.readAndDispatch())
display.sleep();
}
}
public void initUI(CTabFolder folder){
CTabItem NFL = new CTabItem(folder, SWT.NONE);
NFL.setText("NFL Bets");
Button okButton = new Button(folder, SWT.PUSH);
okButton.setText("OK");
okButton.setSize(10,10);
NFL.setControl(okButton);
CTabItem NBA = new CTabItem(folder,SWT.NONE);
NBA.setText("NBA Bets");
CTabItem CFB = new CTabItem(folder,SWT.NONE);
CFB.setText("CFB Bets");
folder.setSize(800,500);
}
public static void main (String [] args) {
Display display = new Display();
new TabsTest(display);
display.dispose();
}
}
What this currently gives me is this....
How would I make this a small button in the bottom right corner? Or just in general make it smaller and move it somewhere.
Since you are using a FillLayout the control takes up the entire space available. What you need is a different kind of a layout. I will suggest you to read this article, it will be a good start.
I generally prefer GridLayout as it is quite easy to use and it fulfills most needs.
Edited: Modifying your code to use GridLayout
public class TabsTest {
private Shell shell;
private CTabFolder folder;
public TabsTest(Display display) {
shell = new Shell(display);
shell.setText("TabsTest");
shell.setLayout(new GridLayout());
CTabFolder folder = new CTabFolder(shell, SWT.CLOSE | SWT.BOTTOM);
folder.setUnselectedCloseVisible(false);
folder.setSimple(false);
folder.setLayoutData(new GridData(GridData.FILL_BOTH));
initUI(folder);
shell.pack();
shell.setBounds(500, 500, 400, 500);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
public void initUI(CTabFolder folder) {
CTabItem NFL = new CTabItem(folder, SWT.NONE);
NFL.setText("NFL Bets");
Composite nflParent = new Composite(folder, SWT.NONE);
nflParent.setBackground(folder.getDisplay().getSystemColor(SWT.COLOR_BLUE));
nflParent.setLayout(new GridLayout());
Button okButton = new Button(nflParent, SWT.PUSH);
okButton.setText("OK");
GridData gd = new GridData();
gd.verticalAlignment = GridData.END;
gd.horizontalAlignment = GridData.END;
gd.grabExcessHorizontalSpace = true;
gd.grabExcessVerticalSpace = true;
okButton.setLayoutData(gd);
NFL.setControl(nflParent);
CTabItem NBA = new CTabItem(folder, SWT.NONE);
NBA.setText("NBA Bets");
CTabItem CFB = new CTabItem(folder, SWT.NONE);
CFB.setText("CFB Bets");
folder.setSize(800, 500);
}
public static void main(String[] args) {
Display display = new Display();
new TabsTest(display);
display.dispose();
}
}

SWT - Getting Selected Rows from Table

I am trying to write a method that will get the current selections from the table and create a ArrayList from the selections.
Method:
public void getPlotterSelection() {
selPrinters = new ArrayList<PrinterProfile>();
int[] row = table.getSelectionIndices();
Arrays.sort(row);
if (row.length > 0) {
for(int i = row.length-1; i >= 0; i--){
PrinterProfile pp = new PrinterProfile(aa.get(i).getPrinterName(), aa.get(i).getProfileName());
selPrinters.add(pp);
}
}
}
This is the error I am getting
ERROR: 16:16:49,503 - TcLogger$IC_LogListener.logging:?
org.eclipse.core.runtime - org.eclipse.ui - 0 - Unhandled event loop exception
org.eclipse.swt.SWTException: Widget is disposed
at org.eclipse.swt.SWT.error(SWT.java:3884)
at org.eclipse.swt.SWT.error(SWT.java:3799)
at org.eclipse.swt.SWT.error(SWT.java:3770)
at org.eclipse.swt.widgets.Widget.error(Widget.java:463)
at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:336)
at org.eclipse.swt.widgets.Table.getSelectionIndices(Table.java:2536)
etc .......
The problem is with this line of code
int[] row = table.getSelectionIndices();
Once again..
I am trying to get the user selected rows in the table and put them in a arraylist.
Edit Adding more code
//////////////////////////////////////////////////////////////////////////
// createDialogArea() //
//////////////////////////////////////////////////////////////////////////
protected Control createDialogArea(Composite parent) {
final Composite area = new Composite(parent, SWT.NONE);
final GridLayout gridLayout = new GridLayout();
gridLayout.marginWidth = 15;
gridLayout.marginHeight = 10;
area.setLayout(gridLayout);
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
area.setLayoutData(gridData);
checkingArray();
createCopyNumber(area);
createPlotterTable(area);
return area;
}
public void checkingArray() {
aa = abd.getPrintersArray();
}
//////////////////////////////////////////////////////////////////////////
// createPlotterTable() //
//////////////////////////////////////////////////////////////////////////
private void createPlotterTable(Composite parent) {
Composite composite = new Composite(parent, SWT.BORDER);
GridLayout gridLayout = new GridLayout(1, false);
composite.setLayout(gridLayout);
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
composite.setLayoutData(gridData);
//gridData = new GridData(SWT.FILL, SWT.FILL, true, true );
table = new Table(composite, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
table.setHeaderVisible(true);
table.setLinesVisible(true);
TableColumn[] column = new TableColumn[2];
column[0] = new TableColumn(table, SWT.FILL);
column[0].setText("Printer Name");
column[0].setWidth(200);
column[1] = new TableColumn(table, SWT.FILL);
column[1].setText("Profile Name");
column[1].setWidth(200);
gridData = new GridData();
gridData.verticalAlignment = GridData.FILL;
gridData.horizontalAlignment = GridData.FILL;
gridData.grabExcessHorizontalSpace = true;
gridData.grabExcessVerticalSpace = true;
table.setLayoutData(gridData);
fillTable(table);
table.setRedraw(true);
}
private void fillTable(Table table) {
table.setRedraw(false);
for(Iterator iterator = abd.getPrintersArray().iterator();iterator.hasNext();){
PrinterProfile printer = (PrinterProfile) iterator.next();
TableItem item = new TableItem(table, SWT.FILL);
int c = 0;
item.setText(c++, printer.getPrinterName());
item.setText(c++, printer.getProfileName());
}
table.setRedraw(true);
}
public void getPlotterSelection() {
selPrinters = new ArrayList<PrinterProfile>();
int[] row = table.getSelectionIndices();
Arrays.sort(row);
if (row.length > 0) {
for(int i = row.length-1; i >= 0; i--){
PrinterProfile pp = new PrinterProfile(aa.get(i).getPrinterName(), aa.get(i).getProfileName());
selPrinters.add(pp);
}
}
}
This is the button that calls the method
Button okButton = createButton(parent, IDialogConstants.OK_ID, "OK", true);
okButton.setEnabled(true);
okButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
getPlotterSelection();
}
});
I would suggest you to user Table viewer rather than Table. It makes your life easier. I see that you are showing table in a dialog. I guess your dialog is getting closed/disposed when you click OK before it get to getPlotterSelection() method.

TableViewer Centered Layout

So now I have successfully added a TableViewer in my TitleAreaDialog.
I am trying to figure out some of layout issues I am having.
Can I control the layout and location of my tableViewer in my Dialog window.
Right now the table is showing up on the right side.
I want it to be centered in my parent Composite.
Can I add the TableViewer to a Parent Layout in the createDialogArea method?
I will be adding more composites to the Dialog and would like to be able to control where they go and how they look.
Also my table shows a half empty column at the end of the table, is there a way to remove that?
Something like:
GridData gridData = new GridData();
gridData.grabExcessHorizontalSpace = true;
gridData.horizontalAlignment = GridData.CENTER;
TableViewer d = createTableViewer(area);
d.setLayoutData(gridData);
This is my createDialogArea code.
protected Control createDialogArea(Composite parent) {
final Composite area = new Composite(parent, SWT.NULL);
final GridLayout gridLayout = new GridLayout();
gridLayout.marginWidth = 15;
gridLayout.marginHeight = 10;
area.setLayout(gridLayout);
TableViewer d = createTableViewer(area);
return area;
}
Here is my tableviewer code
private TableViewer createTableViewer(Composite parent) {
viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
createColumns(parent, viewer);
final Table table = viewer.getTable();
table.setHeaderVisible(true);
table.setLinesVisible(true);
viewer.setContentProvider(new ArrayContentProvider());
viewer.setInput(AplotSelectedDataTable.getInstance().getArrayData());
// Layout the viewer
GridData gridData = new GridData(SWT.CENTER);
gridData.verticalAlignment = GridData.FILL;
gridData.horizontalSpan = 2;
gridData.grabExcessHorizontalSpace = true;
gridData.grabExcessVerticalSpace = true;
gridData.horizontalAlignment = GridData.FILL;
viewer.getControl().setLayoutData(gridData);
return viewer;
}
Here is a small example that should help you with your layout issues:
public class TestClass extends Dialog {
private TableViewer viewer;
protected TestClass(Shell parentShell) {
super(parentShell);
}
protected Control createDialogArea(Composite parent) {
final Composite area = new Composite(parent, SWT.NONE);
final GridLayout gridLayout = new GridLayout(2, true);
gridLayout.marginWidth = 15;
gridLayout.marginHeight = 10;
area.setLayout(gridLayout);
area.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
createButtons(area);
createTableViewer(area);
return area;
}
private void createButtons(Composite parent)
{
Button button1 = new Button(parent, SWT.PUSH);
button1.setText("Button1");
button1.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, true));
Button button2 = new Button(parent, SWT.PUSH);
button2.setText("Button2");
button2.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, true));
}
private void createTableViewer(Composite parent) {
viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
| SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
createColumns(parent);
final Table table = viewer.getTable();
table.setHeaderVisible(true);
table.setLinesVisible(true);
viewer.setContentProvider(new ArrayContentProvider());
// Layout the viewer
GridData gridData = new GridData(SWT.CENTER, SWT.FILL, true, true);
gridData.horizontalSpan = 2;
table.setLayoutData(gridData);
}
private void createColumns(Composite parent)
{
TableViewerColumn viewerColumn = new TableViewerColumn(viewer, SWT.NONE);
final TableColumn column = viewerColumn.getColumn();
column.setText("Title");
column.setWidth(100);
column.setResizable(true);
column.setMoveable(false);
}
public static void main(String[] args) {
Display display = Display.getDefault();
final Shell shell = new Shell(display);
TestClass test = new TestClass(shell);
test.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
It basically creates a small Dialog with 2 buttons at the top and a centered TableViewer below. This should give you an idea on how to solve your problem.