[Solved] Reading values from TextFields in Java

Discussions about using 3rd party extension with OpenOffice.org

[Solved] Reading values from TextFields in Java

Postby Tailor » Tue Dec 22, 2015 1:51 pm

Hello again!

I'm currently programming an extension for AOO 4.1.2 on Windows 7 in Java.
As I found that many of you post answers in StarBasic (or Basic) to Java related questions I would like to state that I HAVE TO program in Java and that an only-macro-based-solution is not allowed (in my company).
So please only answer if you can avoid Basic-Code ;) I would greatly appreciate it!

Now my problem:
I created an OptionPage for my extension which functions fine (FINALLY). There are 3 TextFields and a Button in the OptionDialog. When the Button is pressed, the values from the TextFields should be given to another method in Java.

I already tried this approach in the Action Listener Implementation for the Button (in the method "actionPerformed"):
Code: Select all   Expand viewCollapse view
Object textObject = _xControlCont.getControl("TextField1");
XTextField text = (XTextField) UnoRuntime.queryInterface(XTextField.class, textObject);

But the XTextField yields only "null" when converted. The object I retrieved from the controller seems fine as far as I can tell (it is not null at least). And the control "TextField1" is absolutely correct, that's what the first TextField is named.
I'll also add that I tried this conversion for a label with the interface "XFixedText" and it functions totally fine and behaves normally.

Do I have to use another interface? Have I forgotten something? Is the object not the right conversion-parameter?
I'm also fairly new to Java and the UNO-API confuses the living hell out of me...

If you have any idea on how to handle the object I'd really appreciate any input...thanks in advance :)

EDIT: I also want to add that I based my Java code loosely on this tutorial: https://wiki.openoffice.org/wiki/Documentation/DevGuide/Basic/Creating_Dialogs_at_Runtime
Last edited by Tailor on Tue Dec 22, 2015 5:04 pm, edited 1 time in total.
OpenOffice 4.1.2 on Windows 7
Posts: 9
Joined: Mon Nov 16, 2015 12:34 pm

Re: Reading values from TextFields in Java

Postby MTP » Tue Dec 22, 2015 4:32 pm

I believe your second line of code is returning an object. The first line returns an object wrapper or something (my understanding of Java is very limited). Java is very awkward IMO compared to Python or Basic in that it requires the .queryInterface to return an object where in Python or Basic the object would have been returned by the first line of code.

You'll need to add a third (or more) line to query the object's text property.

In constructing the correct syntax, you would probably find useful an object inspection tool such as XRay (http://bernard.marcelly.perso.sfr.fr/index2.html) or MRI (http://extensions.services.openoffice.org/en/project/mri-uno-object-inspection-tool, [Tutorial] Introduction into object inspection with MRI) that can look at an object you have obtained and report back to you all the object's available methods and properties.
OpenOffice 4.1.1 on Windows 10, HSQLDB 1.8 split database
User avatar
Posts: 1620
Joined: Mon Sep 10, 2012 7:31 pm
Location: Midwest USA

Re: Reading values from TextFields in Java

Postby Tailor » Tue Dec 22, 2015 5:03 pm

Thank you for your reply! :) The tool looks really useful, I'll look into it after my christmas vacation!

I found the solution to my problem. The interface was as I suspected the wrong one. I need to use the "XTextComponent"-interface.

There really should be a better documentation for the API... This is getting ridiculous.

My code now looks like this and functions properly:
Code: Select all   Expand viewCollapse view
XControl textControl = _xControlCont.getControl("TextField1");
XTextComponent textComp = (XTextComponent) UnoRuntime.queryInterface(XTextComponent.class, textControl);
String text = textComp.getText();

I'll add the solved-tag right away :)
OpenOffice 4.1.2 on Windows 7
Posts: 9
Joined: Mon Nov 16, 2015 12:34 pm

Return to Extensions

Who is online

Users browsing this forum: No registered users and 1 guest