Swing Component Alignment

42 views
Skip to first unread message

bertzzie

unread,
Nov 15, 2012, 1:55:36 AM11/15/12
to scala...@googlegroups.com
Hi,
I'm learning how to build GUI using the standard Swing widget on Scala. Here's what I want to do:



I've had success in **creating** the components, but failed on **aligning** it. All of my components are aligned centered, not like what I want (Button1 on left, table on center, and button2 on right). I can't find much information about Scala's Swing either. Most of the search result is about Java's (which I don't know anything about). What should I do to force the alignment?

Here's the code:

    contents += new BoxPanel(Orientation.Vertical) {
        contents += new Button("Button 1")
        contents += new Table(3, 3)
        contents += new Button("Button 2")
    }

The result:



Thanks before.

(I've cross posted from Stackoverflow (original thread) because there seems to be no answer there. Please tell me if this is not allowed. Thanks before).

Stefan Hoeck

unread,
Nov 15, 2012, 2:28:37 AM11/15/12
to scala...@googlegroups.com
BoxPanel might be not flexibel enough for what you want. If you want flexibility, go for GridBagPanel. It uses a GridBagLayout (documented in Java's Swing documentation). It's more complex to use than a BoxLayout, but it gives you (almost) complete freedom about how your components are aligned. See also:

http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

By the way, if you just want to align your components in a horizontal line (Button1 on the left, table in the middle and Button2 on the right), you might try
new BoxPanel(Orientation.Horizontal)
Reply all
Reply to author
Forward
0 new messages