| View previous topic :: View next topic |
| Author |
Message |
hol.sten Super User


Joined: 14 Nov 2004 Posts: 3533 Location: Hamburg, Germany
|
Posted: Sun Nov 14, 2004 2:49 pm Post subject: Access TableColumnSeparator from Java |
|
|
Hi!
I've got a problem: I want to insert a table with 4 columns into a text document and I want to change the column width seperatly. Is this possible via the OpenOffice Java API? I searched this forum with "TableColumnSeparator java" but didn't find anything. I found a thread with some StarBasic code. But I couldn't convert that to Java. I only managed to insert a new table with equally wide columns.
I have a second question concerning tables: Is it possible to add new rows into an already existing table via the Java API? I didn't find anything that offers me that possibility.
Any suggestions?
With kind regards
hol.sten |
|
| Back to top |
|
 |
Cybb20 Super User


Joined: 02 Mar 2004 Posts: 1569 Location: Frankfurt, Germany
|
Posted: Sun Nov 14, 2004 3:28 pm Post subject: |
|
|
To question 2:
The XTableRows interface provides the method insertByIndex(...), that should do it.
Christian _________________ - Knowledge is Power - |
|
| Back to top |
|
 |
DannyB Moderator


Joined: 02 Apr 2003 Posts: 3991 Location: Lawrence, Kansas, USA
|
Posted: Mon Nov 15, 2004 9:45 am Post subject: |
|
|
I assume that you saw this....
http://www.oooforum.org/forum/viewtopic.php?p=12205#12205
I'm translating from the Basic example.
This line....
| Code: | | oTblColSeps = oTable.TableColumnSeparators |
in Java would need to be several steps....
1. QueryInterface to get the XPropertySet interface
2. Call getPropertyValue() to get the TableColumnSeparators property.
What you get back is an Array of TableColumnSeparator.
So next, you need to do a Java typecast, to cast into an array of the structs.
So far, the entire code looks like...
| Code: | XPropertySet xTableProps = UnoRuntime.queryInterface( XPropertySet.class, oTable );
Object colSepsArray = xTableProps.getProperty( "TableColumnSeparators" );
TableColumnSeparators[] colSepsArray2 = (TableColumnSeparators[]) colSepsArray;
|
and that mess is just to accomplish what was one simple assignment statement in Basic.
Next, you adjust the columns, with code translated from these Basic lines in the above linked example in Basic....
| Code: | ' Change the positions of the two separators.
oTblColSeps( 0 ).Position = 2000
oTblColSeps( 1 ).Position = 8000
|
In your Java array, you would do something like...
| Code: | colSepsArray2[0].Position = 2000
colSepsArray2[1].Position = 8000
|
Then, as in Basic....
| Code: | | oTable.TableColumnSeparators = oTblColSeps |
you need to assign the Entire Array back to the TableColumnSeparators property. You would use the XPropertySet interface you already obtained, and call setPropertyValue() to assign the entire array back to the property, as in the Basic example.
Some links....
Insert Tables in Writer
http://www.oooforum.org/forum/viewtopic.php?t=3737
....and Change table cell colors
....and insert text into table cells
http://www.oooforum.org/forum/viewtopic.php?t=6437
Change width of columns in Writer Table
http://www.oooforum.org/forum/viewtopic.php?t=3299
Copying TextTable in Writer
http://www.oooforum.org/forum/viewtopic.php?t=10924 _________________ Want to make OOo Drawings like the colored flower design to the left? |
|
| Back to top |
|
 |
hol.sten Super User


Joined: 14 Nov 2004 Posts: 3533 Location: Hamburg, Germany
|
Posted: Mon Nov 15, 2004 1:18 pm Post subject: |
|
|
Hi Danny!
Right!
| DannyB wrote: | I'm translating from the Basic example.
...
What you get back is an Array of TableColumnSeparator. |
That was my problem! I always got ClassCastExceptions but I didn't found the cause. Now it works. Because your translation contains some minor errors, I post here an example from my Java IDE. Most of the code is based on code from this superb forum and from the Developer's Guide:
| Code: | private void insertTable(XComponent xComponent) {
try
{
// Get the text document
XTextDocument xTextDocument = (XTextDocument)UnoRuntime.queryInterface(XTextDocument.class, xComponent);
// Get the text
XText xText = xTextDocument.getText();
// Create a text cursor for inserting the new table
XTextCursor xTextCursor = xText.createTextCursor();
// Jump to the end of the text
xTextCursor.gotoEnd(false);
// Create a new text table from the document's factory
XMultiServiceFactory xWriterFactory = (XMultiServiceFactory)UnoRuntime.queryInterface(XMultiServiceFactory.class, xComponent);
XTextTable xTable = (XTextTable) UnoRuntime.queryInterface(XTextTable.class, xWriterFactory.createInstance("com.sun.star.text.TextTable"));
// Specify that we want the table to have 4 rows and 4 columns
xTable.initialize(4, 4);
// Insert the table into the document
xText.insertTextContent(xTextCursor, xTable, false);
// Get the property set of the text table
XPropertySet xTableProps = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xTable);
// Get the array(!) of table column seperators
TableColumnSeparator[] separators = (TableColumnSeparator[])TableProps.getPropertyValue("TableColumnSeparators");
// In this example the column seperators are now 2500, 5000 and 7500
// Now Change the table column seperators
separators[0].Position = 7000;
separators[1].Position = 8000;
separators[2].Position = 9000;
// Set the array of table column seperators
xTableProps.setPropertyValue("TableColumnSeparators", separators);
// Although it's a little stupid to increase the row number of the table now,
// we'll do it here for demonstration purpose
// Get the table rows
XTableRows tableRows = (XTableRows)UnoRuntime.queryInterface(XTableRows.class, xTable.getRows());
// Insert after the second row 4 new rows
tableRows.insertByIndex(2,4);
} catch (Exception e) {
// Do something!
}
}
|
| DannyB wrote: | | Some links.... |
Your link lists contains alway good suggestions. Thank you!
A BIG thank you also to Christian for his advice!
With kind regards
hol.sten |
|
| Back to top |
|
 |
ankush General User

Joined: 04 Aug 2008 Posts: 15 Location: INDIA, MUMBAI
|
Posted: Wed Aug 06, 2008 10:43 pm Post subject: |
|
|
Hello
I tried to create just table in the OOo writer actually with macro.
I used your reference code which is
try {
// Get the text document
XTextDocument xTextDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, xComponent);
// Get the text
XText xText = xTextDocument.getText();
// Create a text cursor for inserting the new table
XTextCursor xTextCursor = xText.createTextCursor();
// Jump to the end of the text
xTextCursor.gotoEnd(false);
// Create a new text table from the document's factory
XMultiServiceFactory xWriterFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, xComponent);
// Problem *** nullPointer Exception at following line***
XTextTable xTable = (XTextTable) UnoRuntime.queryInterface(XTextTable.class, xWriterFactory.createInstance("com.sun.star.text.XTextTable"));
// Specify that we want the table to have 4 rows and 4 columns
xTable.initialize(4, 4);
// Insert the table into the document
xText.insertTextContent(xTextCursor, xTable, false);
} catch (Exception e) {
XTextDocument xtextdocument = (XTextDocument) UnoRuntime.queryInterface(
XTextDocument.class, xSc.getDocument());
XText xText = xtextdocument.getText();
XTextRange xTextRange = xText.getEnd();
xTextRange.setString("Exception ....AnkusH 015 ::" + e + "\n");
xTextRange = xText.getEnd();
}
but I am getting "java.lang.NullPointerException" exactly at
XTextTable xTable = (XTextTable) UnoRuntime.queryInterface(XTextTable.class, xWriterFactory.createInstance("com.sun.star.text.TextTable"));
Can you please help me out to create a table through java macro in the OOo writer?
Thanks in advance
AnkusH |
|
| Back to top |
|
 |
hol.sten Super User


Joined: 14 Nov 2004 Posts: 3533 Location: Hamburg, Germany
|
Posted: Fri Aug 08, 2008 11:26 am Post subject: |
|
|
| ankush wrote: | | I tried to create just table in the OOo writer actually with macro. |
What did you do with the code from my posting above? A simple copy and paste would most likely have prevented your Exception.
My posting above shows this line:
XTextTable xTable = (XTextTable) UnoRuntime.queryInterface(XTextTable.class, xWriterFactory.createInstance("com.sun.star.text.TextTable"));
Your posting shows a slightly different line:
XTextTable xTable = (XTextTable) UnoRuntime.queryInterface(XTextTable.class, xWriterFactory.createInstance("com.sun.star.text.XTextTable"));
com.sun.star.text.TextTable is a service and can be used to create an instance. com.sun.star.text.XTextTable is an interface to manage tables. A slight but important difference.
| ankush wrote: | | I used your reference code which is |
No, you didn't  |
|
| Back to top |
|
 |
ankush General User

Joined: 04 Aug 2008 Posts: 15 Location: INDIA, MUMBAI
|
Posted: Sun Aug 10, 2008 10:48 pm Post subject: |
|
|
| hey Thanks :D |
|
| Back to top |
|
 |
ankush General User

Joined: 04 Aug 2008 Posts: 15 Location: INDIA, MUMBAI
|
Posted: Wed Aug 13, 2008 12:07 am Post subject: |
|
|
Hello
Do you know how to read text and table from the writer?
Thanks in advance
AnkusH |
|
| Back to top |
|
 |
|