问题描述
我有一个Jtable,它从一组值中填充。
我的代码是这样的:
I have a Jtable which gets populated from an array of values.My code is like this:
private static final String[] columnNames = {"Line Number", "Error","Fix Proposed","Percentage (%)"};
static DefaultTableModel model = new DefaultTableModel(null,columnNames);
public static void DisplayMyJList(List<CaptureErrors> x,String extension,
ArrayList<Integer> l,ArrayList<Integer> p,
ArrayList<String> e,ArrayList<String> s) throws IOException {//Method to Dynamic get values to be populated in Jtable.
String theExtension = extension;
if(FILE_EXTENSION.equals("java")) {
for(CaptureErrors ex: x) {
Vector row = new Vector();
row.add(ex.getLinenumber());
row.add(ex.getMyfounderror());
row.add(ex.getMycorrection());
row.add(ex.getMyPercentage()+"%");
model.addRow( row );
//model.setRowColour(1, Color.YELLOW);
}
}
table = new JTable(model);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setFillsViewportHeight(true);
table.setShowGrid(true);
table.setShowVerticalLines(true);
table.setGridColor(new Color(0,128,0));
JTableHeader header = table.getTableHeader();
table.setBackground(new Color(255,228,225));
table.setEnabled(true);
header.setFont(new Font("Dialog", Font.CENTER_BASELINE, 12));
header.setBackground(Color.black);
header.setForeground(Color.yellow);
JScrollPane pane4 = new JScrollPane(table);
我可以通过使用JButton从值数组中填充Jtable。 我想要一个条件,如果列百分比,获取此列中的所有值> 30,它会突出显示为color.red的行。
I can populate the Jtable from the array of values by Using a JButton. I want to have a condition where if column "percentage" ,get all values in this column >30, it highlights the rows to color.red.
我不想使用TableCellRendererComponent。我希望在单击Jbutton时执行此操作。
I dont want to user TableCellRendererComponent .I want this action to be performed upon clicking the Jbutton.
实际的Jtable如下所示:
The actual Jtable looks like this:
然后根据什么我想得到,前两行应以红色突出显示。任何帮助表示赞赏。
推荐答案
渲染API的一个问题是难以提供复合渲染器。有办法做,不要误会我的意思,但内置它会很好... [结束咆哮] ......
One of the problems with the render API is that it's difficult to provide compound renderers. There are ways to do, don't get me wrong, but it would have been nice to have it built in...[end rant]...
基本的想法是,您想要设置一系列渲染器,这些渲染器从基础渲染器扩展而来,该渲染器包含确定在所需条件下应该执行的操作所需的逻辑。
The basic idea is you you want to set up a series of renderers that extend from a base renderer which contains the logic required to determine what it should do under the required conditions.
public class FilterRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
Double percent = (Double) table.getValueAt(row, 3);
// You'll need some way to supply the filter value, may via a centralised
// manager of some kind.
if (percent > 0.3 && !isSelected) {
setOpaque(true);
setBackground(Color.RED);
} else {
setOpaque(false);
}
return this;
}
}
public class OtherCellRenderer extends FilterRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
// Apply any special renderer requirements, like translating an object value to String
return this;
}
}
每列需要一个自定义渲染器(从你的例子,那是4)并将每个应用到表列
You'll need a custom renderer for each column (from you example, that's 4) and apply each to the table column
TableColumnModel model = table.getColumnModel();
model.getColumn(0).setCellRenderer(new LineNumberRenderer());
model.getColumn(1).setCellRenderer(new ErrorRenederer());
model.getColumn(2).setCellRenderer(new FixProposedRenderer());
model.getColumn(3).setCellRenderer(new Percentage());
或者你可以使用SwingLabs JXTable
内置支持行荧光笔
Or you could just use SwingLabs JXTable
which has inbuilt support for row highlighters
这篇关于如何更改具有特定列值的JTable整行的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!