How to reset a table

57 Views Asked by At

It's possible to reset a table scala swing or remove it from the container after clicking on a button ? I've tried to create a val with that table but I have always a new table stacked under the old

Here is the code :

// here is the most crucial part when the user click on the button, it will append a new table but if we want to start again, it will append bottom of the old one and I want here a kind of reset or removing of table
  contents = new BoxPanel(Orientation.Vertical) {
    contents += new Label("Hello, you're welcome")
    contents += Button("Query") {
      val query: ScrollPane = new ScrollPane(changeCountry())
      contents -= query

      Try {
        contents += query
      }.getOrElse(Dialog.showMessage(contents.head, "Incorrect input ! This seems that input isn't in that list, write a different code or country"))

    }
// this part will ask to the user to write text to the input to display the table in function of the parameter of my function
  def changeCountry(): Table = {
    val text = Dialog.showInput(parent = contents.head, message = "Write a code of a country or a country", initial = "test")
    text match {
      case Some(s) => airportRunwayByCountry(s)
    }
  }
// this below part creates the table 
  def airportRunwayByCountry(code : String): Table = {
    val headers = Seq("Airport","Runway linked")
    val rowData = Functions.findAirportAndRunwayByCountry(code).map(x => x.productIterator.toArray).toArray
    val tableAirportRunway = new Table(rowData,headers)
    tableAirportRunway}


}
1

There are 1 best solutions below

0
On

Solved with method "remove" of containers

Here is the code :

      Try {
        if(contents.length == 3 \\ number of items in my Box) {
        \\ at this moment, only add the table because none other table exists
          contents += new ScrollPane(changeCountry()) 
        }
        else {
          contents -= contents.remove(3) \\get the id of the old table and remove it at this position
          contents += new ScrollPane(changeCountry()) \\ at this moment, this content will have the id n°2, and the loop can start over without errors
        }