簡體   English   中英

Java - Swing - JTable - 為選定行設置顏色,但不為單元格設置顏色

[英]Java - Swing - JTable - Set Color for Selected Row, but not Cell

當您單擊一個單元格(這可以通過關閉列選擇來完成)時,我試圖使我的表 select 成為一整行,但是,我不希望突出顯示您單擊的特定單元格周圍的超粗邊框。 我希望這會很容易,但顯然它涉及渲染器,所以我做了很多研究,我能得到的最接近的是:

    JTable contactTable = new JTable(tableModel);

    contactTable.setCellSelectionEnabled(true);
    contactTable.setColumnSelectionAllowed(false);
    contactTable.setRowSelectionAllowed(false);
    contactTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    // This renderer extends a component. It is used each time a
    // cell must be displayed.
    class MyTableCellRenderer extends JLabel implements TableCellRenderer {
        // This method is called each time a cell in a column
        // using this renderer needs to be rendered.
        public Component getTableCellRendererComponent(JTable table, Object value,
                boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) {
            // 'value' is value contained in the cell located at
            // (rowIndex, vColIndex)

            if (isSelected) {
                // cell (and perhaps other cells) are selected

            }

            if (hasFocus) {
                // this cell is the anchor and the table has the focus
                this.setBackground(Color.blue);
                this.setForeground(Color.green);
            } else {
                this.setForeground(Color.black);
            }

            // Configure the component with the specified value
            setText(value.toString());

            // Set tool tip if desired
            // setToolTipText((String)value);

            // Since the renderer is a component, return itself
            return this;
        }

        // The following methods override the defaults for performance reasons
        public void validate() {}
        public void revalidate() {}
        protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {}
        public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) {}
    }

    int vColIndex = 0;
    TableColumn col = contactTable.getColumnModel().getColumn(vColIndex);
    col.setCellRenderer(new MyTableCellRenderer());

我從示例中復制了渲染器,只將 hasFocus() function 更改為使用我想要的 colors。 isSelected()中設置 colors 沒有任何作用。

這段代碼的問題是:

  1. 它僅適用於底部 vColIndex 指定的一列。 顯然我希望將其應用於所有列,因此單擊一個單元格會突出顯示整行。 我可以制作一個 for 循環以將其更改為每個單元格,但我認為有一種更好的方法可以同時更改所有列的 cellRenderer。

  2. setForegroundColor()用於更改文本,但setBackgroundColor()對單元格背景沒有任何作用。 我希望它像屬性暗示的那樣實際改變背景顏色。

    • #2 的解決方案:使用this.setOpaque(true); 在分配背景顏色之前。
  3. 當渲染器工作時,它只在單個 Cell 上工作。 我怎樣才能讓它為行中的所有單元格着色?

    • #3 的解決方案:我想通了! 如果您啟用行選擇 ( table.setRowSelectionAllowed(true) ),而不是使用僅影響單個單元格的hasFocus() ,那么您將顏色更改放在if(isSelected)語句中。 然后整行被認為是選中的,它是 colors 中的所有單元格!

3 是最大的,但如果有人知道 #1 或者可以向我解釋為什么它被設計成一次只能將渲染器應用於一列,我們將不勝感激。

太直接了就加行

tablename.setSelectionBackground(Color.red);

就我而言

jtbillItems.setSelectionBackground(Color.red);

教程文章概念:編輯器和渲染器解釋說,“單個單元格渲染器通常用於繪制包含相同類型數據的所有單元格”,如模型的getColumnClass()方法所返回的那樣。

或者,覆蓋為所有單元格調用的prepareRenderer()以有選擇地更改行的外觀。 示例比較了這兩種方法, 表格行呈現一文擴展了該方法的多功能性。

對於第二個問題,您可以嘗試 setSelectionBackground(Color) 和 setSelectionForeGround(Color) 方法。 我不確定您如何解決第一個問題。 最后一個建議您可以使用一些 swing 設計器插件,例如 JBuilder。 這會有很大幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM