📄 ch06.html
字号:
<a name="pgfId-1087782"></a> for (int i=0; i < openItems.length; i++) {</pre> <pre class="CODE"><a name="pgfId-1087783"></a> //Populate the open item nodes with the open item data,</pre> <pre class="CODE"><a name="pgfId-1087784"></a> //i.e., auction ids opennodes[i] = new DefaultMutableTreeNode(openItems[i]); </pre> <pre class="CODE"><a name="pgfId-1087785"></a> //Add the open items under the Open Auction parent node nodes[2].add(opennodes[i]); }</pre> <pre class="CODE"><a name="pgfId-1087786"></a> //Finally, create the tree model from the nodes created above</pre> <pre class="CODE"><a name="pgfId-1087787"></a> //and then create the tree DefaultTreeModel model=new DefaultTreeModel(nodes[0]); tree = new JTree(model); JScrollPane scroll = new JScrollPane(tree); getContentPane().add(scroll, BorderLayout.CENTER); findPanel= new JPanel(); findField= new JTextField(10); findButton= new JButton("find");</pre> <pre class="CODE-caption"><a name="pgfId-1087789"></a>//API Ref: <a name="48732"></a>void addActionListener(ActionListener l)</pre> <pre class="CODE"><a name="pgfId-1087794"></a> <a name="marker-1087790"></a><a name="marker-1087791"></a><a name="marker-1087792"></a><a name="marker-1087793"></a>findButton.addActionListener (new ActionListener() {</pre> <pre class="CODE-caption"><a name="pgfId-1087796"></a>//API Ref: <a name="12167"></a>void actionPerformed(ActionEvent e)</pre> <pre class="CODE"><a name="pgfId-1087801"></a> <a name="marker-1087797"></a><a name="marker-1087798"></a><a name="marker-1087799"></a>public void <a name="marker-1087800"></a>actionPerformed (ActionEvent e) { String field=findField.getText(); if (field != null) {</pre> <pre class="CODE"><a name="pgfId-1087802"></a> //Search for the node whose label matches the search string findNode(findField.getText()); } else { return; } } }); findPanel.add(findField); findPanel.add(findButton); getContentPane().add(findPanel, BorderLayout.SOUTH); } public void findNode(String field) {</pre> <pre class="CODE"><a name="pgfId-1087803"></a> //Get an enumeration list of all the nodes.</pre> <pre class="CODE"><a name="pgfId-1087804"></a> //The ordering is generated by starting from the root</pre> <pre class="CODE"><a name="pgfId-1087805"></a> //to the end of each branch, and to the end of</pre> <pre class="CODE"><a name="pgfId-1087806"></a> //the next branch including any nodes not visited before.</pre> <pre class="CODE-caption"><a name="pgfId-1087808"></a>//API Ref: <a name="77673"></a>Enumeration depthFirstEnumeration() </pre> <pre class="CODE"><a name="pgfId-1087811"></a> <a name="marker-1087809"></a><a name="depthFirstEnumeration method"></a>Enumeration e = nodes[0].depthFirstEnumeration(); Object currNode; while (e.hasMoreElements()) { currNode = e.nextElement(); if (currNode.toString().equals(field)) { </pre> <pre class="CODE"><a name="pgfId-1087812"></a> //Does the node match our search string? If so, generate</pre> <pre class="CODE"><a name="pgfId-1087813"></a> //a path object to highlight the matching node</pre> <pre class="CODE"><a name="pgfId-1087814"></a> //with a call to setSelectionRow.</pre> <pre class="CODE-caption"><a name="pgfId-1087816"></a>//API Ref: <a name="58137"></a>TreePath(Object singlePath)</pre> <pre class="CODE"><a name="pgfId-1087821"></a> <a name="marker-1087817"></a><a name="javax.swing.tree.TreePath class"></a><a name="TreePath class"></a><a name="TreePath constructor"></a>TreePath path=new TreePath(((DefaultMutableTreeNode)currNode).getPath());</pre> <pre class="CODE-caption"><a name="pgfId-1087823"></a>//API Ref: <a name="11684"></a>void makeVisible(TreePath path)</pre> <pre class="CODE"><a name="pgfId-1087828"></a> <a name="marker-1087824"></a><a name="javax.swing.JTree class"></a><a name="JTree class"></a><a name="makeVisible method"></a>tree.makeVisible(path);</pre> <pre class="CODE-caption"><a name="pgfId-1087830"></a>//API Ref: <a name="72169"></a>void setSelectionRow(int row)</pre> <pre class="CODE"><a name="pgfId-1087833"></a> <a name="marker-1087831"></a><a name="setSelectionRow method"></a>tree.setSelectionRow(tree.getRowForPath(path)); return; } } }</pre> <pre class="CODE"><a name="pgfId-1087834"></a> public static void main(String[] args) { SimpleSearchTree frame = new SimpleSearchTree();</pre> <pre class="CODE-caption"><a name="pgfId-1087836"></a>//API Ref: <a name="27085"></a>void addWindowListener(WindowListener l)</pre> <pre class="CODE"><a name="pgfId-1087841"></a> <a name="marker-1087837"></a><a name="marker-1087838"></a><a name="marker-1087839"></a><a name="marker-1087840"></a>frame.addWindowListener( new WindowAdapter() {</pre> <pre class="CODE-caption"><a name="pgfId-1087843"></a>//API Ref: <a name="64689"></a>void windowClosing(WindowEvent e)</pre> <pre class="CODE"><a name="pgfId-1087848"></a> <a name="marker-1087844"></a><a name="marker-1087845"></a><a name="marker-1087846"></a><a name="marker-1087847"></a>public void windowClosing( WindowEvent e ) { System.exit(0); } }); frame.setVisible(true); frame.pack(); frame.setSize(300,150); } }</pre> <p class="Body"><a name="pgfId-1087849"></a><em class="CODE">JTree</em> , <em class="CODE">JTable</em> , and <em class="CODE">JList</em> are probably the most common components you will want to customize. But you can use models such as <em class="CODE">SingleSelectionModel</em> for general data manipulation. The <em class="CODE">SingleSelectionModel</em> class lets you specify how data is selected in a component. <a name="render"></a></p> <p class="Body"><a name="pgfId-1087853"></a><em class="Bold">Custom Cell Rendering. </em><a name="marker-1087851"></a><a name="marker-1087852"></a>As you learned above, many components have a default cell renderer to paint each element in a table, tree, or list. The default cell renderer is usually a <em class="CODE">JLabel</em> and displays a <em class="CODE">String</em> representation of the data element.</p> <p class="Body"><a name="pgfId-1087855"></a>A simple custom cell renderer can extend the <em class="CODE">Default<type>CellRenderer</em> <a name="marker-1087854"></a>class to provide additional customization in the <em class="CODE">get<type>CellRenderer</em> <a name="marker-1087856"></a>. The <em class="CODE">DefaultTableCellRenderer</em> and <em class="CODE">DefaultTreeCellRenderer</em> components both use a <em class="CODE">JLabel</em> to render the cell. This means any customization that can be applied to a <em class="CODE">JLabel</em> can also be used in the <em class="CODE">JTable</em> or <em class="CODE">JTree</em> cell.</p> <p class="Body"><a name="pgfId-1087857"></a>For example, the following renderer sets the background color of the component if the auction item has received a high number of bids:</p> <pre class="CODE"><a name="pgfId-1087858"></a>class CustomRenderer extends DefaultTableCellRenderer {</pre> <pre class="CODE-caption"><a name="pgfId-1087860"></a>//API Ref: <a name="86315"></a>Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)</pre> <pre class="CODE"><a name="pgfId-1087865"></a> <a name="marker-1087861"></a><a name="javax.swing.table.DefaultTableCellRenderer class"></a><a name="DefaultTableCellRenderer class"></a><a name="getTableCellRendererComponent method"></a>public Component getTableCellRendererComponent(JTable table,Object value, boolean isSelected, boolean hasFocus, int row, int column) { Component comp = super.getTableCellRendererComponent(table, value,isSelected, hasFocus, row,column); JLabel label = (JLabel)comp;</pre> <pre class="CODE"><a name="pgfId-1087866"></a> //Does the auction item have 30 or more bids. If so display</pre> <pre class="CODE"><a name="pgfId-1087867"></a> //a hot item image if(((Integer)value).intValue() >= 30) {</pre> <pre class="CODE-caption"><a name="pgfId-1087869"></a>//API Ref: <a name="47804"></a>void setIcon(Icon icon)</pre> <pre class="CODE"><a name="pgfId-1087874"></a> <a name="marker-1087870"></a><a name="marker-1087871"></a><a name="marker-1087872"></a><a name="marker-1087873"></a>label.setIcon(new ImageIcon("Hot.gif")); } else { label.setIcon(new ImageIcon("Normal.gif")); } return label; }} </pre> <p class="Body"><a name="pgfId-1087875"></a>The renderer is set on a column like this:</p> <pre class="CODE"><a name="pgfId-1087876"></a> CustomRenderer custom = new CustomRenderer(); custom.setHorizontalAlignment(JLabel.CENTER); scrollColumnModel.getColumn(2).setCellRenderer(custom);</pre> <p class="Body"><a name="pgfId-1087877"></a>If the component being displayed inside the <em class="CODE">JTable</em> column requires more functionality than is available using a <em class="CODE">JLabel</em> , you can create your own <em class="CODE">TableCellRenderer</em> . This next code example uses a <em class="CODE">JButton</em> as the renderer cell.</p> <pre class="CODE"><a name="pgfId-1087878"></a>class CustomButtonRenderer extends JButton implements TableCellRenderer { public CustomButtonRenderer() { setOpaque(true); }</pre> <pre class="CODE-caption"><a name="pgfId-1087880"></a>//API Ref: <a name="57671"></a>Component getTableCellRendere
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -