簡體   English   中英

Java的。 如何使Jtable特定單元格無法選擇?

[英]Java. How to make Jtable specific cell not selectable?

好吧,因為我正在房間租用應用程序和Jtable給我帶來困惑,我需要你的幫助。 當選擇某個房間時,我的Jtable應該更新為不允許用戶選擇包含拍攝日期的單元格。 所以我需要通過傳遞rowindex和columnindex來使某些單元格不可選。 我該怎么做呢?

您可以使用實現isCellEditable()函數的Custom JTable ,並假設我要禁止選擇First Column的所有條目,然后我可以將其實現為:

JTable  table=new JTable()
{
    public boolean isCellEditable(int rowindex, int colindex)
    {
        if(colindex==0)
        {
            return false; // Disallow Column 0
        } 
        else 
        {
            return true;  // Allow the editing
        }
    }        
};

然后,假設我們有table datadata[][] arrayheadersheaders[]數組,那么我們需要設置Model的JTable並添加dataheaders到它,然后我們將實施TableModelListener調用編輯方法,每當一個細胞點擊。

table.setModel(tableModel);            
table.getModel().addTableModelListener(new TableModelListener() 
{
    public void tableChanged(TableModelEvent e)
    {
        if(e.getType()==TableModelEvent.UPDATE)
        {
            int col = e.getColumn();
            int row=e.getFirstRow();
            UpdateData(row,col); //This function will be called whenever a cell is clicked.
        }
    }
});

現在使用UpdateData函數,您可以使用邏輯實現單擊該單元時要對該單元格執行的操作。

public void UpdateData(int row,int col) 
{ 
    //Implement the logic to update data
}

因此,這是使JTable禁止編輯某些單元格的一種方法。 您還可以根據需要更改isCellEditable()函數的邏輯。

假設我有許多特定的單元格未被編輯,那么我可以存儲這些單元格的索引,並且每次檢查單擊的單元格不存在於不被編輯的條目中。

ArrayList<Integer> row_index=new ArrayList<Integer>();
ArrayList<Integer> column_index=new ArrayList<Integer>();

row_index.add(4);column_index.add(3);
row_index.add(1);column_index.add(3);
row_index.add(2);column_index.add(2);

now implmentation of `isCellEditable()` function:

public boolean isCellEditable(int rowindex, int colindex)
{
    for(int i=0;i<row_index.size();i++)
    {
        int row=row_index.get(i);
        int column=column_index.get(i);

        if(row==rowindex && column=columnindex) //If the entry exists 
            return false;
    }

    return true;
} 

我在這里使用Dynamic Structure來存儲索引,假設將在JTable添加新條目。

嘿嗨實際上是的我會更改單元格顏色,但問題是我有單元格渲染類但只有這樣我才能使用此表更改顏色.getColumnModel()。getColumn(0).setCellRenderer(tce); 但它不好,因為這需要鼠標點擊改變顏色所以我不知道如何顯示哪個單元格(按行和列)沒有任何點擊,因為一旦從組合框中選擇了房間,就必須更改顏色。 所以是的,我堅持這一點。 也許你可以幫助我,我會非常感激。 它很難用於表格

這個基本想法聽起來不錯,但我需要看看你的代碼才知道它為什么不起作用。

下面是使用TableCellRenderer顯示哪些單元格被“預訂”的基本示例,它們阻止UI將它們繪制為選中,用戶仍然可以單擊/選擇單元格,表格將生成選擇通知,但在視覺上,他們贏了看起來沒有選中。

選擇不突出

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.util.Vector;
import javax.swing.DefaultListSelectionModel;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class Cell {

        private boolean isBooked;

        public Cell() {
            isBooked = Math.random() > 0.5 ? true : false;
        }

        public boolean isBooked() {
            return isBooked;
        }
    }

    public static class CellTableCellRenderer extends DefaultTableCellRenderer {

        private static Color BOOKED_COLOR = Color.DARK_GRAY;

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            if (value instanceof Cell) {
                Cell cell = (Cell) value;
                if (cell.isBooked) {
                    setBackground(BOOKED_COLOR);
                } else if (isSelected) {
                    setBackground(table.getSelectionBackground());
                } else {
                    setBackground(table.getBackground());
                }
            }
            return this;
        }

    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new BorderLayout());

            DefaultTableModel model = new DefaultTableModel(0, 10) {
                @Override
                public Class<?> getColumnClass(int columnIndex) {
                    return Cell.class;
                }

            };
            for (int row = 0; row < 10; row++) {
                Vector<Cell> cells = new Vector<>(10);
                for (int col = 0; col < 10; col++) {
                    cells.add(new Cell());
                }
                model.addRow(cells);
            }

            JTable table = new JTable(model);
            table.setDefaultRenderer(Cell.class, new CellTableCellRenderer());
            add(new JScrollPane(table));
        }

    }

    public class RowSelectionModel extends DefaultListSelectionModel {

        @Override
        public void setSelectionInterval(int index0, int index1) {
            System.out.println("Row-setSelectionInterval-" + index0 + "-" + index1);
            super.setSelectionInterval(index0, index1); //To change body of generated methods, choose Tools | Templates.
        }
    }

    public class ColumnSelectionModel extends DefaultListSelectionModel {

        @Override
        public void setSelectionInterval(int index0, int index1) {
            System.out.println("Column-setSelectionInterval-" + index0 + "-" + index1);
            super.setSelectionInterval(index0, index1); //To change body of generated methods, choose Tools | Templates.
        }
    }
}

這里需要注意的重要事項是,渲染與單個渲染器相關聯,因此渲染的所有邏輯都需要通過這個渲染進行。

暫無
暫無

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

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