我有一个JTable
,它将它们的值通过mysql数据库。我制作了几个自定义列表渲染器,以便JTable
能够根据需要显示格式化的数据。我现在需要的是在某些行上添加删除线,具体取决于每行最后一个单元格(5)的值是否具有特定的字符串值,例如(test)。穿透必须位于除最后一个单元格(具有测试值)之外的每个单元格中。它可以与其他自定义列渲染器一起工作而不会混淆任何东西吗?jtable中整行的删除线
谢谢!
我有一个JTable
,它将它们的值通过mysql数据库。我制作了几个自定义列表渲染器,以便JTable
能够根据需要显示格式化的数据。我现在需要的是在某些行上添加删除线,具体取决于每行最后一个单元格(5)的值是否具有特定的字符串值,例如(test)。穿透必须位于除最后一个单元格(具有测试值)之外的每个单元格中。它可以与其他自定义列渲染器一起工作而不会混淆任何东西吗?jtable中整行的删除线
谢谢!
为什么HTML格式,为什么不使用TextAttribute直接
与所有尊重HFOE,他的知识共享(forums.sun.com)
from代码
import java.awt.*;
import java.awt.font.TextAttribute;
import java.util.Map;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.MatteBorder;
import javax.swing.table.*;
public class TablePrepareRenderer extends JFrame {
private static final long serialVersionUID = 1L;
private JTable table;
public TablePrepareRenderer() {
Object[] columnNames = {"Type", "Company", "Shares", "Price", "Boolean"};
Object[][] data = {
{"Buy", "IBM", new Integer(1000), new Double(80.50), false},
{"Sell", "MicroSoft", new Integer(2000), new Double(6.25), true},
{"Sell", "Apple", new Integer(3000), new Double(7.35), true},
{"Buy", "Nortel", new Integer(4000), new Double(20.00), false}
};
DefaultTableModel model = new DefaultTableModel(data, columnNames) {
private static final long serialVersionUID = 1L;
@Override
public Class getColumnClass(int column) {
return getValueAt(0, column).getClass();
}
};
table = new JTable(model) {
private static final long serialVersionUID = 1L;
private Border outside = new MatteBorder(1, 0, 1, 0, Color.red);
private Border inside = new EmptyBorder(0, 1, 0, 1);
private Border highlight = new CompoundBorder(outside, inside);
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component comp = super.prepareRenderer(renderer, row, column);
JComponent jc = (JComponent) comp;
Map attributes = (new Font("Serif", Font.PLAIN, 12)).getAttributes();
//attributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
attributes.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
if (!isRowSelected(row)) {
comp.setForeground(Color.black);
comp.setBackground(row % 2 == 0 ? Color.white : Color.orange);
int modelRow = convertRowIndexToModel(row);
String type = (String) getModel().getValueAt(modelRow, 0);
if (type.equals("Sell")) {
comp.setFont(new Font(attributes));
comp.setForeground(Color.red);
} else {
comp.setFont(new Font("Serif", Font.BOLD, 12));
}
} else {
comp.setFont(table.getFont());
}
jc.setBorder(BorderFactory.createCompoundBorder(jc.getBorder(), BorderFactory.createEmptyBorder(0, 0, 0, 5)));
return comp;
}
};
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane(table);
getContentPane().add(scrollPane);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TablePrepareRenderer frame = new TablePrepareRenderer();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
这里是你想要做的部分例子,
How to strikethrough text of a selected row(having checkbox in first column) of JTable?
请看看代码,它应该解决您的问题。
是的,它可以处理你写的任何列渲染器。它基本上取决于你如何写它。 – 2012-04-23 12:09:30
在互联网上搜索我发现只有单元渲染或整列的实例,没有行。任何可用的例子? – Vagelism 2012-04-23 12:12:50