我想创建一个JTable和(在着色细胞漂亮Excel等)永久上色它的细胞通过一个按钮的点击。到目前为止,每一个细胞我选择,它会在ArrayList<Cell>
。JTable中色细胞永久当我点击按钮
首先,我要对列表中的每一个细胞,当我点击按钮(这是从代码的区别,我发现)被永久着色。我必须使用像这样的statememnt 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;
}
}
你救我!但现在我有另一个问题..选定的单元格会改变颜色,但是当我选择另一个单元格时,所有预先返回到白色(“未选中”)。你能告诉我,我怎么解决这个问题? – 2014-10-02 16:19:14
查看我的更新。我认为它的工作原理就像你想要的不是 – 2014-10-02 16:33:03
当我选择另一个单元格时,单元格仍然会返回“unselected”..我应该更改if中的标志if(cellSelectionSet.contains(row,column)&&!cellSelectionSet.getCellAt(row,column ).isNewlySelected())'语句? – 2014-10-02 16:46:37