1: #! /usr/bin/env python
2: # coding: utf-8
3: ## ----------------------------------------
4: ##
5: ## (C) Copyright 2000-2010, 小粒ちゃん《監修》小泉ひよ子とタマゴ倶楽部
6: ##
7: ## ----------------------------------------
8: #...+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
9: """
10: >>> tips()
11:
12: >>> ## ----------------------------------------
13: >>> None
14: version: #1.0.18
15: """
16: ## ---------------------------------------- demo: jython
17: """
18: _jython=/Users/sketch/book/java_useBetter_1/_jython
19:
20: cd /Users/sketch/book/java_useBetter_1/swing/JSplitPane/splitPaneDemo/jython
21:
22: JYTHONPATH=$_jython jython -i SplitPaneDemo.py
23:
24: ============================================================
25: Version: 2.5.0 (Release_2_5_0:6476, Jun 16 2009, 13:33:26)
26: Module : SplitPaneDemo.py
27: Date : Sat Sep 18 15:27:40 2010
28: ============================================================
29: """
30: ## ----------------------------------------
31: from java.awt import Dimension
32: from java.awt import Font
33: from javax.swing import ImageIcon
34: from javax.swing import JFrame
35: from javax.swing import JLabel
36: from javax.swing import JList
37: from javax.swing import JPanel
38: from javax.swing import JScrollPane
39: from javax.swing import JSplitPane
40: from javax.swing import ListSelectionModel
41: from javax.swing.event import ListSelectionListener
42:
43: from os.path import exists
44:
45: #/SplitPaneDemo itself is not a visible component.
46: class SplitPaneDemo(JPanel): # ListSelectionListener
47: imageNames = [
48: "Bird", "Cat", "Dog", "Rabbit", "Pig", "dukeWaveRed",
49: "kathyCosmo", "lainesTongue", "left", "middle", "right", "stickerface",
50: ]
51:
52: def __init__(self, *args, **keys):
53: #/Create the list of images and put it in a scroll pane.
54: list = JList(
55: self.imageNames,
56: selectionMode = ListSelectionModel.SINGLE_SELECTION,
57: selectedIndex = 0,
58: valueChanged = self,
59: )
60: listScrollPane = JScrollPane(list)
61:
62: self.picture = JLabel(
63: horizontalAlignment = JLabel.CENTER,
64: )
65: self.picture.font = self.picture.font.deriveFont(Font.ITALIC)
66:
67: pictureScrollPane = JScrollPane(self.picture)
68:
69: #/Create a split pane with the two scroll panes in it.
70: self.splitPane = JSplitPane(
71: orientation = JSplitPane.HORIZONTAL_SPLIT,
72: leftComponent = listScrollPane,
73: rightComponent = pictureScrollPane,
74: oneTouchExpandable = True,
75: dividerLocation = 150,
76: )
77:
78: #/Provide minimum sizes for the two components in the split pane.
79: minimumSize = Dimension(100, 50)
80: listScrollPane.minimumSize = minimumSize
81: pictureScrollPane.minimumSize = minimumSize
82:
83: #/Provide a preferred size for the split pane.
84: self.splitPane.preferredSize = 400, 200
85: self.updateLabel(self.imageNames[list.selectedIndex])
86:
87: ## ----------------------------------------
88: #/Listens to the list
89: def __call__(self, e):
90: list = e.source
91: self.updateLabel(self.imageNames[list.selectedIndex])
92:
93: #/Renders the selected image
94: def updateLabel(self, name):
95: icon = self.createImageIcon("_images/%s.gif"%name)
96: self.picture.icon = icon
97: self.picture.text = None if icon else "Image not found"
98:
99: #/Used by SplitPaneDemo2
100:
101: #/** Returns an ImageIcon, or null if the path was invalid. */
102: @classmethod
103: def createImageIcon(self, path):
104: if exists(path):
105: return ImageIcon(path)
106: else:
107: print("Couldn't find file: " + path) #, file=sys.stderr)
108: return None
109:
110: """
111: * Create the GUI and show it. For thread safety,
112: * this method should be invoked from the
113: * event-dispatching thread.
114: """
115: @staticmethod
116: def createAndShowGUI():
117: #/Create and set up the window.
118: frame = JFrame(
119: title = "SplitPaneDemo",
120: locationRelativeTo = None,
121: defaultCloseOperation = JFrame.EXIT_ON_CLOSE,
122: )
123: splitPaneDemo = SplitPaneDemo()
124: frame.contentPane.add(splitPaneDemo.splitPane)
125:
126: #/Display the window.
127: frame.pack()
128: frame.visible = True
129:
130: ## ----------------------------------------
131: from __init__ import *
132: tips = SplitPaneDemo.createAndShowGUI
133:
134: if __name__=='__main__':
135: inform()
136: testmod()
137:
138: ## ========================================