gpt4 book ai didi

java - JTable valuechanged 然后更改单元格颜色

转载 作者:行者123 更新时间:2023-11-29 03:36:27 26 4
gpt4 key购买 nike

我这里有一个包含两 (2) 列的 JTable。右栏是可编辑的,而另一栏是不可编辑的。

所以,我的问题是,每当用户更改单元格的值时,该特定单元格将更改其单元格颜色。

我想这样做是因为我想让用户知道他/她对表格做了一些更改。

我在某个地方找到了这个,它以某种方式解决了我的问题,但有一点没有达到我的预期,即在更改值并单击另一个单元格后,颜色会变回其原始颜色。我想让它留下来直到它被保存。

@Override
public Component prepareEditor(TableCellEditor editor, int data, int columns) {
Component c = super.prepareEditor(editor, data, columns);
c.setBackground(Color.RED);
return c;
}

这可能吗?如果是,请举出一些例子。

更新:

    String[] columnname = {"Student Name", "Grade"};

Object[][] data = {};

gradetable = new JTable(data, columnname){
private Object[][] rowData;

public boolean isCellEditable(int data, int columns){
return columns == 1;
}


public Component prepareRenderer(TableCellRenderer r, int data, int columns){
final Component c = super.prepareRenderer(r, data, columns);

if (data % 2 == 0){
c.setBackground(Color.LIGHT_GRAY);
}
else{
c.setBackground(Color.WHITE);
}

if (isCellSelected(data, columns)){
c.setBackground(Color.ORANGE);
}

return c;
}

@Override
public Component prepareEditor(TableCellEditor editor, int data, int columns) {
Component c = super.prepareEditor(editor, data, columns);
c.setBackground(Color.RED);
return c;
}
};

gradetable.setModel(new DefaultTableModel(data, columnname));
gradetable.setPreferredScrollableViewportSize(new Dimension (350, 130));
gradetable.setFillsViewportHeight(true);
gradetable.getTableHeader().setReorderingAllowed(false);
gradetable.setGridColor(new Color(128,128,128,128));
JScrollPane jsp = new JScrollPane(gradetable);
panel3.add(jsp);

最佳答案

表格使用 TableCellRenderer 在屏幕上绘制值。编辑器和渲染器实际上彼此没有任何关系(从绘画的角度来看)。

因此,一旦编辑器被关闭(接受或取消),单元格就会使用分配的 TableCellRenderer

重新绘制

您需要在您的表模型中提供一些方法来确定哪些行已更新并更改渲染器的状态以匹配。

仅供引用- DefaultTableCellRenderer 使用 JLabel 作为基础渲染器,因此默认情况下它是透明的;您需要使其不透明才能正确呈现。

查看 Using custom renderers了解更多详情

用例子更新

这只不过是一个概念证明。它不会满足您的绝对要求,您应该认真阅读上面链接的教程。

Example image

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
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.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

public class TableEdit {

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

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

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

public class TestPane extends JPanel {

public TestPane() {
setLayout(new BorderLayout());
JTable table = new JTable(new MyTableModel());
table.setSurrendersFocusOnKeystroke(true);
TableColumnModel model = table.getColumnModel();
model.getColumn(1).setCellRenderer(new MyTableCellRenderer());
add(new JScrollPane(table));
}

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

public class MyData {

private String key;
private String value;
private boolean changed;

public MyData(String key, String value) {
this.key = key;
this.value = value;
this.changed = false;
}

public String getKey() {
return key;
}

public String getValue() {
return value;
}

public void setValue(String newValue) {
if (value == null ? newValue != null : !value.equals(newValue)) {
value = newValue;
changed = true;
}
}

public boolean hasChanged() {
return changed;
}
}

public class MyTableModel extends AbstractTableModel {

private List<MyData> data;

public MyTableModel() {
data = new ArrayList<>(25);
for (int index = 0; index < 5; index++) {
data.add(new MyData("A" + (index + 1), "B" + (index + 1)));
}
}

@Override
public int getRowCount() {
return data.size();
}

@Override
public int getColumnCount() {
return 2;
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
MyData myData = data.get(rowIndex);
Object value = null;
switch (columnIndex) {
case 0:
value = myData.getKey();
break;
case 1:
value = myData.getValue();
break;
}
return value;
}

@Override
public Class<?> getColumnClass(int columnIndex) {
return String.class;
}

@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex == 1;
}

public boolean hasChanged(int rowIndex) {
MyData myData = data.get(rowIndex);
return myData.hasChanged();
}

@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
MyData myData = data.get(rowIndex);
myData.setValue(aValue == null ? null : aValue.toString());
}
}

public class MyTableCellRenderer extends DefaultTableCellRenderer {

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
setOpaque(isSelected);
TableModel model = table.getModel();
if (model instanceof MyTableModel) {
MyTableModel myModel = (MyTableModel) model;
if (myModel.hasChanged(row)) {
if (!isSelected) {
setBackground(Color.RED);
setOpaque(true);
}
}
}
return this;
}
}
}

关于java - JTable valuechanged 然后更改单元格颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15286929/

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