Just some common things people seem to be asking about.

How do I make a JTextArea that does word wrapping?

import javax.swing as swing

import java.awt as awt

frame = swing.JFrame("test1")

frame.contentPane.layout = awt.BorderLayout()

textArea = swing.JTextArea("This is obviously the sort of long thing you'd really like to have wrap")

textArea.preferredSize = (30,30)

frame.contentPane.add("Center",textArea);

textArea.lineWrap = True

textArea.wrapStyleWord = True

frame.pack()

frame.show()

How do I close a window?

So if you want to close a window but not exit the whole application, the thing to actually do is set the windows visible parameter to false.  This might seem non-intuitive if you’re really done with the window and never need it again, but making a window invisible is as close as you need to get to gone forever.

import javax.swing as swing

window = swing.JFrame("test1")

button = swing.JButton("show other window")

window.contentPane.add(button)

window2 = swing.JFrame("test2")

button2 = swing.JButton("hide this winow")

window2.contentPane.add(button2)

def callbackFunction(event):

   window2.pack()

   window2.show()

def hideWindow2(event):

   window2.visible = False

button.actionPerformed = callbackFunction

button2.actionPerformed = hideWindow2

window.pack()

window.show()

How do I display a picture?

It’s not obvious, but the easiest way to get an image to appear is to create an ImageIcon and then put it in a JLabel:

import javax.swing as swing

import java.awt as awt

frame = swing.JFrame("test1")

icon = swing.ImageIcon("keith.gif")

label = swing.JLabel(icon)

frame.contentPane.add(label)

frame.pack()

frame.show()

There is a slight challenge though: where is the program going to look for that image file “keith.gif”?  The answer is that it looks normally in the same directory that my sourcefiles are in (for me it’s C:\Users\Mike\workspace\TestJython\src but you will be different).  I could solve this by using an absolute path (eg. icon = swing.ImageIcon("C:\Users\Mike\Downloads\keith.gif"))  but this will cause problems when you run on someone else’s computer... the images aren't guaranteed to be in the same place on someone else's computer.  A good idea is to create a folder called images and store all of the images you will use there so you know where all the images you need are located when you need to submit.  Put that folder in the same folder where your gui.py file is saved.  Then you can refer to all your images as "images\YOUR_IMAGE.png".  Don't forget to send this folder in with your gui.py file

Main takeaway: if you do use images, be sure to check how your code works when invoked on the command line to be sure that paths work as you expect.

How do I have one handler that responds to several events in different ways?

So if you’re dynamically creating things like buttons or other UI elements, it can get a bit problematic.  Say each button corresponds to a different user.  Then you want to have a handler that clicks on the button, figures out what user the button corresponds to, then do something about that user.  Initially you might think “Ok somehow I need my actionPerformed method take two parameters: the event and the username.”  But there’s no way to actually do that...so you need another way.

This is a problem with many possible solutions.  You could, for example, create a set of objects that keep track of the username and then an instance variable then have button passed a method in that individual object.  That is potentially a very nice solution, but maybe a little fancier than you want to deal with.  There’s many others too.

Here’s a way to do this using Dictionaries.  Basically we have a global (could also be an instance in some big class) that associates the individual buttons with the “extra data” you would like to pass.  Then to get the data you just look it up in the Dictionary.  

import javax.swing as swing

import java.awt as awt

frame = swing.JFrame("test1")

frame.contentPane.layout = awt.FlowLayout()

buttonToId = {}

def commonCallback(event):

    button = event.source

    print "Button id is " + str(buttonToId[button])

for id in range(5):

    button = swing.JButton("Button")

    frame.contentPane.add(button)

    #in this case the data we store is an id but it could be an object or other cool structure

    buttonToId[button] = id

    button.actionPerformed = commonCallback

frame.pack()

frame.show()