Conditional enabling or visibility of TextAreaWidget

6 views
Skip to first unread message

Helios De Rosario

unread,
Dec 18, 2011, 7:24:24 PM12/18/11
to Deducer
Hi,

I'm trying to write a dialog with a text area for the user to set the
value of a variable, that only makes sense when a certain option is
selected. Therefore, I'd want that text area (defined as a
TextAreaWidget object) to remain disabled when the option is *not*
selected.

I first tried to use the method "setEnabled" with a RDialogMonitor,
like in the example for the "Run" button described in the Deducer
wiki:
http://www.deducer.org/pmwiki/index.php?n=Main.Development#tr

However, although that method is defined for the TextAreaWidget class,
it does not seem to have any visible effect (nor do the methods
"disable" or "enable").

Then I have tried with the method "setVisible" (or the methods
"show"/"hide") as an alternative solution, and this way I can
successfully show the text area when the associated option is
selected, but it apparently fails to hide it when it is unselected.

The next lines are a simple example to reproduce this problem:

dialog <- new(SimpleRDialog)
dialog$setSize(400L, 200L)
selection <- new(ButtonGroupWidget,c("No", "Yes"))
addComponent(dialog, selection, 20, 450, 600, 20)
textarea <- new(TextAreaWidget)
textarea$hide()
addComponent(dialog,textarea, 20, 950, 200, 550)
monitor <- new(RDialogMonitor, dialog, 500L)
monitorFunction <- function(){
if (selection$getModel()=="Yes") textarea$show() else textarea$hide()
}
monitor$setFunction(toJava(monitorFunction))
monitor$start()

Now I run the dialog:
> dialog$run()
and I can see the simple dialog with the text area hidden (the default
behaviour).

Now, if I choose the option "Yes" in the radio buttons, the text area
shows up, as expected, but if I then go back to "No", the text area
remains.

I have noticed that the properties of the text area object do change
as expected, although the visible behaviour is not coherent with these
changes. I mean, if I check the visibility of the text area with
> textarea$isVisible()
the result is TRUE or FALSE depending on the selected radio button,
exactly as I wanted. But sometimes the text area is still visible in
the dialog, although the "isVisible" method actually returns FALSE.

Therefore, maybe my problem is just a graphical issue, i.e. the aspect
of the dialog is not automatically "refreshed" when the text area is
made invisible. In fact, if I close the dialog and re-run it to
display it again, the text area shows up or dissapears again,
according to its expected state.

So my question is: what can I do to hide the text area, not only
internally but also in the graphic representation of the dialog? An
effective workaround would be hiding and then showing again the whole
dialog when the visibility changes (I have tried it and it does work).
But I hope there is a smarter way to do it.

Or perhaps it would be possible to disable it without hiding it
entirely, as I initially wanted to do?

Thanks in advance,
Helios

Helios De Rosario

unread,
Dec 19, 2011, 11:04:55 AM12/19/11
to Deducer
Ok. I solved it: just repaint the parent dialog after hiding the
widget. So, the monitor function should be:

monitorFunction <- function(){
if ((selection$getModel()=="Yes") && !textarea$isVisible())
textarea$show()
if ((selection$getModel()=="No") && textarea$isVisible()){
textarea$hide()
textarea$getTopLevelAncestor()$repaint()
}

I still would like to disable the text area, without making it
disappear, but at least this seems to work for my purposes.

Helios

On Dec 19, 1:24 am, Helios De Rosario <helios.derosa...@gmail.com>
wrote:

Ian Fellows

unread,
Dec 19, 2011, 1:43:48 PM12/19/11
to ded...@googlegroups.com
Each widget is a JPanel containing Swing components. for example, the
text area widget contains a text area and a label. Disabling the panel
does not have much of an effect. You need to disable the actual
JTextArea with textarea$getTextArea()$setEnabled(FALSE). Next, I would
strongly suggest (unless there is good reason) that you use an
RActionListener instead of an RDialogMonitor. The action listener is
called only when a button is pressed, whereas the monitor continuously
runs at regular intervals, and you have to manually start and stop it.
Also, if you are just putting in one line of text, consider using a
TextFieldWidget.

Ian

Helios De Rosario

unread,
Dec 19, 2011, 5:33:08 PM12/19/11
to Deducer
It works perfectly! Lots of thanks, Ian.

Helios

Reply all
Reply to author
Forward
0 new messages