- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
好吧,因为我正在制作房间租赁应用程序,而 Jtable 给我带来了困惑,我需要你的帮助。当选择某个房间时,我的 Jtable 应该更新,以不允许用户选择包含占用房间日期的单元格。因此,我需要通过传递行索引和列索引来使某些单元格不可选择。我该怎么做?
最佳答案
hey hi actually yes I will be making changes to cell colors but problem is I have cell renderer class but Only way I managed to change colors using this table.getColumnModel().getColumn( 0 ).setCellRenderer( tce ); but its not good since this requires mouse click to change color so I have no idea how to show which cell to paint (by row and column) without any clicking because color must be changed once room selected from combobox. So yeah I stuck with this. Maybe you can help me with this I would be really grateful. Its so hard to work with tables
这个基本想法听起来不错,但我需要查看您的代码才能知道为什么它不起作用。
下面是使用 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.
}
}
}
这里需要注意的是,渲染与单个渲染器相关,因此所有渲染逻辑都需要通过这个单个渲染器来执行。
关于 java 。如何使Jtable特定单元格不可选?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43434382/
我正在尝试用 Swift 编写这段 JavaScript 代码:k_combinations 到目前为止,我在 Swift 中有这个: import Foundation import Cocoa e
我是一名优秀的程序员,十分优秀!