how to pop up calendar when button is clicked in Java SWT? - swt

I am trying to develop Java SWT application in eclipse.
I need to populate text box using DateTime Calendar in SWT when a button is clicked.
I tried the following code but not able to see the Calendar, though it is created.
Any help would be appreciated.
Thanks
public void createPartControl(final Composite parent) {
Button button;
Label label;
final Display dev = parent.getDisplay();
Image image = new Image(dev,"C:\\Users\\rm186021\\Desktop\\Calendar.gif");
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 3;
parent.setLayout(gridLayout);
label = new Label(parent, SWT.NULL);
label.setText("Start date ");
final Text start = new Text(parent, SWT.SINGLE | SWT.BORDER);
Button calButton = new Button(parent, SWT.PUSH);
calButton.setImage(image);
calButton.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
final Display display = new Display();
final Shell shell2 = new Shell(display);
shell2.addListener(SWT.CALENDAR, new Listener() {
public void handleEvent(Event event) {
final DateTime calendar = new DateTime(shell2,SWT.CALENDAR | SWT.POP_UP);
calendar.addSelectionListener (new SelectionAdapter () {
public void widgetSelected (SelectionEvent e) {
start.setData(" " + calendar.getYear() + "-" + (calendar.getMonth() + 1) + "-" + calendar.getDay());
System.out.println(start.getData());
//calendar.dispose();
}
});
}
});
}
});

You're creating a Shell, but never even opening it. Try calling shell2.open().
You're adding an SWT.CALENDAR listener to the Shell. This isn't going to do what you want to do. Or anything, for that matter, since Shell doesn't fire SWT.CALENDAR events. Instead, you simply need to add the DateTime to a container and hook up selection listeners to the Calendar.
SWT.POP_UP is not an appropriate style bit for Calendar.
I would recommend subclassing Dialog (call it CalendarDialog, for example), setting a FillLayout on it, adding a Calendar to it and hooking up listeners that way. Then call CalendarDialog.open().

The DateTime really shouldn't be created with code like that :) Try this instead:
calButton.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
final Shell shell2 = new Shell(dev.getActiveShell());
// new Display() won't work on many platforms if one already exists
final DateTime calendar = new DateTime(shell2, SWT.CALENDAR);
// no need to add a listener to shell2, and POP_UP doesn't work for DateTime
calendar.addSelectionListener(...);
shell2.open();
// Edward Thomson noticed it wasn't called, I missed it
}
};

Related

How to create a toolbar inside viewpart (Not use plugin.xml)

I Created a ToolItem "Save As" like image above, But it not display at toolbar position. So how to create a toolbar inside viewpart (Not use plugin.xml)
IMAGE EXAMPLE
This is my code Create Toolbar:
public void createToolbar(Composite parent) {
// Create composite Toolbar and set layout
toolBarComposite = new Composite(parent, SWT.NONE);
gridLayout = new GridLayout(1, false);
toolBarComposite.setLayout(gridLayout);
gridData = new GridData(SWT.RIGHT, SWT.NONE, true, false);
toolBarComposite.setLayoutData(gridData);
// Create Toolbar
gridData = new GridData(SWT.RIGHT, SWT.NONE, true, false);
toolBar = new ToolBar(toolBarComposite, SWT.FLAT);
toolBar.setLayoutData(gridData);
// Create Item
item = new ToolItem(toolBar, SWT.PUSH);
item.setImage(SAVE_IMAGE);
item.setToolTipText("Save (Ctrl + S)");
item.setEnabled(true);
item.addSelectionListener(new SelectionAdapter() {
private static final long serialVersionUID = -102212312093090431L;
#Override
public void widgetSelected(SelectionEvent e) {
}
#Override
public void widgetDefaultSelected(SelectionEvent e) {
}
});
}
Thank for your advance !
You're going to have to use contributions on the view site's action bar.
Example
// Copy-pasted from an existing project, so the code can be made nicer
private void createAdditionalToolbarActions()
{
getViewSite().getActionBars().getToolBarManager().add(new GroupMarker("additions")); //$NON-NLS-1$
getViewSite().getActionBars().getToolBarManager().prependToGroup("additions", new SaveAction()); //$NON-NLS-1$
getViewSite().getActionBars().updateActionBars();
}
The method getViewSite is part of ViewPart. Call this after the contents of the view have been created.
The SaveAction must implement IAction or IContributionItem. For convenience, just extend the SaveAction from org.eclipse.jface.action.Action and call methods such as setImageDescriptor and setToolTipText.
Do all your business login in the run override.

GWT message box with closable icon

I am using gxt(2.6) messagebox in my gwt application..
I tried this following code to keep closable icon..but its not working
Messagebox.setclosable(true);
A bug?:
http://www.sencha.com/forum/showthread.php?50995-FIXED-Bug-in-MessageBox.setClosable%28true%29
You could also easily build a custom component to have a popup window with "close" button, such as:
public class EnhancedDialogBox extends DialogBox {
public EnhancedDialogBox(String header, String text){
setText(header);
FlowPanel fp = new FlowPanel();
Button ok = new Button("Close");
ok.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
EnhancedDialogBox.this.hide();
}
});
Label lbl = new Label(text);
fp.add(lbl);
fp.add(ok);
add(fp);
}
}
in case of dire need or as a quick and dirty fix or workaround.
Its a unfixed bug in gxt 2.6 ..I reviewed the alert() function in MessageBox.class..
public static Messagebox alert(String title, String msg,
Listener<MessageboxEvent> callback) {
Messagebox box = new Messagebox();
box.setTitle(title);
box.setMessage(msg);
box.callback = callback;
box.setButtons(OK);
box.icon = WARNING;
box.show();
return box;
}
If we add box.setClosable(true); this in existing method it will work fine
public static Messagebox alert(String title, String msg,
Listener callback) {
Messagebox box = new Messagebox();
box.setTitle(title);
box.setMessage(msg);
box.callback = callback;
box.setButtons(OK);
box.icon = WARNING;
box.setClosable(true);
box.show();
return box;
}

Double click event prob

When ever I double click the tree viewer file that file name should appear in the next form view
Sample.dat -> double click -> name should be appear Sample.dat and File related properties should be appear.
I created a treeViewer files and double click the file name that name is not appear.
Please kindly help me reg this,eclipse rcp,Juno,e4 etc..
Perspective class...
private void setLayouts(IPageLayout layout) {
// Range 0.05f to 0.95f....
IFolderLayout explorerFolderLayout = layout.createFolder("explorer", IPageLayout.LEFT, 0.25f, layout.getEditorArea());
explorerFolderLayout.addView(ExplorerView.ID);
view class..
viewer.addDoubleClickListener(new IDoubleClickListener() {
#Override
public void doubleClick(DoubleClickEvent event) {
// TODO Auto-generated method stub
IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
FileName fn = null;
FileNameContainer fnc = null;
if (selection.isEmpty())
return;
List<Object> list = selection.toList();...
....
You need to add click listener to your tree
final Tree tree = new Tree(parent, SWT.BORDER);
tree.addMouseListener(new MouseListener(){
public void mouseDoubleClick(MouseEvent e) {
//get data of selected element
//we use array[0], because we have not multiselect tree
MyDataObject data = (MyDataObject) tree.getSelection()[0].getData();
//now you need to pass "data" to your view with file info
//I don't know how to do it in pre e4 RCP apps, so
//please read this answer: https://stackoverflow.com/a/2227764/1030113
//hope that helps
}
public void mouseDown(MouseEvent e) {}
public void mouseUp(MouseEvent e) {}
});
//to test
TreeItem treeItem1 = new TreeItem(tree, SWT.NONE);
treeItem1.setText("some item 1");
treeItem1.setData(new MyDataObject("somedata 1"));
TreeItem treeItem2 = new TreeItem(tree, SWT.NONE);
treeItem2.setText("some item 2");
treeItem2.setData(new MyDataObject("somedata 2"));
I don't know how to communicate between view in pre e4 RCP apps, but I found some usefull answers here: https://stackoverflow.com/a/2227764/1030113

GWT Tab panel close

I am building an application in GWT. I have a decorated tabpanel in
my application.Where in am adding panels to it dynamically.Now i want
to achieve the closing of these tabs. I want to add a close image to
the tab bar and event to that image for closing. I am using UIbinder.
the working code is like that;
private Widget getTabTitle(final Widget widget, final String title) {
final HorizontalPanel hPanel = new HorizontalPanel();
final Label label = new Label(title);
DOM.setStyleAttribute(label.getElement(), "whiteSpace", "nowrap");
ImageAnchor closeBtn = new ImageAnchor();
closeBtn.setResource(images.cross());
closeBtn.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
int widgetIndex = tabs.getWidgetIndex(widget);
if (widgetIndex == tabs.getSelectedIndex()) {
tabs.selectTab(widgetIndex - 1);
}
tabs.remove(widgetIndex);
}
});
hPanel.add(label);
hPanel.add(new HTML("&nbsp&nbsp&nbsp"));
hPanel.add(closeBtn);
hPanel.setStyleName("gwt-TabLayoutPanelTab");
return hPanel;
}
In order to add tab,
public void addTab() {
TabWriting tw = new TabWriting(); /* TabWriting in my case, this can be any widget */
tabs.add(tw, getTabTitle(tw, "Writing"));
tabs.selectTab(tw);
}
You'll going to need, ImageAnchorClass
public class ImageAnchor extends Anchor {
public ImageAnchor() {
}
public void setResource(ImageResource imageResource) {
Image img = new Image(imageResource);
img.setStyleName("navbarimg");
DOM.insertBefore(getElement(), img.getElement(), DOM
.getFirstChild(getElement()));
}}
It isn't supported natively in GWT.
You can manually try to add it.
Read this - http://groups.google.com/group/google-web-toolkit/browse_thread/thread/006bc886c1ccf5e1?pli=1
I haven't tried it personally, but look at the solution by gregor (last one).
You kinda need to do something along the lines of this
GWT Close button in title bar of DialogBox
First you need to pass in the tab header when you create the new tab. The header you pass in should have your tab text and also an X image or text label to click on. Then add a event handler on the close object that gets the widget you are adding to the tabPanel and removes it. Here is some inline code that works
public void loadTab(final Widget widget, String headingText, String tooltip) {
HorizontalPanel panel = new HorizontalPanel();
panel.setStyleName("tabHeader");
panel.setTitle(tooltip);
Label text = new Label();
text.setText(headingText);
text.setStyleDependentName("text", true);
Label close = new Label();
close.setText("X");
close.setTitle(closeText_ + headingText);
text.setStyleDependentName("close", true);
close.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
Window.alert("close this tab");
ClientGlobal.LOG.info("widget : " + tabPanel_.getWidgetIndex(widget));
tabPanel_.remove(tabPanel_.getWidgetIndex(widget));
}
});
panel.add(text);
panel.add(close);
panel.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_LEFT);
panel.setCellHorizontalAlignment(close, HasHorizontalAlignment.ALIGN_RIGHT);
tabPanel_.add(widget, panel);
tabPanel_.getTabWidget(widget).setTitle(tooltip);
tabPanel_.selectTab(widget);
}

Setting ListGrid selection in SmartGWT

I'm trying to set the selected records of a ListGrid table object in SmartGWT, but I can't find any way of doing it. I know there's a getSelectedRecords() function, but no matching setSelectedRecords(). I tried to see if set/getSelectedState() would work, but GWT complains about needing a primary key and a DataSource object. Is there any way to set the selection of a ListGrid?
For this you can use one of the selectRecords() methods, like so:
public void onModuleLoad()
{
VLayout main = new VLayout();
final ListGrid grid = new ListGrid();
grid.setHeight(500);
grid.setWidth(400);
grid.setFields(new ListGridField("name", "Name"));
grid.setData(createRecords());
final IButton button = new IButton("Select some");
button.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event)
{
grid.selectRecords(new int[]{2, 3}); //This will select index 2 and 3
}
});
main.addMember(grid);
main.addMember(button);
RootPanel.get().add(main);
}
private ListGridRecord[] createRecords()
{
return new ListGridRecord[]{
createRecord("monkey"),
createRecord("banana"),
createRecord("orange"),
createRecord("sun")
};
}
private ListGridRecord createRecord(String name)
{
ListGridRecord record = new ListGridRecord();
record.setAttribute("name", name);
return record;
}