トップ 一覧 ping 検索 ヘルプ RSS ログイン

Jython JTable 1の変更点

  • 追加された行はこのように表示されます。
  • 削除された行はこのように表示されます。
!!!Jython JTable 1
[Jython Swing][Swing][Jython][Python]

!!方針
以下を参考に、Jythonでテーブルを実装してみる
*http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
*http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#simple

!!ソースコード
::実行例
{{ref_image 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))
 
         # Create the scroll pane and add the table to it.
         scrollPane = JScrollPane(table)
 
         # Add the scroll pane to this panel.
         self.add(scrollPane)
 
     def createUI(self):
         # Create and set up the window.
         frame = JFrame("SimpleTableDemo")
         frame.defaultCloseOperation = JFrame.EXIT_ON_CLOSE
 
         # Create and set up the content pane.
         self.opaque = True
         frame.contentPane = self
 
         # Display the window.
         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())