使用SWINGX的Highlighter对表格设置背景色和前景色
注:最近开始学习swing编程,使用到了swingx,但是网上相关资料比较少,然后做了一些例子。
本例主要是对表格中的单元格动态设置背景色,
但是完成后发现再根据列排序时,相应的单元格背景色没有随之移动,希望牛人给些建议!
首先奉上截图效果:

那么,通过右键可以设置表格背景色。
例子源码结构:
- AuditTableColumnsHighlighter
- AuditTableCellsHighlighter
- AuditTableRowsHighlighter
这三个主要负责改变背景色或者前景色。
- DefaultJXTable
这个继承自JXTable,并添加了右键菜单选项响应事件。
- TestFrame
测试类。
一下是部分代码:
AuditTableCellsHighlighter.class
public class AuditTableCellsHighlighter extends AbstractHighlighter {
private int [] rows;
private int [] columns;
private boolean bg_fg = true;
private Color selectedColor;
private JXTable table;
public AuditTableCellsHighlighter(JXTable table, Color selectedColor){
this.rows = table.getSelectedRows();
this.columns = table.getSelectedColumns();
this.selectedColor = selectedColor;
this.table = table;
}
public AuditTableCellsHighlighter(JXTable table, Color selectedColor, boolean bg_fg){
this.rows = table.getSelectedRows();
this.columns = table.getSelectedColumns();
this.bg_fg = bg_fg;
this.selectedColor = selectedColor;
this.table = table;
}
@Override
protected boolean canHighlight(Component component, ComponentAdapter adapter) {
if(adapter.getComponent() instanceof DefaultJXTable ){
return true;
}else{
return false;
}
}
@Override
protected Component doHighlight(Component renderer, ComponentAdapter adapter) {
if(inArray(rows, adapter.row) && inArray(columns, adapter.column)){
boolean b = bg_fg == true ? applyBackground(renderer, adapter) : applyForeground(renderer, adapter);
}
return renderer;
}
protected boolean applyBackground(Component renderer, ComponentAdapter adapter) {
if (selectedColor != null) {
renderer.setBackground(selectedColor);
}
return true;
}
protected boolean applyForeground(Component renderer, ComponentAdapter adapter) {
if (selectedColor != null) {
renderer.setForeground(selectedColor);
}
return true;
}
protected boolean inArray(int [] array, int value){
for (int i : array) {
if(i == value)
return true;
}
return false;
}
}
public class DefaultJXTable extends JXTable{
private JPopupMenu tablePopupMenu;
private boolean CELLS_BG = true;
private boolean CELLS_FG = false;
private boolean ROWS_BG = true;
private boolean ROWS_FG = false;
private boolean COLUMNS_BG = true;
private boolean COLUMNS_FG = false;
public DefaultJXTable() {
super();
createPopuMenu();
}
protected void addColumnModelAdapter(){
getColumnModel().addColumnModelListener(new TableColumnModelAdapter(this));
}
protected void createPopuMenu(){
tablePopupMenu = new JPopupMenu();
JMenuItem cellsbgHithlighter = new JMenuItem("设置单元格背景色");
cellsbgHithlighter.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addHighlighter(new AuditTableCellsHighlighter(DefaultJXTable.this, ColorUtil.showColorChooser(DefaultJXTable.this), CELLS_BG));
}
});
tablePopupMenu.add(cellsbgHithlighter);
JMenuItem cellsfgHithlighter = new JMenuItem("设置单元格前景色");
cellsfgHithlighter.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addHighlighter(new AuditTableCellsHighlighter(DefaultJXTable.this, ColorUtil.showColorChooser(DefaultJXTable.this), CELLS_FG));
}
});
tablePopupMenu.add(cellsfgHithlighter);
JMenuItem rowsbgHithlighter = new JMenuItem("设置行背景色");
rowsbgHithlighter.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addHighlighter(new AuditTableRowsHighlighter(DefaultJXTable.this, ColorUtil.showColorChooser(DefaultJXTable.this), ROWS_BG));
}
});
tablePopupMenu.add(rowsbgHithlighter);
JMenuItem rowsfgHithlighter = new JMenuItem("设置行前景色");
rowsfgHithlighter.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addHighlighter(new AuditTableRowsHighlighter(DefaultJXTable.this, ColorUtil.showColorChooser(DefaultJXTable.this), ROWS_FG));
}
});
tablePopupMenu.add(rowsfgHithlighter);
JMenuItem columnsbgHithlighter = new JMenuItem("设置列背景色");
columnsbgHithlighter.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addHighlighter(new AuditTableColumnsHighlighter(DefaultJXTable.this, ColorUtil.showColorChooser(DefaultJXTable.this), COLUMNS_BG));
}
});
tablePopupMenu.add(columnsbgHithlighter);
JMenuItem columnsfgHithlighter = new JMenuItem("设置列前景色");
columnsfgHithlighter.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addHighlighter(new AuditTableColumnsHighlighter(DefaultJXTable.this, ColorUtil.showColorChooser(DefaultJXTable.this), COLUMNS_FG));
}
});
tablePopupMenu.add(columnsfgHithlighter);
JMenuItem repaint = new JMenuItem("repaint");
repaint.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DefaultJXTable.this.repaint();
}
});
tablePopupMenu.add(repaint);
addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
tablePopupMenu.show(DefaultJXTable.this, e.getX(), e.getY());
}
}
});
}
}
TestFrame.class
public class TestFrame implements Runnable {
private static final String NAMES = "Name";
private static final String CITY = "City";
public static void main(String[] args) {
SwingUtilities.invokeLater(new TestFrame());
}
public void run() {
JFrame frame = new JFrame("View Demo");
frame.setLayout(new BorderLayout());
DefaultJXTable table = new DefaultJXTable();
table.setModel(new RandomTableModel(20));
frame.add(new JScrollPane(table));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(JOptionPane.getRootFrame());
frame.setVisible(true);
}
private static class RandomTableModel extends AbstractTableModel {
private static String[] contactValues = {
"Jeanette", "Kirill", "Bloid", "Alamray", "Romain"};
private static String[] cityValues = {
"Paris", "Lyon", "Marseille", "Avignon", "Nice", "Cannes"
};
private static String getRandomName() {
int index = (int) (Math.floor(Math.random() * contactValues.length) % contactValues.length);
return contactValues[index];
}
private static String getRandomCity() {
int index = (int) (Math.floor(Math.random() * cityValues.length) % cityValues.length);
return cityValues[index];
}
private int _rowCount;
private Object[][] _data;
RandomTableModel(int rowCount) {
_rowCount = rowCount;
_data = new Object[getColumnCount()][_rowCount];
for (int i = 0; i < _data.length; i++) {
for (int j = 0; j < _data[i].length; j++) {
if (getColumnClass(i) == String.class) {
_data[i][j] = i == 0 ? getRandomName() : getRandomCity();
} else {
_data[i][j] = new Double(Math.random() * 100);
}
}
}
}
@Override
public Class<?> getColumnClass(int columnIndex) {
return (columnIndex % 2 == 0) ? String.class : Double.class;
}
public int getColumnCount() {
return 4;
}
public int getRowCount() {
return _rowCount;
}
public Object getValueAt(int rowIndex, int columnIndex) {
return _data[columnIndex][rowIndex];
}
@Override
public String getColumnName(int column) {
switch (column) {
case 0:
return NAMES;
case 1:
return "Number 1";
case 2:
return CITY;
case 3:
return "Number 2";
default:
break;
}
return "Column " + column;
}
}
}
这样,主要的代码就可以看出具体的实现过程了。

浙公网安备 33010602011771号