This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Is there a Scala swing JTree ?

4 replies
sanjay_dasgupta
Joined: 2009-12-08,
User offline. Last seen 1 year 10 weeks ago.
Isn't there a way of creating and using a JTree via scala.swing? The docs don't seem to list a JTree equivalent.
Thanks for any help.
- Sanjay
Martin S. Weber
Joined: 2008-12-23,
User offline. Last seen 42 years 45 weeks ago.
Re: Is there a Scala swing JTree ?

On 02/12/10 10:27, Sanjay Dasgupta wrote:
> Isn't there a way of creating and using a JTree via scala.swing? The docs don't seem to list a JTree equivalent.
>

I use the JTree directly, wrapped in a Component, e.g.
new ScrollPane {
contents = new Component {
override lazy val peer = new JTree(new DefaultTreeModel(root)) {
addTreeSelectionListener(new TreeSelectionListener {
def valueChanged(e: TreeSelectionEvent) {
//...
}
})
addMouseListener(new java.awt.event.MouseAdapter {
override def mousePressed(e: JMouseEvent) {
//...
}
override def mouseReleased(e: JMouseEvent) {
//...
}
})
}
}
// somewhere also revalidate/repaint

i.e. it's easy enough wrapped to survive not having it wrapped already

Hth,

-Martin

Ken Scambler
Joined: 2009-11-07,
User offline. Last seen 42 years 45 weeks ago.
Re: Is there a Scala swing JTree ?

Certainly one can get by without a wrapped version, but I disagree that it's
not worth the effort to wrap it, it's a conspicuous absence in the Scala
Swing API.

The following common usages are tedious, verbose, non-type safe, and/or
require unsafe null usage:
- Creating a custom tree model, backed by your own user objects -- the Scala
Swing way would be to have a standard typesafe Map behind it
- Events - there are heaps of events created by trees --
TreeWillExpandListeners, TreeSelection, etc -- Using the Reactor/Publisher
PartialFunction model would make this code far more readable and concise.
- Editable components -- This is done with implicit values elsewhere in
scala.swing, and should be here too.
- Custom renderers -- Can't remember how Scala deals with these, but this is
always fiddly in Java Swing too.

Bottom line is, JTrees are a massive pain to use in Java, for no
particularly good reason. A Scala wrapper would be a massive boon for Scala
Swing users.
-Ken

Martin S. Weber-2 wrote:
>
> On 02/12/10 10:27, Sanjay Dasgupta wrote:
>> Isn't there a way of creating and using a JTree via scala.swing? The docs
>> don't seem to list a JTree equivalent.
>>
>
> I use the JTree directly, wrapped in a Component, e.g.
> new ScrollPane {
> contents = new Component {
> override lazy val peer = new JTree(new DefaultTreeModel(root)) {
> addTreeSelectionListener(new TreeSelectionListener {
> def valueChanged(e: TreeSelectionEvent) {
> //...
> }
> })
> addMouseListener(new java.awt.event.MouseAdapter {
> override def mousePressed(e: JMouseEvent) {
> //...
> }
> override def mouseReleased(e: JMouseEvent) {
> //...
> }
> })
> }
> }
> // somewhere also revalidate/repaint
>
> i.e. it's easy enough wrapped to survive not having it wrapped already
>
> Hth,
>
> -Martin
>
>

imaier
Joined: 2008-07-01,
User offline. Last seen 23 weeks 3 days ago.
Re: Is there a Scala swing JTree ?

Thanks, Ken.

That's a fair analysis of what needs to be done. Moreover, one could
experiment with swinglabs JXTreeTable for an ultimate list/table/tree
view. I don't know whether that's the silver bullet though, as I don't
have enough experience with it.

Would you like to work on a wrapper?

Ingo

On 2/13/10 3:22 AM, Ken Scambler wrote:
>
> Certainly one can get by without a wrapped version, but I disagree that it's
> not worth the effort to wrap it, it's a conspicuous absence in the Scala
> Swing API.
>
> The following common usages are tedious, verbose, non-type safe, and/or
> require unsafe null usage:
> - Creating a custom tree model, backed by your own user objects -- the Scala
> Swing way would be to have a standard typesafe Map behind it
> - Events - there are heaps of events created by trees --
> TreeWillExpandListeners, TreeSelection, etc -- Using the Reactor/Publisher
> PartialFunction model would make this code far more readable and concise.
> - Editable components -- This is done with implicit values elsewhere in
> scala.swing, and should be here too.
> - Custom renderers -- Can't remember how Scala deals with these, but this is
> always fiddly in Java Swing too.
>
> Bottom line is, JTrees are a massive pain to use in Java, for no
> particularly good reason. A Scala wrapper would be a massive boon for Scala
> Swing users.
> -Ken
>
>
> Martin S. Weber-2 wrote:
>>
>> On 02/12/10 10:27, Sanjay Dasgupta wrote:
>>> Isn't there a way of creating and using a JTree via scala.swing? The docs
>>> don't seem to list a JTree equivalent.
>>>
>>
>> I use the JTree directly, wrapped in a Component, e.g.
>> new ScrollPane {
>> contents = new Component {
>> override lazy val peer = new JTree(new DefaultTreeModel(root)) {
>> addTreeSelectionListener(new TreeSelectionListener {
>> def valueChanged(e: TreeSelectionEvent) {
>> //...
>> }
>> })
>> addMouseListener(new java.awt.event.MouseAdapter {
>> override def mousePressed(e: JMouseEvent) {
>> //...
>> }
>> override def mouseReleased(e: JMouseEvent) {
>> //...
>> }
>> })
>> }
>> }
>> // somewhere also revalidate/repaint
>>
>> i.e. it's easy enough wrapped to survive not having it wrapped already
>>
>> Hth,
>>
>> -Martin
>>
>>
>

Ken Scambler
Joined: 2009-11-07,
User offline. Last seen 42 years 45 weeks ago.
Re: Is there a Scala swing JTree ?

Hi Ingo,
Sure, I'll give it a go, thanks. Between the scala.swing source and your
SID there should be enough of a basis for a sound design. I haven't used
the JXTreeTable either -- it sounds like it might be worth a look.
Ken

Ingo Maier-3 wrote:
>
> Thanks, Ken.
>
> That's a fair analysis of what needs to be done. Moreover, one could
> experiment with swinglabs JXTreeTable for an ultimate list/table/tree
> view. I don't know whether that's the silver bullet though, as I don't
> have enough experience with it.
>
> Would you like to work on a wrapper?
>
> Ingo
>
> On 2/13/10 3:22 AM, Ken Scambler wrote:
>>
>> Certainly one can get by without a wrapped version, but I disagree that
>> it's
>> not worth the effort to wrap it, it's a conspicuous absence in the Scala
>> Swing API.
>>
>> The following common usages are tedious, verbose, non-type safe, and/or
>> require unsafe null usage:
>> - Creating a custom tree model, backed by your own user objects -- the
>> Scala
>> Swing way would be to have a standard typesafe Map behind it
>> - Events - there are heaps of events created by trees --
>> TreeWillExpandListeners, TreeSelection, etc -- Using the
>> Reactor/Publisher
>> PartialFunction model would make this code far more readable and concise.
>> - Editable components -- This is done with implicit values elsewhere in
>> scala.swing, and should be here too.
>> - Custom renderers -- Can't remember how Scala deals with these, but this
>> is
>> always fiddly in Java Swing too.
>>
>> Bottom line is, JTrees are a massive pain to use in Java, for no
>> particularly good reason. A Scala wrapper would be a massive boon for
>> Scala
>> Swing users.
>> -Ken
>>
>>
>> Martin S. Weber-2 wrote:
>>>
>>> On 02/12/10 10:27, Sanjay Dasgupta wrote:
>>>> Isn't there a way of creating and using a JTree via scala.swing? The
>>>> docs
>>>> don't seem to list a JTree equivalent.
>>>>
>>>
>>> I use the JTree directly, wrapped in a Component, e.g.
>>> new ScrollPane {
>>> contents = new Component {
>>> override lazy val peer = new JTree(new DefaultTreeModel(root)) {
>>> addTreeSelectionListener(new TreeSelectionListener {
>>> def valueChanged(e: TreeSelectionEvent) {
>>> //...
>>> }
>>> })
>>> addMouseListener(new java.awt.event.MouseAdapter {
>>> override def mousePressed(e: JMouseEvent) {
>>> //...
>>> }
>>> override def mouseReleased(e: JMouseEvent) {
>>> //...
>>> }
>>> })
>>> }
>>> }
>>> // somewhere also revalidate/repaint
>>>
>>> i.e. it's easy enough wrapped to survive not having it wrapped already
>>>
>>> Hth,
>>>
>>> -Martin
>>>
>>>
>>
>
>
>

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland