How to add an image to a label in SWT - swt

Click here, in this example the text appears on the right side I want it to be under the image.
How to do this using SWT itself?

To show an image with text underneath you'd need two labels and a layout to position them under each other
Composite parent = new ...
RowLayout layout = new RowLayout( SWT.VERTICAL );
layout.center = true;
parent.setLayout( layout );
Label imageLabel = new Label( parent, SWT.NONE );
imageLabel.setImage( ... );
Label textLabel = new Label( parent, SWT.NONE );
textLabel.setText( "text" );
In order to left-align the labels, omit the layout.center = true.
By default, the RowLayout has a spacing of 3 pixels that can be changed through the spacing field.

Related

Eclipse SWT Maintaining Same layout in 2 separate composites

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);
}
}

Unity Editor GUI, change the label width of EditorGUILayout.Toggle

I can't find a way to increase the label width of EditorGUILayout.Toggle. Here's my code, it doesn't do anything and Unity clips the text and cuts it short.
GUILayoutOption[] options = new GUILayoutOption[] {
GUILayout.Width(400.0f),
GUILayout.MinWidth(250.0f),
GUILayout.ExpandWidth(true)
};
MyBoolValue = EditorGUILayout.Toggle("My Long Description Text Here", MyBoolValue, options);
I did try wrapping the Toggle button with
EditorGUILayout.BeginHorizontal();
EditorGUILayout.EndHorizontal();
But it also didn't do anything. What can I do remove clipping from the text?
Set EditorGUIUtility.labelWidth before doing your Toggle, and then restore it to its original value so you don't mess up any subsequent controls.
float originalValue = EditorGUIUtility.labelWidth;
EditorGUIUtility.labelWidth = 400;
MyBoolValue = EditorGUILayout.Toggle("My Long Description Text Here", MyBoolValue);
EditorGUIUtility.labelWidth = originalValue;

How to put margin between icon and text of an SWT Button?

I would like to have some margin between text and icon (image) on my Button.
That's what I have:
Button btn = new Button(grpInventar, SWT.NONE);
GridData gd_btn = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
gd_btn.widthHint = 154;
btn.setLayoutData(gd_btn);
btn.setAlignment(SWT.LEFT);
btn.setImage(this.getImageByLocation(Application.getIconLocByInventoryClass(Whateverimage.class )));
btn.setText("Text");
I would love to add something like:
btn.setMarginBetweenTextIcon(12)
Is there something out there?
There is no support for this in SWT. You could use an image with empty pixels on the right or text with leading spaces.
Also note that not all platforms support having an image and text in a button.

Dynamically positioning the table and bar chart generated using jfree chart

Hi All Iam using itextpdf-5.1.0.jar, displaying Table(the values in this table are fetched from the database based on start date and end date in the input screen) and bar chart for the values in the above table.
The problem is I want to position the bar chart below the table, values that iam fetching from the database may vary depending upon start date and end date, now Iam positioning table and bar chart by giving some static values(234,567) , if there are large no of values table values and bar chart are getting overrided. Is there any other way to position table and bar chart dynamically.
Document document = new Document(PageSize.A4_LANDSCAPE, 10, 10, 10, 10);
document.open();
document.add(new Paragraph("Batch Report:", FontFactory.getFont(FontFactory.COURIER, 10, Font.BOLD, new CMYKColor(255, 255, 255, 255))));
Paragraph paragraph1 = new Paragraph();
paragraph1.setSpacingBefore(4);
document.add(paragraph1);
PdfPTable table = new PdfPTable(5);
table.setWidthPercentage(100);
PdfPCell c1;
for (BoardBean bean : listHeader) {
addColumn(bean.getID(),c1,table,myColor,btableheadercolor);
}
Add Column values to table
private void addColumn(String text,PdfPCell c1,PdfPTable dataTable,BaseColor myColor,BaseColor btablecolumncolor) {
try {
final Font tabletdcolor = new Font(Font.FontFamily.HELVETICA, 6, Font.NORMAL, BaseColor.BLACK);
c1 = new PdfPCell(new Paragraph(text, tabletdcolor));
cellStyle(c1, myColor, btablecolumncolor);
dataTable.addCell(c1);
} catch (Exception ex) {
ex.printStackTrace();
}
}
Generating bar chart
JFreeChart reportBarChart = genBatchReportBarChart(listHeader);
PdfTemplate reportTemplate = contentByte.createTemplate(280, 230);
Graphics2D reportGraphics = reportTemplate.createGraphics(280, 230, new DefaultFontMapper());
Rectangle2D reportRectangle = new Rectangle2D.Double(0, 0, 280, 230);
reportBarChart.draw(reportGraphics, reportRectangle);
reportGraphics.dispose();
contentByte.addTemplate(reportTemplate, 10, height+150);
Now in the above code iam fixing the position of bar chart and if the values are low in number its good but if they are large in number bar chart is overriding the table values.Depending upon the table values the bar chart need to be aligned how can I achieve that.
You can ask the table for its total height after you've added the table to the document, and use that table height to decide where to put the chart.
Or (even easier to achieve): you can wrap the PdfTemplate inside an Image object:
Image img = Image.getInstance(reportTemplate);
Add that image with document.add() right after you've added the table (assuming that you're adding the table with document.add()).

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;