java 着色 jTable 行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11135695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Coloring jTable row
提问by Luna
I want to color specific rows in jTable..i did it for columns by using this code,
我想为 jTable 中的特定行着色..我使用此代码为列做了它,
private class CustomCellRenderer extends DefaultTableCellRenderer {
/* (non-Javadoc)
* @see
javax.swing.table.DefaultTableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, java.lang.Object, boolean, boolean, int, int)
*/
@Override
public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus, int row, int column) {
Component rendererComp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus,row, column);
//Set foreground color
// rendererComp.setForeground(Color.red);
//Set background color
rendererComp .setBackground(Color.pink);
return rendererComp ;
}
}
And i call the above code using,
我调用上面的代码使用,
jTable1.getColumnModel().getColumn(3).setCellRenderer(new CustomCellRenderer());
But i want to do the same for rows in jTable..There's no getColumnModel() or getColumn() in the case of rows..So what's the alternate way for doing that? I am doing it in Netbeans by using Java Swing..
但是我想对 jTable 中的行做同样的事情..在行的情况下没有 getColumnModel() 或 getColumn() ..那么这样做的替代方法是什么?我在 Netbeans 中使用 Java Swing 做这件事。
回答by Guillaume Polet
Here is an example on how you can combine both column colors and row color. You basically perform tests in the TableCellRenderer to see if the background should be of one color or another.
这是一个关于如何组合列颜色和行颜色的示例。您基本上在 TableCellRenderer 中执行测试以查看背景是否应该是一种颜色或另一种颜色。
import java.awt.Color;
import java.awt.Component;
import java.util.Enumeration;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
public class TestTable {
public class MyTableCellRenderer extends DefaultTableCellRenderer implements TableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
setBackground(null);
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
setText(String.valueOf(value));
boolean interestingRow = row % 5 == 2;
boolean secondColumn = column == 1;
if (interestingRow && secondColumn) {
setBackground(Color.ORANGE);
} else if (interestingRow) {
setBackground(Color.YELLOW);
} else if (secondColumn) {
setBackground(Color.RED);
}
return this;
}
}
private JFrame f;
private JTable table;
protected void initUI() {
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
Vector<String> columNames = new Vector<String>();
columNames.add("Col 0");
columNames.add("Col 1");
columNames.add("Col 2");
for (int i = 0; i < 20; i++) {
Vector<Object> v = new Vector<Object>();
v.add(i % 3 == 0 ? "Hello" : "World");
v.add("Some data in row " + (i + 1));
v.add("Some other data in row " + (i + 1));
data.add(v);
}
table = new JTable(new DefaultTableModel(data, columNames));
Enumeration<TableColumn> en = table.getColumnModel().getColumns();
while (en.hasMoreElements()) {
TableColumn tc = en.nextElement();
tc.setCellRenderer(new MyTableCellRenderer());
}
f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.add(new JScrollPane(table));
f.pack();
f.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestTable().initUI();
}
});
}
}
回答by trashgod
回答by vedant
This seems like a very dirty way of handling it. It'll be a much better idea if you use a gridLayout using the layoutManager for you containter (i think it must be a JFrame). U can add individual componets (JPanels, Jbuttons, or any other JComponent) and handle their look by using the paint()/repaint() methods.
这似乎是一种非常肮脏的处理方式。如果您使用 layoutManager 为您的容器使用 gridLayout 将是一个更好的主意(我认为它必须是一个 JFrame)。您可以添加单独的组件(JPanels、Jbuttons 或任何其他 JComponent)并使用paint()/repaint() 方法处理它们的外观。
EDIT
编辑
ORyou can change the the getTableCellRendererComponent(....) method to set your custom background colors using nested if-else statements or switch-case according to the int rows ,int columns (that are provided as arguments).
或者,您可以更改 getTableCellRendererComponent(....) 方法以根据 int 行、int 列(作为参数提供)使用嵌套的 if-else 语句或 switch-case 来设置自定义背景颜色。
This will be much easier
这会容易得多