JFace : Adding images to a table viewer is creating an issue - java

I have a table viewer with 5 columns. In the 1st column I check some conditions and add an image and the rest of the columns will have text, But when I try to add the image and then add the rest of the columns with text then the 2nd column's text sits right beside the image in the 1st column. Here's my sample code snippet :
TableItem item = new TableItem(table, SWT.NONE);
if(condition) {
item.setImage(image);
}
else {
item.setImage(image);
}
item.setText(new String[]{"street", "city","state","Zip Code"});
Problem is that the string "street" sits right beside the image in the 1st column itself but I want the string "street" in the second column and the rest of the strings in the subsequent columns. What am I doing wrong here? How do I add table items from the 2nd column?

Each column in the table can have both an image and text.
Calling item.setImage(image) sets the image for the first column.
Calling item.setText(String []) sets the text for each column starting at the first column. So your "street" goes next to the image.
If you don't want text in the first column then set the text for that column to blank:
item.setText(new String[]{"", "street", "city", "state", "Zip Code"});
Update:
You can also set the text for individual columns with
item.setText(column, text);
If you are using TableViewer you should not be using TableItem, instead use the label provider and content provider.

Not as clear. If you want just an image in the first colum then then just provide an empty string:
item.setText(new String[]{"", "street", "city","state","Zip Code"});
But it is not worth working with jface without using the benefits of ContentProvider and LabelProvider. So you should read some jface tutorials before doing stuff like that.
Example:
TableViewer tableViewer = new TableViewer(container, SWT.BORDER | SWT.FULL_SELECTION);
tableViewer.setContentProvider(ArrayContentProvider.getInstance());
TableViewerColumn tableViewerColumnImg = new TableViewerColumn(tableViewer, SWT.NONE);
tableViewerColumnImg.setLabelProvider(new ColumnLabelProvider()
{
#Override
public Image getImage(Object element)
{
Person p = (Person) element;
return p.getImage();
}
});
TableColumn tblclmnImg = tableViewerColumnImg.getColumn();
tblclmnImg.setWidth(100);
tblclmnImg.setText("Img");
TableViewerColumn tableViewerColumnStreet = new TableViewerColumn(tableViewer, SWT.NONE);
tableViewerColumnStreet.setLabelProvider(new ColumnLabelProvider()
{
#Override
public String getText(Object element)
{
Person p = (Person) element;
return p.getStreet();
}
});
TableColumn tblclmnStreet = tableViewerColumnStreet.getColumn();
tblclmnStreet.setWidth(100);
tblclmnStreet.setText("Street");
tableViewer.setInput(//set an array of persons here
);

Related

Java SWT clear table headers

I'm working on a SWT GUI and I'm trying to create a button that on press clear all table data and the headers.
table.removeAll();
This command don't work well because it's removing only the data inside and I need to remove the table headers too.
Is there a solution?
EDIT: after the code to clear the header worked, if I try to add new file with data, the header go to the next point. why if it blank? (the ArrayLists that contains the header names cleared too).
First image is when first file uploaded, after click on "Start" button the data shown on table:
Second image is after the table cleared all data by pressing on another button:
Third image is after uploading new file and press "Start" button:
EDIT: Headers Set
tableConfigurationSystemColumnTools.add("Parameter Name");
for (String str : tableSystemColumn) {
String[] a = str.split("PCM");
tableConfigurationSystemColumnTools.add(a[0].trim());
}
for (int loopIndexSystemColumnTools = 0;
loopIndexSystemColumnTools < tableConfigurationSystemColumnTools.size(); loopIndexSystemColumnTools++) {
TableColumn column = new TableColumn(tableConfigurationSystem, SWT.NULL);
column.setWidth(100);
column.setText(tableConfigurationSystemColumnTools.get(loopIndexSystemColumnTools));
}
for (int loopIndexSystemColumnTools = 0; loopIndexSystemColumnTools < tableConfigurationSystemColumnTools.size(); loopIndexSystemColumnTools++) {
tableConfigurationSystem.getColumn(loopIndexSystemColumnTools).pack();
}
EDIT: I've founded the answer, look at my comment.
button.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
table.removeAll();
TableColumn[] columns = table.getColumns();
for (int i = 0; i < columns.length; i++) {
columns[i].setText("");
}
}
});
I founded the answer - in the "for" loop that creating TableColumn, I'v added "index":
TableColumn column = new TableColumn(tableConfigurationSystem, SWT.NONE, loopIndexSystemColumnTools);
this is working.
Thank you guys.

How to add CheckBox object in table in JAVA Swing

I am having a table which is getting data from database. But I want to add a row with checkbox having attributes as name But everytime I run the program it show the value as
javax.swing.JCheckBox[ , 0, 0, 0x0, invalid, alignmentX = 0.0, alignmentY = 0.5, border = java................
Here is the code.
while(rs.next()) {
Vector row = new Vector();
String name = rs.getString("name");
String catid = rs.getString("catalogid");
String brand = rs.getString("brand");
String counter = rs.getString("counter");
String qty = rs.getString("qty");
String price = rs.getString("column_price");
row.add(name);
row.add(catid);
row.add(brand);
row.add(counter);
row.add(qty);
row.add(price);
cb = new JCheckBox(name, true);
row.add(cb);
model.addRow(row);
}
You don't add components to the TableModel of a JTable. You add data and use renderers to render the data.
So in your case you need to:
add Boolean.TRUE as the data to the TableModel.
override the getColumnClass(...) method of the TableModel to return Boolean.class so the table can render the Boolean object as a check box.
Read the Swing tutorial on How to Use Tables for more information and examples to get you started.

Fixing some content at the end of first page aspose words java

I am working with apose words java recently.
In my first page I have a table need to merge, which can grow any size, no fixed number of rows and at the end of my first page, I want to keep some content (for example contact details) to be fixed. (Note: I can't keep contact details in Footer or in foot note section because of some formatting I need to ensure which can't maintain in footer or foot note section)
On growing of table as many rows, My content is going down, But I want to fix it at the end of my first page. if table grows bigger in size, wanted to skip the content and render table in next page.
is there any solution/work around for this?
My expected results are like below....
Page 1 Start
dynamic Table row1
dynamic Table row2
dynamic Table row3
Contact Details ,wanted to fix at the end of my first page
Page 1 end
Page 2 Start
dynamic table row 4
dynamic table row 5
........
For your scenario, ideally the contact details should be set in a footer. It is possible, but very risky.
First create a new document, either in Aspose.Words or MS Word, it will be used as a template.
Add a blank table on top
Add contact details, after the blank table
Add a bookmark, after the contact details
Now, using Aspose.Words, you can check the location of the bookmark, every time you are adding a new row in the table. If bookmark is at page 1, add new row to the first table. If bookmark is at page 2, add new row to the second table. Below is the sample code that adds rows to the table, keeping the contact details fixed on page 1.
Template document: Google drive link
Java source code is given below.
public static void main(String[] args)
{
try
{
String template = Common.DATA_DIR + "Contact Template.docx";
String saveDocument = Common.DATA_DIR + "Contact with tables.docx";
String bookmarkNameContact = "ContactEnd";
// Load the template
com.aspose.words.Document wordDoc = new com.aspose.words.Document(template);
DocumentBuilder builder = new DocumentBuilder(wordDoc);
// Find the contacts bookmark
com.aspose.words.Bookmark bookmarkContact = wordDoc.getRange().getBookmarks().get(bookmarkNameContact);
// Set the table with null
com.aspose.words.Table table = null;
// Add some rows
for (int i = 0; i < 50; i++)
{
// If contacts bookmark is on 1st page, add new rows to first table
if (getBookmarkPage(wordDoc, bookmarkContact) == 1)
{
table = (com.aspose.words.Table) wordDoc.getChild(NodeType.TABLE, 0, true);
} else
{
// If the contacts bookmark is on second page, add rows to second table
table = (com.aspose.words.Table) wordDoc.getChild(NodeType.TABLE, 1, true);
// If there is no second table, create it
if (table == null)
{
table = createNewTable(wordDoc, bookmarkContact);
}
}
// Add rows dynamically to either first or second table
addRow(wordDoc, table, "some text " + i);
}
// Save the document
wordDoc.save(saveDocument);
} catch (Exception ex)
{
System.err.println(ex.getMessage());
}
}
private static com.aspose.words.Table createNewTable(com.aspose.words.Document wordDoc, com.aspose.words.Bookmark bookmarkContact) throws Exception
{
// Get the first table and clone it to create the second one
com.aspose.words.Table firstTable = (com.aspose.words.Table) wordDoc.getChild(NodeType.TABLE, 0, true);
com.aspose.words.Table table = (com.aspose.words.Table) firstTable.deepClone(true);
// Add the second table after the bookmark
bookmarkContact.getBookmarkEnd().getParentNode().getParentNode().appendChild(table);
// Delete all its rows
table.getRows().clear();
return table;
}
// Add a new row to the table
private static void addRow(com.aspose.words.Document wordDoc, com.aspose.words.Table table, String text)
{
// Create a new row
com.aspose.words.Row row = new com.aspose.words.Row(wordDoc);
row.getRowFormat().setAllowBreakAcrossPages(true);
// Add it to the table
table.appendChild(row);
// Add cells to the row
for (int iCell = 0; iCell < 4; iCell++)
{
// Create a new cell and set text inside it
com.aspose.words.Cell cell = new com.aspose.words.Cell(wordDoc);
cell.appendChild(new com.aspose.words.Paragraph(wordDoc));
cell.getFirstParagraph().appendChild(new Run(wordDoc, text));
cell.getFirstParagraph().getParagraphFormat().setSpaceAfter(0);
row.appendChild(cell);
}
}
private static int getBookmarkPage(com.aspose.words.Document wordDoc, com.aspose.words.Bookmark bookmarkContact) throws Exception
{
// Find the page number, where our contacts bookmark is
LayoutCollector collector = new LayoutCollector(wordDoc);
return collector.getStartPageIndex(bookmarkContact.getBookmarkEnd());
}
I work with Aspose as Developer Evangelist.

tableviewercolumn setwidth percentages instead of pixels

the setWidth method for the TableViewerColumn class takes integer types, I would really like to use percentages, is there anyway to do this, or pack the table or something?
Use TableLayout to layout your table and use ColumnWeightData to specify the 'weight' of each column.
For example, two columns with a 60 / 40 weighting:
TableViewer viewer = ....
TableLayout layout = new TableLayout();
TableViewerColumn col1 = new TableViewerColumn(viewer, SWT.LEAD);
layout.setColumnData(col1.getColumn(), new ColumnWeightData(60));
TableViewerColumn col2 = new TableViewerColumn(viewer, SWT.LEAD);
layout.setColumnData(col2.getColumn(), new ColumnWeightData(40));
viewer.getTable().setLayout(layout);
And yes, you can pack the table/tree too.
for (final TreeColumn item : tree.getColumns()) {
item.pack();
}
for (final TableColumn item : table.getColumns()) {
item.pack();
}
Which is quite similar to the CTRL+NUM-PAD '+' Keycode on Windows.

Refreshing table Editor with table item in swt

Hello i have a following code for swt table
private Table folderAssociationTable;
private TableItem item;
private TableEditor editor;
folderAssociationTable = componentsRenderer.createTableWidget(container, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION, 2, true);
folderAssociationTable.addListener(SWT.MeasureItem, new Listener() {
public void handleEvent(Event event) {
event.height = 20;
}
});
componentsRenderer.createTableColumWidget(folderAssociationTable, SWT.CHECK, "Item", 80);
// add a column to display source folders
componentsRenderer.createTableColumWidget(folderAssociationTable,
SWT.LEFT, PropertyClass.getPropertyLabel(QTLConstants.SOURCE_FOLDER_COLUMN_LABEL), 200);
// add a column to display target folders
componentsRenderer.createTableColumWidget(folderAssociationTable,
SWT.LEFT, PropertyClass.getPropertyLabel(QTLConstants.TARGET_FOLDER_COLUMN_LABEL), 200);
and then adding table data in this fashion
item = new TableItem(folderAssociationTable, SWT.NONE);
editor = new TableEditor (folderAssociationTable);
Button button = new Button(folderAssociationTable, SWT.CHECK);
button.setText("item "+ (i+1));
button.pack();
editor.minimumWidth = button.getSize ().x;
editor.setEditor(button, item, 0);
item.setText(1, folderlist[i]); // add source folder to the first column of the table
item.setText(2, targetFolderPath); // add target folder to the second column of the table
sourceTargetFolderMap.put(folderlist[i], targetFolderPath);
I have remove button on this page outside table component , on its action Listener i am removing selected row from table , but in this case when table is getting updated only table items are updated but table editor remain at same position , how can i refresh both table editor and table items on click of remove button.
Have you seen the example given in the documentation? Add this to your ActionListener:
// to close the old editor
Control oldEditor = editor.getEditor();
if (oldEditor != null) oldEditor.dispose();
// add some logic if you want to open the editor again on a different row

Categories