Eclipse SWT Maintaining Same layout in 2 separate composites - eclipse

Hello Everyone,
I have 2 Groups and each group has 3 Label and Text combinations, as you can see from the above Image.
I would like the text field to grab as much as space possible.hence, I have used grab 'true' only for the text field.
These label and text fields are well aligned inside their own group/composite.
But, when these 2 Groups are considered as a whole, the label and text fields are out of alignment.
Is there a way to align these label and text fields across multiple Groups?
is there a way to align these labels and text fields, without declaring each groups having equal column width, and specifying the number of columns label and text fields occupy?
Below is the code I have used for creating the below groups.
private void createFirstGroup(Composite parent) {
Group firstGroup = new Group(parent, SWT.NONE);
firstGroup.setText("first group");
GridDataFactory.fillDefaults().grab(true, false).applyTo(firstGroup);
GridLayoutFactory.fillDefaults().numColumns(2).equalWidth(false).applyTo(firstGroup);
String[] labels = new String[]{"Kwd","Variable","Value"};
for(int index=0;index<labels.length; index++){
Label label = new Label(firstGroup, SWT.NONE);
label.setText(labels[index]);
GridDataFactory.fillDefaults().applyTo(label);
Text textField = new Text(firstGroup, SWT.BORDER);
GridDataFactory.fillDefaults().grab(true, false).applyTo(textField);
}
}
private void createSecondGroup(Composite parent) {
Group secondGroup = new Group(parent, SWT.NONE);
secondGroup.setText("second group");
GridDataFactory.fillDefaults().grab(true, false).applyTo(secondGroup);
GridLayoutFactory.fillDefaults().numColumns(2).equalWidth(false).applyTo(secondGroup);
String[] labels = new String[]{"Count","Upper Comment","Lower Comment"};
for(int index=0;index<labels.length; index++){
Label label = new Label(secondGroup, SWT.NONE);
label.setText(labels[index]);
GridDataFactory.fillDefaults().applyTo(label);
Text textField = new Text(secondGroup, SWT.BORDER);
GridDataFactory.fillDefaults().grab(true, false).applyTo(textField);
}
}

Related

itextsharp - Add to a PdfPTable a cell with right-aligned italic text

Dear Internet Community.
I am trying to build an itextsharp PdfPTable with a row of footer cells with italic text, and I wish to right-align stuff like amount values.
Either I am doing it wrong, or setting alignment AND style is mutually exclusive.
private void AddCellFooterRightAlign(PdfPTable pdfPTable, string text)
{
var phrase = new Phrase(text)
{
Font = FontFactory.GetFont("Arial", Font.DEFAULTSIZE, Font.ITALIC),
};
var pdfPCell = new PdfPCell(phrase)
{
HorizontalAlignment = Element.ALIGN_RIGHT,
};
pdfPTable.AddCell(pdfPCell);
}
This yields a cell with right-aligned, normal-style text.
private void AddCellFooterRightAlign(PdfPTable pdfPTable, string text)
{
var phrase = new Phrase(text)
{
Font = FontFactory.GetFont("Arial", Font.DEFAULTSIZE, Font.ITALIC),
};
var pdfPCell = new PdfPCell()
{
HorizontalAlignment = Element.ALIGN_RIGHT,
};
pdfPCell.AddElement(phrase);
pdfPTable.AddCell(pdfPCell);
}
This yields the opposite: normal-aligned (left), italic-style text.
Notice the subtle difference: By sending the Phrase-object into the Cell constructor, I retain the alignment, but by using AddElement I retain the font style.
Note: I'm stuck with v.5.5.3.0 for the foreseeable future.
Thanks!
-S
The problem is that you are using a Phrase. Use a Paragraph where you can set the font and the alignment.

How do I add a radio group to a table cell using iTextSharp 4.1.6?

I'm trying to generate a dynamic survey as a PDF. The question text appears in the first cell, and the field appears in the 2nd cell. I have no problems when adding a text field or drop down field, but I cannot get a radio group to display. I am able to get a single radio button, but would like the entire group to display in the cell. Is this possible? And if so, how?
Here is a code sample of what I've tried thus far.
Table.AddCell(Question.TEXT + ":");
PdfFormField RadioGroup = PdfFormField.CreateRadioButton(ExportWriter, true);
RadioGroup.FieldName = "field" + Question.QUESTION_ID;
using (Entities _context = new Entities())
{
QuestionOptions = SURVEY.GetQuestionOptions(Question.QUESTION_ID, _context).ToList();
}
RadioCheckField Radio;
PdfFormField RadioField = null;
foreach (OPTIONS Option in QuestionOptions)
{
Radio = new RadioCheckField(ExportWriter, new Rectangle(40, 806 - count * 40, 60, 788 - count * 40), Option.OPTION_NAME, "option" + Option.OPTION_ID.ToString());
Radio.BackgroundColor = new GrayColor(0.8f);
Radio.CheckType = RadioCheckField.TYPE_CIRCLE;
RadioField = Radio.RadioField;
RadioField.SetWidget(new Rectangle(10, 20), PdfAnnotation.HIGHLIGHT_INVERT);
RadioGroup.AddKid(RadioField);
}
ExportWriter.AddAnnotation(RadioGroup);
Cell = new PdfPCell();
Events = new iTextSharp.text.pdf.events.FieldPositioningEvents(ExportWriter, RadioGroup);
Cell.CellEvent = Events;
Table.AddCell(Cell);
The issue is that when the radio buttons appear, they appear on the bottom left of the page, and not within the cell.
Thanks in advance!

horizontalSpan in SWT GridLayout doesn't span

I'm trying to configure a dialog window like so:
#Override
protected Control createDialogArea(Composite parent) {
GridLayout dialogAreaLayout = new GridLayout();
dialogAreaLayout.numColumns = 2;
parent.setLayout(dialogAreaLayout);
GridData gridData = new GridData();
gridData.grabExcessHorizontalSpace = true;
gridData.horizontalAlignment = GridData.FILL;
gridData.minimumWidth = 300;
// the label in the first row should span across both columns
gridData.horizontalSpan = 2;
Label headlineLabel = new Label(parent, SWT.NONE);
headlineLabel.setText("example test");
headlineLabel.setLayoutData(gridData);
// the rest should be ordered in two columns
gridData.horizontalSpan = 1;
companyLabel = new Label(parent, SWT.NONE);
companyLabel("company");
companyTextfield = new Text(parent, SWT.BORDER);
companyTextfield(gridData);
...
What I'm trying to accomplish is a label in the first line, that spans across both columns and have the following fields be ordered in pairs per line. What I get with this is, that the lable (that should be left in second line) is right in first line, just as the label wouldn't span across two columns. Can anybody see, what I'm doing wrong?
Thanx again!
After setting the layoutData to headlineLabel, you are setting the horizontalSpan to 1. You need to create a new LayoutData object for every SWT widget. It is not possible to reuse them after setting them.

How to load dynamic data on a eclipse shell?

I want to load details of a user's when clicking their names . So I create a Grid Layout with two columns left hand side is standalone and right hand side data will be loaded based on clicking their names .
JAVA CODE
Display display = PlatformUI.createDisplay();
Shell shell = new Shell(display);
shell.setSize(400, 400);
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
shell.setLayout(gridLayout);
shell.setBackground(new Color(display, 255, 255, 255));
final Table table = new Table(shell, SWT.VIRTUAL);
table.setLayout(new GridLayout());
table.setSize(400, 400);
final TableColumn column = new TableColumn(table, SWT.LEFT);
column.setWidth(150);
column.setText("Runnable Services");
table.setItemCount(5);
final String[] services = new String[] { "Franklin", "Christian",
"Richard Levi",
"Dale Steyn", "Chris Gayle" };
table.addListener(SWT.SetData, new Listener() {
public void handleEvent(Event event) {
TableItem item = (TableItem) event.item;
int index = table.indexOf(item);
item.setText(services[index]);
System.out.println(item.getText());
}
});
table.addSelectionListener(new OnClickListener());
alignCenter(shell, display);
shell.open();
I added a selection listener on a table . When click the players I add their details on SelectionListener's widget Selected method .
JAVA CODE
Composite parent = new Composite(table.getParent(), SWT.NONE);
parent.setLayout(new GridLayout(2, false));
Label label = new Label(parent, SWT.NULL);
label.setText("Name :");
System.out.println(table.getSelection()[0].getText());
Text text = new Text(parent, SWT.BORDER);
text.setText(table.getSelection()[0].getText());
text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
| GridData.GRAB_HORIZONTAL));
ViewActionDelegate.resize(200, true, 20, table.getShell());
If i did like this on click action each time data will be appended to existing shell . How can I clear the righ hand side details ? . I posted a image below .
How can i do this ? .
Finally I found the answer .
Issue :
I append new elements into already created composite .
Solution:
Clear elements from existing composite before drawing new elements .
Java Code :
if ((composite != null) && (!composite.isDisposed())) {
Control[] children = composite2.getChildren();
for(Control ch : children){
ch.dispose();
}
}

Table shows extra blank columns at the end

I am using jface tableViewer.When table has no data in it ,it shows all columns correctly But when Data gets added to the table it shows extra blank space or column at the end of the table.
I am using TreeViewer + TreeViewerColumn and had this problem too, this workaround might work for your TableViewer too: Programmatically set the size of the last column on parent resize:
treeViewer.getTree().addControlListener(new ControlAdapter() {
public void controlResized(ControlEvent e) {
packAndFillLastColumn();
}
});
where the action is in
// Resize last column in tree viewer so that it fills the client area completely if extra space.
protected void packAndFillLastColumn() {
Tree tree = treeViewer.getTree();
int columnsWidth = 0;
for (int i = 0; i < tree.getColumnCount() - 1; i++) {
columnsWidth += tree.getColumn(i).getWidth();
}
TreeColumn lastColumn = tree.getColumn(tree.getColumnCount() - 1);
lastColumn.pack();
Rectangle area = tree.getClientArea();
Point preferredSize = tree.computeSize(SWT.DEFAULT, SWT.DEFAULT);
int width = area.width - 2*tree.getBorderWidth();
if (preferredSize.y > area.height + tree.getHeaderHeight()) {
// Subtract the scrollbar width from the total column width
// if a vertical scrollbar will be required
Point vBarSize = tree.getVerticalBar().getSize();
width -= vBarSize.x;
}
// last column is packed, so that is the minimum. If more space is available, add it.
if(lastColumn.getWidth() < width - columnsWidth) {
lastColumn.setWidth(width - columnsWidth);
}
}
Works well for me - you might want to set column resizable to false ;-). This can also be called when data in the last column changes (introducting / removing vertical scroll bar).
Thanks Thomas. Your idea worked for me as well, though I was using TableViewer and TableColumn.
Quoting my code so that others can take some hints.
`public void controlResized(ControlEvent e) {
if ( listOfTableColumns.size() != colProportions.length )
{
logger.warn( "Number of columns passed and size of column proportions array are different. " +
"Columns resizing shall not be effective on GUI window resizing" );
return;
}
Rectangle area = tableBaseComposite.getClientArea();
Point size = theTable.computeSize(SWT.DEFAULT, SWT.DEFAULT);
ScrollBar vBar = theTable.getVerticalBar();
int width = area.width - theTable.computeTrim(0,0,0,0).width - vBar.getSize().x;
if (size.y > area.height + theTable.getHeaderHeight()) {
// Subtract the scrollbar width from the total column width
// if a vertical scrollbar will be required
Point vBarSize = vBar.getSize();
width -= vBarSize.x;
}
Point oldSize = theTable.getSize();
if (oldSize.x > area.width) {
// table is getting smaller so make the columns
// smaller first and then resize the table to
// match the client area width
int index = 0 ;
for ( Iterator<TableColumn> iterator = listOfTableColumns.iterator(); iterator.hasNext(); )
{
TableColumn column = iterator.next();
column.setWidth( (int) numberFromPercentage( width, colProportions[index++] ) );
}
listOfTableColumns.get( listOfTableColumns.size() - 1).pack();
theTable.setSize(area.width, area.height);
} else {
// table is getting bigger so make the table
// bigger first and then make the columns wider
// to match the client area width
int index = 0;
theTable.setSize(area.width, area.height);
for ( Iterator<TableColumn> iterator = listOfTableColumns.iterator(); iterator.hasNext(); )
{
TableColumn column = iterator.next();
column.setWidth( (int) numberFromPercentage( width, colProportions[index++] ) );
}
listOfTableColumns.get( listOfTableColumns.size() - 1).pack();
}
}`
No need for complicated hacks to remove the extra unwanted column space at the end...
Just create a columnLayout:
TableColumnLayout columnLayout = new TableColumnLayout();
and then set it to each of your columns:
columnLayout.setColumnData(YOUR_VIEWER_COLUMN1.getColumn(), new ColumnPixelData(200));
columnLayout.setColumnData(YOUR_VIEWER_COLUMN2.getColumn(), new ColumnWeightData(200, 100));
Finally, set the layout on your parent composite:
parent.setLayout(columnLayout);
Full sample:
public void createPartControl(Composite parent) {
TableViewer viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
TableViewerColumn keyColumn = new TableViewerColumn(viewer, SWT.LEFT);
TableViewerColumn valueColumn = new TableViewerColumn(viewer, SWT.LEFT);
TableColumnLayout columnLayout = new TableColumnLayout();
columnLayout.setColumnData(keyColumn.getColumn(), new ColumnPixelData(200));
columnLayout.setColumnData(valueColumn.getColumn(), new ColumnWeightData(200, 100));
parent.setLayout(columnLayout);
}
Just guessing: maybe your columns do not get resized to fill all the table?
How do you set the widths of columns?
Consider using TableColumnLayout for the table container.
On windows, you will always get an extra column/row if the net width of all the columns that has been set up is less than the dimension of the table. So its always good to make your columns fit your table, also there is some space left for scroll bars, though I am not very sure about this, but its always better to specify whether you want vertical or horizontal scroll bars.
I used the packAndFillLastColumn() method and it worked for me. But I found one issue with it. My table was created with a border. After using the packAndFillLastColumn() method the border for the row no longer exists. I used the setLinesVisible(true) method within the packAndFillLastColumn() method but still that does not work.
So simple! Just remove this line in your table commands inside the createContents function:
table.getColumn(i).pack();
Good-luck
As a workaround use :
-For Column
use TableColumnLayout for the treeViewer's composite and set appropriate column data for each column using:
"tableColumnLayout.setColumnData(column,new ColumnWeightData(...as per your requirement));"
-For Row
Set GridData to the treeViewer's composite and provide height hint using
"gridData.heightHint = table.getItemHeight()*numberOfVisibleRows"
I found eclipse has marked it as WONTFIX.. so can not do much to remove this space..We have tp live with it...:)
To the end column we need to set the setWidth to window size or shell-size, parent-shell size like 1500,5000
final TableViewerColumn viewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
final TableColumn column = viewerColumn.getColumn();
column.setText(title);
column.setResizable(true);
column.setMoveable(true);
//set the setWidth size upto shell size or set upto to some size like 1000,1500,2000,5000
col.setWidth(comp.getShell().getSize().x); // or col.setWidth(1500) ;
return viewerColumn;