| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

Jython JTable 1

提供: MyMemoWiki
2020年2月16日 (日) 04:28時点におけるPiroto (トーク | 投稿記録)による版
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
ナビゲーションに移動 検索に移動

Jython JTable 1

Jython Swing | Swing | Jython | Python |

方針

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

ソースコード

実行例

0748 js jtable01.jpg

ソースコード
# -*- 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())