Add SWT widget Group when a checkbox is selected - java

I have a GUI which has 3 different sections. In one of the section I have 2 checkboxes.
I want to add one more widget multiple selection List when one of the checkbox is selected.
I added a selectionListener to checkbox and when it is selected I am calling method which creates multiple selection List.
Problem is this list is not getting added in the GUI when checkbox is selected and it is not removed when checkbox is unchecked.
I am not able to find the cause. Can anybody help me?
Below is the code to add the multiple selection list
private void createFirstLevelSubFolderGroup() {
GridData gridData = new GridData();
gridData.grabExcessVerticalSpace = true;
gridData.verticalAlignment = org.eclipse.swt.layout.GridData.FILL;
gridData.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
gridData.grabExcessHorizontalSpace = true;
subFolderGroup = new Group(this, SWT.NONE);
subFolderGroup.setLayout(new FillLayout());
subFolderGroup.setLayoutData(gridData);
subFolderGroup.setText("First Level SubFolder");
firstLevelFolderList = new List(subFolderGroup, SWT.V_SCROLL | SWT.MULTI);
subFolderGroup.setVisible(true);
//firstLevelFolderList.setVisible(false);
}
//code where the call to add this list is there
//code adding a checkbox is here and below am adding a listener to that checkbox
ArchiveCheckbox.addSelectionListener(new SelectionAdapter()
{
#Override
public void widgetSelected(SelectionEvent e)
{
if (ArchiveCheckbox.getSelection()) {
// here am trying to call the method which adds multiple selection list
createFirstLevelSubFolderGroup();
}
else {
// I want to remove that widget
subFolderGroup.setVisible(false);
firstLevelFolderList.removeAll();
}
}
}
Basically I am not able to add this dynamically.
A small code snippet which demonstrates my scenario is fine if the code which I provided doesn't have the req info.
![Below is the gui which I am creating. When check box prepare archive for catch is selected I want a multiple selection list should appear just below the checkbox and it should disappear when it is unchecked. Currently when it is checked multiple selection list appearing but it hides the other group Model that contains sources ][1]
[2]: http://i.stack.imgur.com/Xl2ml.png

Create all the controls at the start. Set a GridData layout on each control you want to hide and set the exclude flag to true and make in control invisible. So something like:
Control control = .. create control ...
GridData data = new GridData(flags);
data.exclude = true;
control.setLayoutData(data);
control.setVisible(false);
When you want to make the controls visible set the exclude flag to false, make the control visible and call layout() on the parent Composite.
GridData data = (GridData)control.getLayoutData();
data.exclude = false;
control.setVisible(true);
parentComposite.layout();

Related

how to put css styling for one specific checkbox in checkboxGroup of vaadin 8

I have a checkboxgroup which has multiple checkboxes. I have certain checkboxes which needs to look different, either by bold text or colored text ,which wont change irrespective of selection/unselection.
I have following code to build checkboxgroup. But I am not able to put style specific to one checkbox, because I dont have access to it. How can I do that
CheckBoxGroup<ReferenceScreenResultAnswer> answersOptionGroup = new CheckBoxGroup<>(question.getText());
List<ReferenceScreenResultAnswer> checkBoxItems = new ArrayList<>();
answersOptionGroup.setItems(checkBoxItems);
.......
// this is where i want to put CSS to specific checkbox/values
for (Answer answer : preSelectedAnswer)
{
ReferenceScreenResultAnswer rsra = new ReferenceScreenResultAnswer();
rsra.setAnswer(answer);
rsra.setReferenceScreenResultQuestion(rsrq);
answersOptionGroup.select(rsra);
}
I can do invidiual checkboxes like
CheckBox cb = new CheckBox();
cb.setCaptionAsHtml(true);
cb.setCaption("<b> hello </b> there");
But I am not able to access individual checkboxes from CheckBoxGroup. Any idea how to access them
i found the answer:
// css style the pre selected answer, so they look different irrespective
// of their selection
answersOptionGroup.setItemCaptionGenerator(new ItemCaptionGenerator<ReferenceScreenResultAnswer>()
{
#Override
public String apply(ReferenceScreenResultAnswer item)
{
if (preSelectedAnswer.contains(item.getAnswer()))
return "<strong>" + item.getAnswer().toString() + "</strong>";
else
return item.getAnswer().toString();
}
});

How to create user input field in Eclipse plugin using Eclipse Nebula Grid?

I am simply trying to create a Grid plugin that can have each cell be edited by a user. I found Eclipse Nebula and it seems pretty close to what I want to do except I can't figure out a way to make the cells editable. So far I have something simple like this:
public class SampleView2 extends ViewPart {
public SampleView2() {
}
public void createPartControl(Composite parent) {
// create Grid
Grid grid = new Grid(parent,SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
grid.setHeaderVisible(true);
// create column
GridColumn col = new GridColumn(grid, 0);
col.setText("First Column");
col.setWidth(140);
// write text in row
GridItem item = new GridItem(grid, 0);
item.setText("This is my first cell"); // <--- I want the user to be able to edit this
}
This code produces this:
As you can see, I can manually set the text in the cell but I want the user to be able to edit it.
In Eclipse Nebula Grid the GridEditor is for editable cells.
This code snippet gives an example of how to use the GridEditor.

SWT - Table Viewer center checkbox

The last column of my Table Viewer contains a check box only. The check box appears in the left side of the cell, and because the column name is pretty long it looks ugly as hell. How can I center the check box in the middle of the cell ? Is it possible without using images ? Here is how I create the column:
// third column - check box. temporary
TableColumn column = new TableColumn(viewer.getTable(), SWT.NONE);
column.setText("PrettyLongColumnName");
column.setWidth(100);
TableViewerColumn checkColumn = new TableViewerColumn(viewer, column);
checkColumn.setLabelProvider(new ColumnLabelProvider() {
// the checkboxes should be disposed and rebuilt when input changes
#Override
public void update(ViewerCell cell) {
MyObject system = (MyObject) cell.getElement();
TableItem item = (TableItem) cell.getItem();
Button button;
if (buttonsMap.containsKey(cell.getElement())) {
button = rightTableButtons.get(cell.getElement());
} else {
button = new Button((Composite) cell.getViewerRow().getControl(), SWT.CHECK);
button.setEnabled(true);
buttonsMap.put(cell.getElement(), button);
TableEditor editor = new TableEditor(item.getParent());
editor.grabHorizontal = true;
editor.grabVertical = true;
editor.setEditor(button, item, cell.getColumnIndex());
editor.layout();
}
}
}
});
TL;DR: Not available nativley, but can be implemented via epic hackery.
Checkboxes are actually an OS feature. As SWT is cross-platform, we rely on it being provided by OS.
AFIK the only thing provided by all OS's (Gtk/Win32/Cocoa) is a single checkbox on the first column.
Other
fancy functionality has to be implemented manually.
One way I've seen people do it is to draw custom icons and then update the icon when you click on it with event listeners.
One example on how to draw icons on the right is in this snippet. You'd have to add click listener to change the icon when clicked into checked/unchecked.
Note, this may cause your application to look inconsistent across platforms and themes (e.g dark theme).
To get around this, we have had some people that actually generate a native checkbox, then programatically take a screen shot, then draw it in the right side of a column. I think this is hackery at it's finest.
Let me know if you have questions.

How can I set visible to hide the jDialog (inside if condition) that is set visible shown (outside if loop)?

Here I want to open a DialogFrame containing an error message when a buttonGroup is not active & the search button is clicked. So inside the ActionEvent I have made the DialogFrame to setVisible(true). But when the button group is active & I click the search button (inside the if condition), the setVisible(false) doesn't seem to work, in other word the DialogFrame still pop ups!
How can I turn the visibility off of the DialogFrame inside the if condition?
private void jButtonSearchActionPerformed(java.awt.event.ActionEvent evt) {
SrchEMsg sem = new SrchEMsg(this);
sem.setVisible(true);
sem.setLocationRelativeTo(null);
sem.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
if (bgGroup.getSelection() != null) {
sem.setVisible(false); //doesn't work.
SrchResult sr = new SrchResult();
sr.setVisible(true);
sr.pack();
sr.setLocationRelativeTo(null);
sr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.dispose();
}
}
I would recommend not to manipulate the visibility but simply not to create sem at all if some condition is met:
if (bgGroup.getSelection() == null) {
// only handle `sem`
SrchEMsg sem = new SrchEMsg(this);
sem.setVisible(true);
sem.setLocationRelativeTo(null);
sem.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
} else {
// only handle `sr`
SrchResult sr = new SrchResult();
sr.setVisible(true);
sr.pack();
sr.setLocationRelativeTo(null);
sr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.dispose();
}
Keep it simple. Get rid of
sem.setVisible(true);
and instead simply do
sem.setVisible(bgGroup.getSelection() == null);
only set it visible if need be
If instead your wish to set the dialog invisible when the user makes a selection, then you can't do this in dialog creation code, but rather need to respond to the appropriate event, such as an ActionListener or ItemListener added to your JRadioButtons.

Trying to add a menu to a TableViewer's header row (titles row)

I am trying to add an action to a menu to the header (titles) of a TableViewer.
This is the code that I am using now:
viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL
| SWT.FULL_SELECTION | SWT.BORDER);
//...
MenuManager manager = new MenuManager();
viewer.getControl().setMenu(manager.createContextMenu(viewer.getControl()));
manager.add(new Action("MENU ITEM TEXT") {
#Override
public void run() {
// get the current selection of the tableviewer
IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
// do something
if (selection.getFirstElement() instanceof MyObject)
return;
System.out.println("OK: "+selection.getFirstElement().getClass().getName());
}
});
And this is how it looks:
The problem is that the menu gets added to the entire TableViewer, not only to the header row.
Because for the other rows I will need to use a different menu.
I have tryied to find a way of adding the action only to the top row (the titles row), but with no success so far.
So how can I add the menu only for the header?
You add a MouseListener to the table, and you check for the following things:
Clicked button is mouse right-click.
The pointer of the event is located within the bounds of your table item (i.e. your first TableItem - you will use table.getItem(Point)).
If these conditions are met, you open the menu at mouse location.
Actually, here's a snippet of how this can be done.

Categories

Resources