大家好,我没有发现下面的代码有什么不正确的地方。它可以很好地填充表格和标题。然而,当我改变第 1 行,第 1 列 to test然后单击它不会像我预期的那样为该行着色。然后单击我单击的那个会变成绿色,从那时起我单击桌子上的任何地方都会变成绿色。
该列不会改变1(公司)因为这将是要进行更改的默认列。这row是这里唯一的动态数字。
测试流程:
- 双击 IBM。
- type in test.
- 单击任何其他单元格以保存该单元格值。
- 该行未更改(第 1 行)。
- 再次单击测试单元。
- 所有行都将变为绿色。
预期流量:
- 双击 IBM。
- type in test.
- 单击任何其他单元格以保存该单元格值。
- 更改为的单元格test更改该行green.
- 点击Shares对于细胞(3, 3).
- 双击并更改4000 to 1000.
- 单击任何其他单元格以保存该单元格值。
- 更改为的单元格1000更改该行red.
填充表和标题:
单击并更改第 1 行,第 1 列价值test:
编辑后将该单元格单击到另一个单元格上:
现在,单击任何其他单元格(请注意测试行不是应有的绿色):
现在单击我编辑的单元格test:
你在上面看到它只是给我点击的每一行着色,不管我说的逻辑如何
if ("test".equals(type)) {....
java代码:
@SuppressWarnings("serial")
public class TableRowRenderingTip extends JPanel {
public TableRowRenderingTip() {
Object[] columnNames = {"Type", "Company", "Shares", "Price", "Boolean"};
Object[][] data =
{
{"Buy", "IBM", new Integer(1000), new Double(80.5), Boolean.TRUE},
{"Sell", "Dell", new Integer(2000), new Double(6.25), Boolean.FALSE},
{"Short Sell", "Apple", new Integer(3000), new Double(7.35), Boolean.TRUE},
{"Buy", "MicroSoft", new Integer(4000), new Double(27.50), Boolean.FALSE},
{"Short Sell", "Cisco", new Integer(5000), new Double(20), Boolean.TRUE}
};
DefaultTableModel model = new DefaultTableModel(data, columnNames) {
@SuppressWarnings({ "unchecked", "rawtypes" })
public Class getColumnClass(int column) {
return getValueAt(1, column).getClass();
}
};
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("Border", createBorder(model));
add(tabbedPane);
}
private JComponent createBorder(DefaultTableModel model) {
JTable table = new JTable(model) {
private Border outside = new MatteBorder(1, 0, 1, 0, Color.RED);
private Border _outside = new MatteBorder(1, 0, 1, 0, Color.GREEN);
private Border inside = new EmptyBorder(0, 1, 0, 1);
private Border highlight = new CompoundBorder(outside, inside);
private Border _highlight = new CompoundBorder(_outside, inside);
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
JComponent jc = (JComponent)c;
String type = (String)getModel().getValueAt(convertRowIndexToModel(row), 1);
if (isRowSelected(row)) {
if ("test".equals(type)) {
jc.setBorder( _highlight ); // Green color
jc.setBackground(Color.GREEN);
} else {
jc.setBorder( highlight ); //Red color
}
}
return c;
}
};
//table.setPreferredScrollableViewportSize(table.getPreferredSize());
//table.changeSelection(0, 0, false, false);
return new JScrollPane( table );
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI()
{
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Table Row Rendering");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new TableRowRenderingTip() );
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
因此,不用说,至少可以说,我有点沮丧,因为我已经为此工作了几个小时,试图找出可能出现的问题。
我确信我正在寻找的事情会很简单......