Swing 基于列值的JTable行颜色更改-在弹出窗口中单击
我的Swing 基于列值的JTable行颜色更改-在弹出窗口中单击,swing,jtable,tablemodel,tablecellrenderer,Swing,Jtable,Tablemodel,Tablecellrenderer,我的jTable加载了数据,这就是我在jTable上调用弹出功能的地方 jTable.addMouseListener(new TablePopupListener(jTable)); displayTable(); 因此,基本上,如果我右键单击一行,就会出现一个弹出窗口(信用检查),如果我单击它,就会为该行的最后一个单元格设置一个值。现在,基于这个列单元格值,我必须定义一行的颜色。假设如果单元格值失败,则将行变为红色,否则变为绿色。我尝试了customCellRenderer并定义了我的条件
jTable
加载了数据,这就是我在jTable
上调用弹出功能的地方
jTable.addMouseListener(new TablePopupListener(jTable));
displayTable();
因此,基本上,如果我右键单击一行,就会出现一个弹出窗口(信用检查),如果我单击它,就会为该行的最后一个单元格设置一个值。现在,基于这个列单元格值,我必须定义一行的颜色。假设如果单元格值失败,则将行变为红色,否则变为绿色。我尝试了customCellRenderer
并定义了我的条件,但行颜色没有变化。自定义单元格渲染器对于我必须编写的按钮功能非常有用。下面的代码使用preparecellRenderer
,我觉得这很简单,但我看不到行颜色有任何变化
我错过了一些连接,请提供帮助
提前谢谢
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;
}
};
}
}
}
类TablePopupListener扩展了MouseAdapter实现ActionListener{
jpopmpmenu弹出窗口;
JTable表;
int[]行;
表格模型;
ArrayList rowValueList=新建ArrayList();
JMenuItem creditCheck=新的JMenuItem(“信用检查”);
公共表复制器(JTable JTable){
this.table=jTable;
model=table.getModel();
popup=new JPopupMenu();
JMenuItem creditCheck=新的JMenuItem(“信用检查”);
creditCheck.addActionListener(这个);
弹出。添加(信用检查);
}
public void mousePressed(MouseEvent me){
壁炉;
}
公共无效MouseEvent me(MouseEvent me){
壁炉;
}
公共真空壁炉(MouseeEvent me){
/*
*只有在列表中有行选择时,才会显示弹出菜单
*桌子
*/
//show(table,me.getX(),me.getY());
如果(me.isPopupTrigger()&&table.getModel().getRowCount()!=0
&&table.getSelectedRow()!=-1){
//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());
如果(!source.isRowSelected(行))
source.changeSelection(行、列、假、假);
show(table,me.getX(),me.getY());
}
}
}
已执行的公共无效行动(行动事件ae){
如果(ae.getActionCommand().equals(“信用检查”)){
System.out.println(“您已单击creditCheckpopup”);
selRows=table.getSelectedRows();
如果(selRows.length>0){
for(int i=0;i
经过一些格式化(相信我,代码的可读性很重要;-)之后,您似乎在弹出菜单中实例化了一个新表,只有该表具有自定义呈现程序。你可以这样做,但对你真正的桌子没有任何影响
将prepareRenderer移动到实际表中(作为参数传递到弹出窗口中的表),您应该会看到颜色。注意:由于DefaultTableCellRenderer中有一个bug,您必须始终设置颜色,即
if (nothingToDo) {
setBackground(normal)
} else if ... {
setBackground(one)
} else {
setBackground(other)
}
编辑:试图解释代码结构的变化,伪代码片段
当前状态,这就是您正在做的:
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(...
}
换成,这就是你应该做的:
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
编辑2:
-更改了伪代码以实际注册侦听器)
-addMouseListener下面缩进的代码表示内部代码的轮廓