我已经尝试了一段时间,并通过互联网寻找解决方案但是我失败了。 我想要做的是动态更改Jtable的行背景。 我创建了arrayList,它保留了所选行的数量(每次用户按alt +单击一个单元格时添加它们) 并在我自己的tableCellRenderer中添加了
for(Integer c: leftSelectedCells){
if(c.equals(row)){comp.setForeground(Color.red); }
else { comp.setForeground(Color.black);}
}
它正在工作,对于很少的单元格,或者在选定的列恢复到它们的原始颜色之后的某个时间,我已经检查过,这些int仍然在Array中,这不是问题,任何想法可能导致问题?< / p>
答案 0 :(得分:2)
正如上面评论中已经提到的,您需要为所有必需的列提供自定义渲染器。作为替代方法,您可以覆盖JTable.prepareRenderer
以根据受影响的行列表设置背景。 @camickr Table Row Rendering解释了这种方法。下面是一个示例,突出显示使用鼠标+ Alt 键单击的行。为简单起见,突出显示的行列表保留为客户端属性。
import java.awt.Color;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
public class TableHighlight {
TableHighlight() {
JFrame frame = new JFrame("TableHighlight");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Object[][] data = { { "Column 1", "Column 2" },
{ "Column 1", "Column 2" }, { "Column 1", "Column 2" },
{ "Column 1", "Column 2" } };
Object[] columnNames = { "Column 1", "Column 2" };
DefaultTableModel model = new DefaultTableModel(data, columnNames);
final JTable table = new JTable(model) {
@Override
public Component prepareRenderer(TableCellRenderer renderer,
int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
List<Integer> selectedRows = (List<Integer>) getClientProperty("highlightRows");
c.setBackground(selectedRows.contains(row) ? Color.cyan : getBackground());
return c;
}
};
table.putClientProperty("highlightRows", new ArrayList<Integer>());
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent evt) {
if (!evt.isAltDown())
return;
int row = table.rowAtPoint(evt.getPoint());
if (row == -1)
return;
List<Integer> selectedRows = (List<Integer>) table
.getClientProperty("highlightRows");
int index = selectedRows.indexOf(row);
if (index != -1)
selectedRows.remove(index);
else
selectedRows.add(row);
table.repaint();
}
});
frame.add(new JScrollPane(table));
frame.setLocationByPlatform(true);
frame.pack();
frame.setVisible(true);
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TableHighlight();
}
});
}
}
答案 1 :(得分:1)
我建议使用CTRL进行多选而不是ALT。首先,任何想要多选的用户都已经知道如何操作,因为CTRL是多选的事实上的标准修改键。其次,你可以免费获得你想要的行为,而不必实现任何特殊的行为(可能除了你喜欢的颜色方案)。
package com.example.table.multiselect;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
public class MultiRowTableSelect extends JFrame {
public MultiRowTableSelect() {
DefaultTableModel model = new DefaultTableModel(0, 3);
model.addRow(new Integer[]{1, 2, 3});
model.addRow(new Integer[]{4, 5, 6});
model.addRow(new Integer[]{7, 8, 9});
model.addRow(new Integer[]{10, 11, 12});
JTable tbl = new JTable(model);
tbl.setRowSelectionAllowed(true); // when you click a cell, the entire row will be highlighted
/* Assuming you want all cells to be rendered using the custom renderer
* unless otherwise overridden, you can simply set the JTable's default
* renderer. The implementation is so simple here that I'm just
* creating an anonymous subclass of DefaultTableCellRenderer.
*/
tbl.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (isSelected) {
c.setForeground(Color.RED); // you can set the foreground and/or background here
}
return c;
}
});
add(new JScrollPane(tbl), BorderLayout.CENTER);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationByPlatform(true);
pack();
setSize(400, 200);
setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
new MultiRowTableSelect();
}
}