繁体   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