entries between labels dynamically

Jason Orendorff jason at jorendorff.com
Mon Feb 18 03:19:04 EST 2002


Artur Skura wrote:
> I have a specific problem: I have to create dialogs on the fly, and
> in those dialog text together with text entries.

The code below works in Jython.  Not to say that Java Swing is
necessarily the way to go here, of course; just another option.

## Jason Orendorff    http://www.jorendorff.com/


# jythonwocky.py - Text fields between labels, dynamically

from java.awt import *
from javax.swing import *
import re

text = """'Twas _____, and the _____ _____
Did _____ and _____ in the _____.
All _____ were the _____
And the _____ _____.
"""

frame = JFrame("Blanks", size=(500, 300))
mainpanel = Box.createVerticalBox()

for line in text.split('\n'):
    mainpanel.add(Box.createVerticalStrut(6))
    parts = re.split(r'(_+)', line)
    hb = Box.createHorizontalBox()
    for part in parts:
        if part:
            if part.replace('_', '') == '':
                hb.add(JTextField(8, preferredSize=(100, 20),
                                  maximumSize=(100, 20)))
            else:
                hb.add(JLabel(part))
    hb.add(Box.createHorizontalGlue())
    mainpanel.add(hb)

mainpanel.add(Box.createVerticalGlue())
frame.contentPane.layout = BorderLayout()
frame.contentPane.add(mainpanel, BorderLayout.CENTER)
frame.visible = 1







More information about the Python-list mailing list