Reputation: 1263
I've been trying to create an Option dialog that isn't limited to two or three choices (Option.YesNo or Option.YesNoCancel), but I've been unable to find a way to use anything but these built-in options. Specifically, the following refuses to accept anything that I can put for optionType:
object Choices extends Enumeration {
type Choice = Value
val red, yellow, green, blue = Value
}
val options = List("Red", "Yellow", "Green", "Blue")
label.text = showOptions(null,
"What is your favorite color?",
"Color preference",
optionType = Choices.Value,
entries = options,
initial = 2) match {
case Choice.red => "Red"
case Choice.yellow => "Yellow"
case Choice.green => "Green"
case Choice.blue => "Blue"
case _ => "Some other color"
}
Upvotes: 2
Views: 810
Reputation: 67330
Yes, it's one of the many design bugs in Scala-Swing. You can write your own showOptions
method:
import swing._
import Swing._
import javax.swing.{UIManager, JOptionPane, Icon}
def showOptions[A <: Enumeration](
parent: Component = null,
message: Any,
title: String = UIManager.getString("OptionPane.titleText"),
messageType: Dialog.Message.Value = Dialog.Message.Question,
icon: Icon = EmptyIcon,
entries: A,
initial: A#Value): Option[A#Value] = {
val r = JOptionPane.showOptionDialog(
if (parent == null) null else parent.peer, message, title, 0,
messageType.id, Swing.wrapIcon(icon),
entries.values.toArray[AnyRef], initial)
if (r < 0) None else Some(entries(r))
}
val res = showOptions(message = "Color", entries = Choices, initial = Choices.green)
If you want to pass in strings instead, change to entries: Seq[Any]
, initial: Int
, use entries(initial)
in the call, and just return r
of Int
.
Upvotes: 1
Reputation: 33093
optionType
isn't for passing in a Scala type, and actually I'm not sure what its purpose is in this API. But you can just set optionType = Options.YesNoCancel
, and that should work, I think.
Upvotes: 0