⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch06.html

📁 java2高级编程
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<a name="pgfId-1087634"></a>//API Ref: <a name="52281"></a>ListSelectionMode getSelectionModel()</pre>					<pre class="CODE"><a name="pgfId-1087637"></a>    <a name="marker-1087635"></a> <a name="getSelectionModel method"></a>ListSelectionModel scrollSelection = scrollTable.getSelectionModel();     scrollSelection.addListSelectionListener(new ListSelectionListener() {       public void valueChanged(ListSelectionEvent e) {         ListSelectionModel lsm = (ListSelectionModel)e.getSource();         if (!lsm.isSelectionEmpty())  {           setFixedRow();         }       }     });     //Create a custom renderer and use it on column 2     CustomRenderer custom = new CustomRenderer();     custom.setHorizontalAlignment(JLabel.CENTER);</pre>					<pre class="CODE-caption"><a name="pgfId-1087639"></a>//API Ref: <a name="44648"></a>void setCellRenderer(TableCellRenderer renderer)</pre>					<pre class="CODE"><a name="pgfId-1087641"></a>     <a name="marker-1087640"></a>scrollColumnModel.getColumn(2).setCellRenderer(custom);        //Use the custom button renderer on column 3          scrollColumnModel.getColumn(3).setCellRenderer(new                                                     CustomButtonRenderer());     //Add the custom button editor to column 3      CustomButtonEditor customEdit=new CustomButtonEditor(frame);     scrollColumnModel.getColumn(3).setCellEditor(customEdit);     //Add the scrolling column headers to the header viewport     //the headers viewport is later added to the headerPanel with     //an associated scroll bar</pre>					<pre class="CODE-caption"><a name="pgfId-1087643"></a>//API Ref: <a name="47649"></a>JTableHeader getTableHeader()</pre>					<pre class="CODE"><a name="pgfId-1087646"></a>     <a name="marker-1087644"></a><a name="getTableHeader method"></a>headers.add(scrollTable.getTableHeader());          //Create a panel for the top left of the display that contains     //the headers for the column that does not move     JPanel topPanel = new JPanel();     topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.X_AXIS));     adjustColumnWidth(fixedColumnModel.getColumn(0), 100);     JTableHeader fixedHeader=fixedTable.getTableHeader();     fixedHeader.setAlignmentY(Component.TOP_ALIGNMENT);     topPanel.add(fixedHeader);</pre>					<pre class="CODE-caption"><a name="pgfId-1087648"></a>//API Ref: <a name="67020"></a>Component createRigidArea(Dimension d)</pre>					<pre class="CODE"><a name="pgfId-1087653"></a>    <a name="marker-1087649"></a> <a name="marker-1087650"></a><a name="marker-1087651"></a><a name="marker-1087652"></a>topPanel.add(Box.createRigidArea(new Dimension(2, 0)));</pre>					<pre class="CODE"><a name="pgfId-1087654"></a>     topPanel.setPreferredSize(new Dimension(400, 40));          //Create a panel to contain the scroll headers that will     //be displayed at the top right </pre>					<pre class="CODE"><a name="pgfId-1087655"></a>     JPanel headerPanel = new JPanel();     headerPanel.setAlignmentY(Component.TOP_ALIGNMENT);</pre>					<pre class="CODE"><a name="pgfId-1087656"></a>     headerPanel.setLayout(new BorderLayout());</pre>					<pre class="CODE"><a name="pgfId-1087657"></a>     JScrollPane scrollpane = new JScrollPane();</pre>					<pre class="CODE-caption"><a name="pgfId-1087659"></a>//API Ref: <a name="67384"></a>JScrollBar getHorizontalScrollBar()</pre>					<pre class="CODE"><a name="pgfId-1087664"></a>    <a name="marker-1087660"></a> <a name="marker-1087661"></a><a name="marker-1087662"></a><a name="marker-1087663"></a>scrollBar = scrollpane.getHorizontalScrollBar();     headerPanel.add(headers, &quot;North&quot;);     headerPanel.add(scrollBar, &quot;South&quot;);     topPanel.add(headerPanel);     //Set the scrollable part of both the table views     scrollTable.setPreferredScrollableViewportSize(new Dimension(300,180));     fixedTable.setPreferredScrollableViewportSize( new Dimension(100,180));     fixedTable.setPreferredSize(new Dimension(100,180));     innerPort = new JViewport();     //If the user moves the scrollbar at the top     //of the scrollable data update the header and table     //data positions so they are viewable </pre>					<pre class="CODE-caption"><a name="pgfId-1087666"></a>//API Ref: <a name="94020"></a>void setView(Component view)</pre>					<pre class="CODE"><a name="pgfId-1087671"></a>    <a name="marker-1087667"></a> <a name="javax.swing.JViewport class"></a><a name="JViewport class"></a><a name="getViewPosition method"></a>innerPort.setView(scrollTable);     scrollpane.setViewport(innerPort);      scrollBar.getModel().addChangeListener(new ChangeListener()  {       public void stateChanged(ChangeEvent e) {</pre>					<pre class="CODE-caption"><a name="pgfId-1087673"></a>//API Ref: <a name="28082"></a>Point getViewPosition()</pre>					<pre class="CODE"><a name="pgfId-1087676"></a>         <a name="marker-1087674"></a><a name="marker-1087675"></a>Point q = headers.getViewPosition();         Point p = innerPort.getViewPosition();         int val = scrollBar.getModel().getValue();         p.x = val;         q.x = val;</pre>					<pre class="CODE-caption"><a name="pgfId-1087677"></a>//API Ref: void setView(Component view)</pre>					<pre class="CODE"><a name="pgfId-1087680"></a>        <a name="marker-1087678"></a> <a name="setViewPosition method"></a>headers.setViewPosition(p);         headers.repaint(headers.getViewRect());         innerPort.setViewPosition(p);         innerPort.repaint(innerPort.getViewRect());       }     });     //Disable continuous update of the scroll table. Needed to      //improve performance of a scrolling table in SDK 1.2     scrollTable.getTableHeader().setUpdateTableInRealTime(false);          //The bottom panel is where both tables data is positioned     JPanel bottomPanel = new JPanel();     bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.X_AXIS));     fixedTable.setAlignmentY(Component.TOP_ALIGNMENT);     bottomPanel.add(fixedTable);     bottomPanel.add(Box.createRigidArea(new Dimension(2, 0)));     innerPort.setAlignmentY(Component.TOP_ALIGNMENT);     bottomPanel.add(innerPort);     bottomPanel.add(Box.createRigidArea(new Dimension(2, 0)));     //Only allow vertical scrollbars as we are managing the      //horizontal scroll ourselves     scrollPane = new JScrollPane(bottomPanel,                      JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,                                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);     JViewport outerPort = new JViewport();     outerPort.add(bottomPanel);     //Workaround to affix the headers to the top of the scrollpane     //uses setColumnHeaderView</pre>					<pre class="CODE-caption"><a name="pgfId-1087682"></a>//API Ref: <a name="63995"></a>void setColumnHeaderView(Component view)</pre>					<pre class="CODE"><a name="pgfId-1087687"></a>      <a name="marker-1087683"></a><a name="marker-1087684"></a><a name="marker-1087685"></a><a name="marker-1087686"></a>scrollPane.setColumnHeaderView(topPanel);</pre>					<pre class="CODE-caption"><a name="pgfId-1087689"></a>//API Ref: <a name="16340"></a>void setViewport(JViewport viewport)</pre>					<pre class="CODE"><a name="pgfId-1087694"></a>    <a name="marker-1087690"></a> <a name="javax.swing.JScrollPane class"></a><a name="JScrollPane class"></a><a name="setViewPort method"></a>scrollPane.setViewport(outerPort);     scrollTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);     frame.getContentPane().add(scrollPane);     scrollTable.validate();     frame.setSize(450,200);  }  void setFixedRow() {    int index=scrollTable.getSelectedRow();    fixedTable.setRowSelectionInterval(index, index);  }  void setScrollableRow() {    //Highlight the row by calling setRowSelectionInterval with a     //range of 1    int index=fixedTable.getSelectedRow();    scrollTable.setRowSelectionInterval(index, index);  }  void adjustColumnWidth(TableColumn c, int size) {    c.setPreferredWidth(size);    c.setMaxWidth(size);    c.setMinWidth(size);  }                                          </pre>					<p class="Body"><a name="pgfId-1087697"></a><em class="C-Code">JList</em> <em class="Bold">Model. </em><a name="marker-1087695"></a>The <em class="CODE">JList</em> <a name="marker-1087696"></a>component displays a vertical list of data elements and uses a <em class="CODE">ListModel</em> <a name="marker-1087698"></a>to hold and manipulate the data. It also uses a <em class="CODE">ListSelectionModel</em> <a name="marker-1087699"></a>object to enable selection and subsequent retrieval of elements in the list.</p>					<p class="Body"><a name="pgfId-1087702"></a>Default implementations of the <em class="CODE">AbstractListModel</em> <a name="marker-1087700"></a>and <em class="CODE">AbstractListSelectionModel</em> <a name="marker-1087701"></a>classes are provided in the Project Swing API in the form of the <em class="CODE">DefaultListModel</em> <a name="marker-1087703"></a>and <em class="CODE">DefaultListSelectionModel</em> <a name="marker-1087704"></a>classes. If you use these two default models and the default cell renderer, you get a list that displays model elements by calling the <em class="CODE">toString</em> method on each object. The list uses the <em class="CODE">MULTIPLE_INTERVAL_SELECTION</em> <a name="marker-1087705"></a>list selection model to select each element from the list.</p>					<p class="Body"><a name="pgfId-1087706"></a>Three selection modes are available to <em class="CODE">DefaultListSelectionModel</em> : <em class="CODE">SINGLE_SELECTION</em> <a name="marker-1087707"></a>, where only one item is selected at a time; <em class="CODE">SINGLE_INTERVAL_SELECTION</em> <a name="marker-1087708"></a>, in which a range of sequential items can be selected; and <em class="CODE">MULTIPLE_INTERVAL_SELECTION</em> <a name="marker-1087709"></a>, which allows any or all elements to be selected. The selection mode can be changed by calling the <em class="CODE">setSelectionMode</em> <a name="marker-1087710"></a>method in the <em class="CODE">JList</em> class.</p>					<pre class="CODE"><a name="pgfId-1087711"></a>public SimpleList() {  JList list;  DefaultListModel deflist;  deflist= new DefaultListModel();  deflist.addElement(&quot;element 1&quot;);  deflist.addElement(&quot;element 2&quot;);  list = new JList(deflist);  JScrollPane scroll = new JScrollPane(list);  getContentPane().add(scroll, BorderLayout.CENTER);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -