1

I'm having a basic issue with alignment in a BoxPanel. When either of the TextAreas takes input, the other components in the panel, that aren't the width of the panel, shift along with the characters.

What is my mistake? Thanks!

blank TextArea

filled TextArea

val exitButton = new Button {
  text = "Exit"
  borderPainted = true
  enabled = true
}

val japaneseTranslation = new TextArea(5, 25)
val englishTranslation = new TextArea(5,25)

val translationPanel = new BoxPanel (Orientation.Vertical) {
  contents += new Label {
    text = "Translation"
    font = new Font("Ariel", java.awt.Font.PLAIN, 20)
    horizontalAlignment = Alignment.Center
  }

  contents += new Label {
    text = "Japanese"
    font = new Font("ariel", java.awt.Font.PLAIN, 10)
  }

  /*contents += new TextField ("Japanese") {                                                                                                                  
    editable = false                                                                                                                                          
  }*/
  contents += japaneseTranslation


  contents += new Label {
    text = "English"
    font = new Font("ariel", java.awt.Font.PLAIN, 10)
  }
  contents += englishTranslation

  contents += exitButton

}
Peter Becich
  • 989
  • 3
  • 14
  • 30
  • 2
    Assuming that `BoxPanel` is based on Swing's `BoxLayout` you may want to set `setAlignmentX` on both `japaneseTranslation` and `englishTranslation` to `Component.LEFT_ALIGNMENT`. Not sure what is the equivalent of this constant in Scala. – tenorsax Dec 09 '13 at 01:45
  • Did you test the solution provided below? Does that solve your problem? – Simon May 28 '14 at 08:31
  • 1
    @Aqua that was a helpful lead, thanks. `peer.setAlignment...` as in Simon's answer is the working equivalent in Scala, apparently. – Peter Becich May 29 '14 at 06:49

1 Answers1

1

Wrapping the TextArea components into a ScrollPane helps:

val exitButton = new Button {
  text = "Exit"
  borderPainted = true
  enabled = true
}

val japaneseTranslation = new TextArea(5, 25)
val englishTranslation = new TextArea(5,25)

val translationPanel = new BoxPanel (Orientation.Vertical) {
  contents += new Label {
    text = "Translation"
    font = new Font("Ariel", java.awt.Font.PLAIN, 20)
    horizontalAlignment = Alignment.Center
  }

  contents += new Label {
    text = "Japanese"
    font = new Font("ariel", java.awt.Font.PLAIN, 10)
  }

  /*contents += new TextField ("Japanese") {                                                                                                                  
    editable = false                                                                                                                                          
  }*/
  contents += new ScrollPane(japaneseTranslation)


  contents += new Label {
    text = "English"
    font = new Font("ariel", java.awt.Font.PLAIN, 10)
  }
  contents += new ScrollPane(englishTranslation)

  contents += exitButton

}

Another possibility is, as implied by Aqua in his comment, to set the alignmentX of your TextArea components:

val japaneseTranslation = new TextArea(5, 25) {
  peer.setAlignmentX(0)
}
val englishTranslation = new TextArea(5,25) {
  peer.setAlignmentX(0)
}

Please post a SSCCE next time.

Simon
  • 4,103
  • 7
  • 28
  • 53
  • Sorry for the delayed response. Both solutions fix the problem completely. Aqua's solution looked promising and I tried to implement it, but did not find `peer`. I will post a SSCCE next time. Thanks! – Peter Becich May 29 '14 at 06:46