Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request for a scala.swing.CardPanel (uses javax.swing.CardLayout) #135

Open
scabug opened this issue Oct 18, 2010 · 2 comments
Open

Request for a scala.swing.CardPanel (uses javax.swing.CardLayout) #135

scabug opened this issue Oct 18, 2010 · 2 comments

Comments

@scabug
Copy link

scabug commented Oct 18, 2010

Here is a simple implementation:

import swing.{Component, LayoutContainer, Panel}
import java.awt.CardLayout

class CardPanel extends Panel with LayoutContainer {
  type Constraints = String
  def layoutManager = peer.getLayout.asInstanceOf[CardLayout]
  override lazy val peer = new javax.swing.JPanel(new CardLayout) with SuperMixin

  private var cards : Map[String, Component] = Map.empty

  protected def areValid(c: Constraints) = (true, "")
  protected def add(comp: Component, l: Constraints) = {
    // we need to remove previous components with the same constraints as the new one,
    // otherwise the layout manager loses track of the old one
    cards.get(l).foreach { old => cards -= l; peer.remove(old.peer) }
    cards += (l -> comp)
    peer.add(comp.peer, l)
  }

  def show(l : Constraints) = layoutManager.show(peer, l)

  protected def constraintsFor(comp: Component) = cards.iterator.find { case (_, c) => c eq comp}.map(_._1).orNull
}
@scabug
Copy link
Author

scabug commented Oct 18, 2010

Imported From: https://issues.scala-lang.org/browse/SI-3933?orig=1
Reporter: @oxbowlakes

@scabug
Copy link
Author

scabug commented Mar 27, 2013

Samuel Halliday (fommil) said (edited on Mar 27, 2013 2:39:47 PM UTC):
hmm, this seems somewhat limited as an implementation.

Anytime I want to use a CardLayout I find myself wanting to use the following functions (much more than the arbitrary String lookup of the Components)

  • first
  • last
  • next
  • previous

and being able to access the current Component that is shown. This I do with the following helper method

  def current = getComponents.filter(_.isVisible).headOption

If these functions were made available to subclasses of CardPanel, then very clean code to replace the following would be possible

  addKeyListener(new KeyAdapter {
    override def keyPressed(e: KeyEvent) {
      e.getKeyCode match {
        case KeyEvent.VK_ENTER if current == Some(panel3) =>
          log.info("finished...")

        case KeyEvent.VK_ENTER =>
          cards.next(IntroductionView.this)

        case KeyEvent.VK_LEFT if current != Some(panel0) =>
          cards.previous(IntroductionView.this)

        case _ =>
      }
    }
  })

@scabug scabug closed this as completed Jul 17, 2015
@SethTisue SethTisue transferred this issue from scala/bug Nov 19, 2020
@scala scala deleted a comment from scabug Nov 19, 2020
@SethTisue SethTisue reopened this Nov 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants