Taking Swing without cafeine with Jython

TrustyTony

Here is experiment of how VM and Swing UI manage the sort test posted previously, instead of CPython and Tkinter. The timing is not so sweet, but at least no Java required!

K:\jython2.5.2>java -jar jython.jar swing_test.py
256000 words generated in 10.93 s
Sorted in 1.68 s
List prepared in 22.71 s
394 Views
About the Author

IT Pro doing Eng-Fin-Eng translations

import random
import string
import time

import java.lang as lang
import javax.swing as swing
import java.awt as awt

def exit(event):
    lang.System.exit(0)

t0 = time.clock()
word_max = 256000
names = [''.join(random.choice(string.ascii_uppercase)
                 for count in range(6))
         for word_count in range(word_max)]
t0 -= time.clock()
print('%i words generated in %.2f s' % (word_max, -t0))

t0 = time.clock()
names.sort()
t0 -= time.clock()
print('Sorted in %.2f s' % -t0)

t0 = time.clock()
win = swing.JFrame("Sort names", windowClosing=exit)
win.contentPane.layout = awt.FlowLayout()
namelist = awt.Choice()
for name in names:
    namelist.addItem(name)

win.add(namelist)
win.pack()
win.size=(200, 200)
win.show()
t0 -= time.clock()
print('List prepared in %.2f s' % -t0)
TrustyTony 888 pyMod Team Colleague Featured Poster

Speedier list by JList.

K:\jython2.5.2>java -jar jython.jar jlist_ex.py
256000 words generated in 9.84 s
Sorted in 1.48 s
Time taken for list: 1.37 s
import random
import string
import time

import javax.swing as swing
import java.awt as awt

class JListWindow(object):
  def __init__(self, title, data):
    t0 = time.clock()
    frame = swing.JFrame(title)
    frame.setLayout(awt.FlowLayout())
    frame.setDefaultCloseOperation(swing.WindowConstants.EXIT_ON_CLOSE)

    spane = swing.JScrollPane()
    spane.getViewport().setView(swing.JList(data))
    self.label = swing.JLabel('Time to build list')
    frame.add(self.label)
    frame.add(spane)
    frame.pack()
    frame.setVisible(True)

    t0 -= time.clock()
    self.label.text = 'Time taken for list: %.2f s' % -t0
    print(self.label.text)


if __name__ == '__main__':
    t0 = time.clock()
    word_max = 256000
    names = [''.join(random.choice(string.ascii_uppercase)
                     for count in range(6))
             for word_count in range(word_max)]
    t0 -= time.clock()
    print('%i words generated in %.2f s' % (word_max, -t0))

    t0 = time.clock()
    names.sort()
    t0 -= time.clock()
    print('Sorted in %.2f s' % -t0)

    JListWindow("Jython Sorted List", names)
TrustyTony 888 pyMod Team Colleague Featured Poster

Making object based on JFrame:

import random
import string
import time

import javax.swing as swing
import java.awt as awt


def random_words(word_max, length):
    return [''.join(random.choice(string.ascii_uppercase)
                     for count in range(length))
             for word_count in range(word_max)]

class WordWindow(swing.JFrame):
    def __init__(self, length=6,  word_max=256000):
        super(swing.JFrame, self).__init__()
        self.contentPane.layout = awt.FlowLayout(  )
        self.setDefaultCloseOperation(swing.WindowConstants.EXIT_ON_CLOSE)
        self.title = 'Testing Swing and Jython'
        self.label = swing.JLabel()
        self.add(self.label)
        self.pack()
        self.show()

        t0 = time.clock()
        self.names = random_words(word_max, length)
        t0 -= time.clock()
        self.label.text = '%i words generated in %.2f s. ' % (word_max, -t0)
        self.pack()

        t0 = time.clock()
        self.names.sort()
        t0 -= time.clock()
        self.label.text += ' Sorting %.2f s.   ' % -t0
        self.pack()

        t0 = time.clock()
        spane = swing.JScrollPane()
        spane.getViewport().setView(swing.JList(self.names))
        t0 -= time.clock()
        self.label.text += 'Build: %.2f s. ' % -t0

        self.add(spane)
        self.pack()

if __name__ == '__main__':
    my_swing = WordWindow()
    print(my_swing.label.text)
kashmirhouse 0 Newbie Poster

Great Posting Friends thanks for information.

TrustyTony 888 pyMod Team Colleague Featured Poster

You are free to upvote and even to give reputation. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.