Valerin
Valerin

Reputation: 475

scala - calling a created new button

I have this:

def top = new MainFrame {
  contents = new GridBagPanel {
  def constraints(x: Int, y: Int, .... ) {...}

  add(btn("A"), constraints(0, 0))

  private def btn(name: String): Button = new Button() {
    text = name
  }
}

and it doesn't show the button's name, but when I write in this way it is fine:

def top = new MainFrame {
  contents = new GridBagPanel {
    def constraints(x: Int, y: Int, .... ) {...}

    add(new Button("A"), constraints(0, 0))
  }
}

I really don't get why?

Upvotes: 1

Views: 54

Answers (1)

Jon Kartago Lamida
Jon Kartago Lamida

Reputation: 854

It could work, just replace the declaration of btn method by not using "name" argument:

 private def btn(x: String): Button = new Button() {
   text = x
 }

The reason is, if you browse to the scala Button source code and then its superclass Component, you can see there is a name variable declared in Component class (See line 81)

def name: String = peer.getName

So that in your first code, instead of getting value from argument that you pass, it will get "name" from above peer.getName, which is null and at the end make your button text is not updated correctly.

Upvotes: 2

Related Questions