gpt4 book ai didi

java - 当我单击按钮时 JTable 颜色单元格永久

转载 作者:行者123 更新时间:2023-11-30 09:02:02 25 4
gpt4 key购买 nike

我想创建一个 JTable 并通过单击按钮永久为其单元格着色(非常像 excel 中的单元格着色)。到目前为止,我选择的每个单元格都会进入 ArrayList<Cell>。 .

首先,我希望在单击按钮时列表中的每个单元格都永久着色(这与我找到的代码不同)。我是否必须使用这样的语句 table.getColumnModel().getColumn(column).setCellRenderer(this);

这是一个可编译的代码,如果你调试它,你会看到当点击按钮时,我无法访问 getTableCellRendererComponent() 方法。为什么会这样?如果您能告诉我您的意见,我将不胜感激。

(注意:这是一个庞大程序的复制品,所以它有点乱,并且在某些地方进行了硬编码。无法针对我要解决的问题缩小它)。

类 ColorSelectedTableCells.java

public class ColorSelectedTableCells extends JPanel {
private JButton btn = new JButton("color cells");
private MyCellRenderer myCellRenderer = new MyCellRenderer();
public static final Object[][] DATA = new Object[3][3];
public static final String[] COLS = {"A", "B", "C"};
private static final int PREF_WIDTH = 400;
private static final int PREF_HEIGHT = 300;
private static CellSelectionSet cellSelectionSet = new CellSelectionSet();

private JTable table = new JTable(DATA,COLS){
@Override
public boolean isCellEditable(int row, int column) {return false;}

@Override
public boolean isCellSelected(int row, int column) {
if (cellSelectionSet.containsOneOrLess()) {
return super.isCellSelected(row, column);
}
return cellSelectionSet.contains(row, column);
}

@Override
public void changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend) {
super.changeSelection(rowIndex, columnIndex, toggle, extend);
if (toggle) {
cellSelectionSet.add(rowIndex, columnIndex);
}
else {
if (extend) {
cellSelectionSet.add(rowIndex, columnIndex);
}
else {
cellSelectionSet.clear();
cellSelectionSet.add(rowIndex, columnIndex);
}
}
}
};

public ColorSelectedTableCells() {
table.setDefaultRenderer(Integer.class, myCellRenderer);
table.setCellSelectionEnabled(true);
table.setColumnSelectionAllowed(false);
table.setRowSelectionAllowed(false);

JScrollPane scrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JPanel btnPanel = new JPanel();
btnPanel.add(btn);

setLayout(new BorderLayout());
add(scrollPane, BorderLayout.CENTER);
add(btnPanel, BorderLayout.SOUTH);

btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
myCellRenderer.setShowSelected(true);
table.repaint();
}
});
}

@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_WIDTH, PREF_HEIGHT);
}

private static void createAndShowUI() {
JFrame frame = new JFrame();
frame.getContentPane().add(new ColorSelectedTableCells());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}

MyCellRenderer.java 类

private static class MyCellRenderer extends DefaultTableCellRenderer {
private boolean showSelected = false;
private byte colorSwitcher;

public void setShowSelected(boolean showSelected) {
this.showSelected = showSelected;
}

public void setColorSwitcher(byte colorSwitcher){
this.colorSwitcher = colorSwitcher;
}

@Override
public Component getTableCellRendererComponent(JTable table,Object value, boolean isSelected, boolean hasFocus, int row,int column) {
Component superComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

if(showSelected && table.isCellSelected(row, column)){
superComponent.setBackground(Color.GREEN);
}
else if (table.isCellSelected(row, column)){
superComponent.setBackground(table.getSelectionBackground());
}
else {
superComponent.setBackground(table.getBackground());
}
return superComponent;
}
}
}

类 cellSelectionSet.java

  public class CellSelectionSet {
private List<Cell> cells = new ArrayList<>();

public void add(int r, int c) {
if (!contains(r, c)) {
cells.add(new Cell(r, c));
}
}

public boolean containsOneOrLess() {
return cells.size() <= 1;
}

public boolean contains(int r, int c) {
for (Cell cell : cells) {
if (cell.is(r, c)) {
return true;
}
}
return false;
}

public Cell getElementAt(int i){
return cells.get(i);
}

public int getSize(){
return this.cells.size();
}

public void clear() {
cells.clear();
System.out.println("CellSelectionSet cleared.");
}
}

类 Cell.java

public class Cell {
private int row, column;

public Cell(int row, int column){
this.row = row;
this.column = column;
}

public boolean is(int r, int c) {
return row == r && column == c;
}
}

最佳答案

主要问题:

table.setDefaultRenderer(Integer.class, myCellRenderer);

应该是

table.setDefaultRenderer(Object.class, myCellRenderer);

这将使您的细胞改变颜色。但是你会遇到一个问题,在你按下按钮之后,你选择的每个单元格都会自动改变颜色,因为 showSelected 属性设置为 true。谁知道呢,也许这就是你想要的。

另一个问题是单元格保持选择颜色。如果你的检查语句有问题

if(showSelected && table.isCellSelected(row, column)){

一旦您进行了更多选择,原来的选择将被清除。一个修复方法是检查 CellSelectionSet 以查看它是否包含单元格,同时检查它是否是新选择的。有点像

if (cellSelectionSet.contains(row, column)
&& !cellSelectionSet.getCellAt(row, column).isNewlySelected())

我添加了一个方法 getCellAt 到你的 CellSelectionSet

public Cell getCellAt(int row, int column) {
Cell c = null;
for (Cell cell : cells) {
if (cell.is(row, column)) {
c = cell;
}
}
return c;
}

还有 Cell 类中的一个标志,用于检查它是否是 newlySelected。默认为真

class Cell {

private boolean newlySelected = true;

public boolean isNewlySelected() {
return newlySelected;
}

public void setNewlySelected(boolean newlySelected) {
this.newlySelected = newlySelected;
}
}

当你第一次添加单元格时,它会被重新选中并且不会呈现不同的颜色,因为它没有通过检查

!cellSelectionSet.getCellAt(row, column).isNewlySelected()

但是当您按下按钮时,您将遍历列表并将所有单元格 newlySelected 设置为 false。

public void actionPerformed(ActionEvent e) {
//myCellRenderer.setShowSelected(true);
for (int i = 0; i < cellSelectionSet.getSize(); i++) {
cellSelectionSet.getElementAt(i).setNewlySelected(false);
}
table.repaint();
}

关于java - 当我单击按钮时 JTable 颜色单元格永久,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26163228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com