I am creating a multiple column JMesa table. I want to pre-sort the table on a specified column. How can I achieve this?
You have to call addSort() on SortSet, then associate the SortSet with the TableModel. Sample code:
SortSet mySortSet = new SortSet() {};
mySortSet.addSort("your column property here", Order.ASC);
Limit limit = TableModelUtils.getLimit("table model id here", request, itemsList);
limit.setSortSet(mySortSet);
model.setLimit(limit);
Related
I have an existing Workbook with a Sheet that has a table ('table' refers to the Insert Table feature of the Excel UI with column headers and filter arrows, etc). I am having trouble determining which classes to use to edit existing rows of the table or inserting new rows into the table.
I have been successful (thanks in no small part to JasonPlutext) with writing new cell contents to existing cells as well as creating entirely new cells/rows. I'd rather not have to write the header row and all the data rows first and them make it into a table using the API, but I would like to know if anybody know how it should be done. In the past, I could just create the table with the number of rows I knew I would need but in this case the rows quantity is dynamic. I was hoping I could just reference a tablePart and then there would be some method for inserting into a List object.
Any guidance is appreciated.
EDIT:
So as a concrete example, let's say I have a workbook and sheet with an existing table of 2 columns with 2 rows (including the header) starting at A1. I can open the underlying xl>tables>table1.xml and see this:
<table xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="xr xr3" xmlns:xr="http://schemas.microsoft.com/office/spreadsheetml/2014/revision" xmlns:xr3="http://schemas.microsoft.com/office/spreadsheetml/2016/revision3" id="5" xr:uid="{FAABA541-34FC-423B-94F5-DDD8D784132E}" name="SummarySFTP" displayName="SummarySFTP" ref="A1:B2" totalsRowShown="0" headerRowDxfId="46" headerRowBorderDxfId="45" tableBorderDxfId="44">
<autoFilter ref="A1:B2" xr:uid="{93499C15-75FB-4436-A9B9-0C1FCBD787F4}">
<filterColumn colId="0" hiddenButton="0"/>
<filterColumn colId="1" hiddenButton="0"/>
</autoFilter>
<tableColumns count="2">
<tableColumn id="1" xr3:uid="{D4DA50CD-C581-4286-9B64-42B02B6646B6}" name="Status"/>
<tableColumn id="2" xr3:uid="{9D4F40B3-B530-42E1-92E9-86D5632CA191}" name="Quantity" dataDxfId="43"/>
</tableColumns>
<tableStyleInfo showFirstColumn="0" showLastColumn="0" showRowStripes="1" showColumnStripes="0"/>
</table>
I can see my two columns, and the ref attribute of the root tag as well as the autoFilter block's ref attribute. What I want to do is add a new row such that the area of the table would be A1:B3.
Inspecting an xlsx file containing a table in the docx4j webapp, it looks fairly straightforward.
It generates code for the contents of the table part like:
CTTable table = smlObjectFactory.createCTTable();
JAXBElement<org.xlsx4j.sml.CTTable> tableWrapped = smlObjectFactory.createTable(table);
// Create object for autoFilter
CTAutoFilter autofilter = smlObjectFactory.createCTAutoFilter();
table.setAutoFilter(autofilter);
autofilter.setRef( "A2:B4");
// Create object for tableColumns
CTTableColumns tablecolumns = smlObjectFactory.createCTTableColumns();
table.setTableColumns(tablecolumns);
tablecolumns.setCount( new Long(2) );
// Create object for tableColumn
CTTableColumn tablecolumn = smlObjectFactory.createCTTableColumn();
tablecolumns.getTableColumn().add( tablecolumn);
tablecolumn.setTotalsRowFunction(org.xlsx4j.sml.STTotalsRowFunction.NONE);
tablecolumn.setName( "Column1");
tablecolumn.setId( 1 );
// Create object for tableColumn
CTTableColumn tablecolumn2 = smlObjectFactory.createCTTableColumn();
tablecolumns.getTableColumn().add( tablecolumn2);
tablecolumn2.setTotalsRowFunction(org.xlsx4j.sml.STTotalsRowFunction.NONE);
tablecolumn2.setName( "Column2");
tablecolumn2.setId( 2 );
// Create object for tableStyleInfo
CTTableStyleInfo tablestyleinfo = smlObjectFactory.createCTTableStyleInfo();
table.setTableStyleInfo(tablestyleinfo);
tablestyleinfo.setName( "TableStyleMedium2");
table.setTableType(org.xlsx4j.sml.STTableType.WORKSHEET);
table.setHeaderRowCount( new Long(1) );
table.setTotalsRowCount( new Long(0) );
table.setName( "Table1");
table.setId( 1 );
table.setRef( "A2:B4");
table.setDisplayName( "Table1");
Once you know the row quantity, you can see you need to write it in 2 places, using setRef and autofilter.setRef
I need to display some content in a tabular form dynamically in Java. The content includes data that is fetched from an API in JSON format. At the end of each row I need to display a checkbox as well. The number of rows is dynamic and the columns are fixed. How do I do this?
The table will have the following columns:
Index
Username
Upload date
Percentage
Matched results
[Checkbox]
Finally found the answer to this problem. We can dynamically create a table using the class DefaultTableModel and the Java swing class JTable.
model = new DefaultTableModel();
Set the columns
model.setColumnIdentifiers(new Object[]{"Index","User","Reg no.","Match"});
jTable1.setModel(model);
jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
jTable1.setFillsViewportHeight(true);
jScrollPane1.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jScrollPane1.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
Add the rows from the JSON response
for(int i=0;i< jsonArr.size();i++)
{
str = jsonArr.get(i).toString();
jsonObj = (JSONObject)parser.parse(str);
String sub = jsonObj.get("subid").toString();
String uname = jsonObj.get("username").toString();
String regno = jsonObj.get("regno").toString();
String percent = jsonObj.get("percent").toString();
model.addRow(new Object[]{sub,uname,regno,percent});
}
I have recently tried to code a small java file which will insert a row into an already existing table in a .odt document. The table itself has 4 rows and 3 column, but I would like to implement a check which will expand that table if the content to be inserted is larger than 4. However, every time I try to get the table's rows, it returns a null pointer. I am not that familiar with UNO api, but as far as i read through the documentation, the class XColumnsAndRowRange should be used in this situation. My code is as follows:
XTextTablesSupplier xTablesSupplier = (XTextTablesSupplier) UnoRuntime.queryInterface(XTextTablesSupplier.class, xTextDocument);
XNameAccess xNamedTables = xTablesSupplier.getTextTables();
try {
Object table = xNamedTables.getByName(tblName);
XTextTable xTable = (XTextTable) UnoRuntime.queryInterface(XTextTable.class, table);
XCellRange xCellRange = (XCellRange) UnoRuntime.queryInterface(XCellRange.class, table);
if(flag){
XColumnRowRange xCollumnAndRowRange =(XColumnRowRange)
UnoRuntime.queryInterface(XColumnRowRange.class, xCellRange);
XTableRows rows = xCollumnAndRowRange.getRows();
System.out.println("Testing if this works");
rows.insertByIndex(4, size-4);
}
I am not sure if I am missing something here or if I should be using a different function.
As Lyrl suggested, this works:
XTableRows rows = xTable.getRows();
Apparently XColumnRowRange is only used for spreadsheets.
Note: With Basic or Python you would not have this problem, because those languages do not need queryInterface. The code would simply be:
table = tables.getByName(tblName)
rows = table.getRows()
Hi I can write a table into a word document as the code below... I wonder how do you add table cell background colour?
IDocument myDoc = new Document2004();
//myDoc.addEle(BreakLine.times(1).create());
Table tbl = new Table();
tbl.addTableEle(TableEle.TH, "Jira Number", "Description","asd","ee");
myDoc.addEle(tbl);
yes you can.
Try to instantiate a TableV2 rather than Table.
take a look at the example:
http://java2word.blogspot.com.au/2012/02/huge-ms-word-documents-with-java2word.html
you'll have to do something like this "withStyle().bgColor("00FFFF")" in your fluent interface code.
cheers
Leonardo Correa
I am writing a Java application that retrieves and presents data from an HBase database.
When writing the Get method for retrieving a row, I would like to get all the data for that row, but exclude the value for a particular column family (the "big" column family). Note: I need to retrieve the column names (qualifiers?) in that family because they contain valuable information.
Is it possible to write a Filter for that?
I have two solutions. The first one does not work and the second one is quite slow.
First solution (using a composite filter):
HTable table = getTable();
Get get = new Get(row);
FilterList filter = new FilterList(FilterList.Operator.MUST_PASS_ONE);
FilterList subFilterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
subFilterList.addFilter(new KeyOnlyFilter());
subFilterList.addFilter(new FamilyFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("big"))));
filter.addFilter(subFilterList);
filter.addFilter(new FamilyFilter(CompareOp.NOT_EQUAL, new BinaryComparator(Bytes.toBytes("big"))));
get.setFilter(filter);
retrieveAndUseResult(table, get);
This solution works neither conceptually nor in practise - but perhaps I am on the right track using a composite FilterList?
Second solution (using two gets):
HTable table = getTable();
Get get = new Get(row);
// exclude the entire "big" column family
get.setFilter(new FamilyFilter(CompareOp.NOT_EQUAL, new BinaryComparator(Bytes.toBytes("big"))));
retrieveAndUseResult(table, get);
Get get2 = new Get(row);
// include the "big" column family, but only retrieve the key
FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
filterList.addFilter(new KeyOnlyFilter());
filterList.addFilter(new FamilyFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("big"))));
get2.setFilter(filterList);
retrieveAndUseResult(table, get2);
This works, but I would favor having to do only one get.
I ended up using a variant of the second solution - using two gets. But I used a batch get list to speed it up.
The code:
HTable table = getTable();
Get get = new Get(row);
// exclude the entire "big" column family
get.setFilter(new FamilyFilter(CompareOp.NOT_EQUAL, new BinaryComparator(Bytes.toBytes("big"))));
Get get2 = new Get(row);
// include the "big" column family, but only retrieve the key
FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
filterList.addFilter(new KeyOnlyFilter());
filterList.addFilter(new FamilyFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("big"))));
get2.setFilter(filterList);
List<Get> getList = new ArrayList<Get>();
getList.add(get);
getList.add(get2);
retrieveAndUseResults(table, getList);