0

I'm currently learning scala, and making an encryption program with a basic scala swing UI. I added 2 swing buttons which text is held by 2 var.

The code looks like this :

  var encText = "Encrypt"
  var decText = "Decrypt"

  def top = new MainFrame {
    title = "Data Guardian"
    minimumSize = new Dimension(500, 200)

    contents = new GridPanel(2, 2) {
      hGap = 3; vGap = 3
      contents += new Button {
        text = encText
        reactions += {
          case ButtonClicked(_) => Main.startEnc
        }
      }
      contents += new Button {
        text = decText
        reactions += {
          case ButtonClicked(_) => Main.startDec
        }
      }
    }
    size = new Dimension(150, 40)
  }

Those "text" var will be changed often during the encryption/decryption process by various methods, but when they do change, the text displayed on the buttons doesn't.

I'd like to know a way to make the displayed text of the buttons automatically change when the var that holds that text changes.

Thanks a lot for your insight :)

Viria
  • 47
  • 5

1 Answers1

1

Make the strings private and write getters/setters that change the button text as a side-effect.

You'll need to give the buttons names, rather than having anonymous instances as you do above.

Luigi Plinge
  • 50,650
  • 20
  • 113
  • 180