JTable根据列值更改行颜色-弹出窗口中单击 [英] JTable row color change based on a column value- on pop up click
问题描述
我的jTable
装有数据,这就是我在jTable
上调用弹出功能的地方.
My jTable
is loaded with data and this is where I call my Pop up functionality on jTable
.
jTable.addMouseListener(new TablePopupListener(jTable));
displayTable();
因此,基本上,如果我右键单击一行,则会出现一个弹出窗口(信用检查),如果单击它,则将值设置为该行的最后一个单元格.现在,基于此列单元格值,我必须定义行的颜色.假设单元格值失败,则将该行变为红色,否则变为绿色.我已经尝试过customCellRenderer
并定义了我的条件,但是行颜色没有变化.不过,自定义单元格渲染器非常适合我必须编写的按钮功能.下面的代码使用prepare cellRenderer
,我觉得很简单,但是行颜色没有任何变化.
So basically, if I right-click a row, a popup(credit check) comes up and if I click it is setting a value to the last cell in that row. Now, based on this column cell value I have to define the color of a row. Let's say if the cell value fails then turn the row to red else to green. I have tried customCellRenderer
and defined my condition but there is no change in row color. The custom cell renderer worked great for a button functionality that I had to write, though. The below code uses prepare cellRenderer
which I felt is easy but I don't see any change in row color.
我缺少联系,请帮忙.
谢谢.
class TablePopupListener extends MouseAdapter implements ActionListener {
JPopupMenu popup;
JTable table;
int[] selRows;
TableModel model;
ArrayList rowValueList = new ArrayList();
JMenuItem creditCheck = new JMenuItem("Credit Check");
public TablePopupListener(JTable jTable) {
this.table = jTable;
model = table.getModel();
popup = new JPopupMenu();
JMenuItem creditCheck = new JMenuItem("Credit Check");
creditCheck.addActionListener(this);
popup.add(creditCheck);
}
public void mousePressed(MouseEvent me) {
firePopup(me);
}
public void mouseReleased(MouseEvent me) {
firePopup(me);
}
public void firePopup(MouseEvent me) {
/*
* The popup menu will be shown only if there is a row selection in the
* table
*/
// popup.show(table, me.getX(), me.getY());
if (me.isPopupTrigger() && table.getModel().getRowCount() != 0
&& table.getSelectedRow() != -1) {
// popup.show(table,me.getX(),me.getY());
if (me.isPopupTrigger()) {
JTable source = (JTable) me.getSource();
int row = source.rowAtPoint(me.getPoint());
int column = source.columnAtPoint(me.getPoint());
if (!source.isRowSelected(row))
source.changeSelection(row, column, false, false);
popup.show(table, me.getX(), me.getY());
}
}
}
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals("Credit Check")) {
System.out.println("you have clicked creditCheckpopup");
selRows = table.getSelectedRows();
if (selRows.length > 0) {
for (int i = 0; i < selRows.length; i++) {
// get Table data
for (int j = 1; j < (table.getColumnCount()) - 1; j++) {
rowValueList.add(model.getValueAt(selRows[i], j));
}
System.out.println("Selection : " + rowValueList);
}
} else {
System.out.println("you have clicked something idiot");
}
int result = new COpxDeal(rowValueList).CheckCredit();
if (result == 1)
rowValueList.add("pass");
else
rowValueList.add("fail");
String aValue = (String) rowValueList.get(14);
for (int i = 0; i < selRows.length; i++) {
model.setValueAt(aValue, selRows[i], 15);
}
// inserted comment (Kleopatra): where are we? that's outside of the TablePopup?
// okay, nothing like copying the code into an IDE and let that do the formatting, silly me ;-)
// this is indeed _inside_ the popup, that is the table is recreated
table = new JTable(model) {
public Component prepareRenderer(TableCellRenderer renderer,
int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
JComponent jc = (JComponent) c;
// if (!isRowSelected(row)){
// c.setBackground(getBackground());
// System.out.println(isRowSelected(row));
// }
int modelRow = convertRowIndexToModel(row);
String strTestValue = "fail";
String strTblValue = (String) getModel().getValueAt(
modelRow, 15);
System.out.println("result :" + strTblValue);
if (strTblValue == null || strTblValue.equals(""))
System.out.println("there is nothing in strTblValue");
else if (strTestValue.equals(strTblValue)) {
jc.setBackground(Color.RED);
} else {
jc.setBackground(Color.green);
}
return c;
}
};
}
}
}
推荐答案
经过某种格式设置(相信我,对代码而言,可读性很重要;-)似乎就像您在popupMenu内实例化了一个新表,并且只有该表具有自定义渲染器.您可以执行此操作,但对您的实际表没有任何影响.
after some formatting (believe me, it's important for code to be readable ;-) seems like you instantiate a new table inside your popupMenu and only that table has the custom renderer. Which you can do, but doesn't have any effect on the your real table.
将prepareRenderer移到您的真实表中(您将其作为参数传递到弹出窗口中),您应该会看到颜色.注意:由于DefaultTableCellRenderer中的错误,您必须始终设置颜色,即
Move the prepareRenderer into your real table (the one you pass into the popup as parameter) and you should see the coloring. Beware: due to a bug in DefaultTableCellRenderer, you have to set the color always, that is
if (nothingToDo) {
setBackground(normal)
} else if ... {
setBackground(one)
} else {
setBackground(other)
}
尝试解释代码结构,伪代码段的变化
trying to explain the changes in code structure, pseudo-code snippets
当前状态,这就是您正在做的事情:
Current state, that's what you are doing:
JTable table = new JTable();
table.addMouseListener(new TablePopupListener(table));
// keep listener-local reference to table
JTable table = table;
....
// in the listener guts, the reference is replaced
table = new JTable() {
@Override
Component prepareRenderer(...
}
更改为,这就是您应该做的:
Change to, that's what you should do:
JTable table = new JTable() {
@Override
Component prepareRenderer(...
};
table.addMouseListener(new TablePopupListener(table));
// keep listener-local reference to table
JTable table = table;
// don't replace ever, it's for reading only
-更改了伪代码以实际注册侦听器) -在addMouseListener下方缩进的代码是作为TablePopupListener
edit 2: - changed the pseudo-code to actually register the listener) - the code indented below the addMouseListener is mean as an outline of the code inside the TablePopupListener
这篇关于JTable根据列值更改行颜色-弹出窗口中单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!