トップ 差分 一覧 ping ソース 検索 ヘルプ PDF RSS ログイン

Jython JTable 1



目次



記事一覧

キーワード

Jython JTable 1

[Jython Swing][Swing][Jython][Python]

 方針

以下を参考に、Jythonでテーブルを実装してみる


 ソースコード

実行例

ソースコード

# -*- coding: utf-8 -*-

from java.lang import *
from javax.swing import JFrame;
from javax.swing import JPanel;
from javax.swing import JScrollPane;
from javax.swing import JTable;
from javax.swing import SwingUtilities
from javax.swing.table import TableModel
from javax.swing import UIManager
from java.awt import Dimension;
from java.awt import GridLayout;
from java.awt.event import MouseAdapter;
from java.awt.event import MouseEvent;

class JTableTest(JPanel):

    def __init__(self):
        self.DEBUG = True
        super(JTableTest,self).__init__(GridLayout(1,0))

        columnNames = [ "First Name",
                        "Last Name",
                        "Sport",
                        "# of Years",
                        "Vegetarian"]

        data = [
            ["Mary", "Campione","Snowboarding", 5, False],
            ["Alison", "Huml","Rowing", 3, True],
            ["Kathy", "Walrath","Knitting", 2, False],
            ["Sharon", "Zakhour","Speed reading", 20, True],
            ["Philip", "Milne","Pool", 10, False]
        ]

        table = JTable(data, columnNames)
        table.preferredScrollableViewportSize = Dimension(500, 70)
        table.fillsViewportHeight = True

        if self.DEBUG:
            table.addMouseListener(MouseListener(table))

        scrollPane = JScrollPane(table)
        self.add(scrollPane)

    def createUI(self):
        frame = JFrame("SimpleTableDemo")
        frame.defaultCloseOperation = JFrame.EXIT_ON_CLOSE

        self.opaque = True
        frame.contentPane = self

        frame.pack()
        frame.visible = True
        
class MouseListener(MouseAdapter):
    def __init__(self, table):
        self.table = table
        
    def mouseClicked(self, e):
        numRows = self.table.rowCount
        numCols = self.table.columnCount
        model = self.table.model

        print "Value of data: "
        for i in range(numRows):
            print "\t\trow %d:" % i,
            for j in range(numCols):
                print "\t%s" % model.getValueAt(i, j),
            print ""
        print "-" * 25

class Invoker(Runnable):
    def run(self):
        # Java Look & Feel (Metal) のデフォルトでボールドフォントを使用しない
        UIManager.put("swing.boldMetal", Boolean.FALSE)
        sample = JTableTest()
        sample.createUI()
        
SwingUtilities.invokeLater(Invoker())



YAGI Hiroto (piroto@a-net.email.ne.jp)
twitter http://twitter.com/pppiroto

Copyright© 矢木 浩人 All Rights Reserved.