Java Oracle TableRenderDemo中的单元格/行格式
好的,首先让我说,在几年之后,我将回到编程领域。与此同时,情况发生了很大变化(IDE、OO),我第一次开始用Java编程 我需要构建一个GUI前端,目前正在努力使用JTables、tableModels和Renders 我目前正在通过Oracle TableRenderDemo努力了解它到底在做什么,我有点卡住了 我想我可能会增加这个帖子,但现在 我不明白的是,当第一次运行时,为什么John和Jane的整行都是绿色的,但是当我为他们中的任何一个单击素食者单击框时,只有组合框单元格会改变颜色,而不是整行 请记住,我对所有这些Java东西都是新手,可能需要一个非常简单的解释 非常感谢, 加里 这是密码。。。(谢谢) 包com.novartis.adhoc.viewJava Oracle TableRenderDemo中的单元格/行格式,java,swing,tablecellrenderer,Java,Swing,Tablecellrenderer,好的,首先让我说,在几年之后,我将回到编程领域。与此同时,情况发生了很大变化(IDE、OO),我第一次开始用Java编程 我需要构建一个GUI前端,目前正在努力使用JTables、tableModels和Renders 我目前正在通过Oracle TableRenderDemo努力了解它到底在做什么,我有点卡住了 我想我可能会增加这个帖子,但现在 我不明白的是,当第一次运行时,为什么John和Jane的整行都是绿色的,但是当我为他们中的任何一个单击素食者单击框时,只有组合框单元格会改变颜色,而不
/*
* TableRenderDemo.java requires no other files.
*/
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
/**
* TableRenderDemo is just like TableDemo, except that it explicitly initializes
* column sizes and it uses a combo box as an editor for the Sport column.
*/
public class TableRenderDemo extends JPanel {
private static final long serialVersionUID = 1L;
private boolean DEBUG = false;
public TableRenderDemo() {
super(new GridLayout(1, 0));
JTable table = new JTable(new MyTableModel())
{
private static final long serialVersionUID = 1L;
public Component prepareRenderer(TableCellRenderer renderer,
int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
int modelRow = convertRowIndexToModel(row);
// Color row based on a cell value
if ((Boolean) getModel().getValueAt(modelRow, 4)) {
c.setBackground(Color.GREEN);
} else {
c.setBackground(getBackground());
}
return c;
}
};
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
// Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
// Set up column sizes.
initColumnSizes(table);
// Fiddle with the Sport column's cell editors/renderers.
setUpSportColumn(table, table.getColumnModel().getColumn(2));
// Add the scroll pane to this panel.
add(scrollPane);
}
/*
* This method picks good column sizes. If all column heads are wider than
* the column's cells' contents, then you can just use
* column.sizeWidthToFit().
*/
private void initColumnSizes(JTable table) {
MyTableModel model = (MyTableModel) table.getModel();
TableColumn column = null;
Component comp = null;
int headerWidth = 0;
int cellWidth = 0;
Object[] longValues = model.longValues;
TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer();
for (int i = 0; i < 5; i++) {
column = table.getColumnModel().getColumn(i);
comp = headerRenderer.getTableCellRendererComponent(null,
column.getHeaderValue(), false, false, 0, 0);
headerWidth = comp.getPreferredSize().width;
comp = table.getDefaultRenderer(model.getColumnClass(i))
.getTableCellRendererComponent(table, longValues[i], false, false, 0, i);
cellWidth = comp.getPreferredSize().width;
if (DEBUG) {
System.out.println("Initializing width of column " + i + ". "
+ "headerWidth = " + headerWidth + "; cellWidth = "
+ cellWidth);
}
column.setPreferredWidth(Math.max(headerWidth, cellWidth));
}
}
public void setUpSportColumn(JTable table, TableColumn sportColumn) {
// Set up the editor for the sport cells.
JComboBox<String> comboBox = new JComboBox<String>();
comboBox.addItem("Snowboarding");
comboBox.addItem("Rowing");
comboBox.addItem("Knitting");
comboBox.addItem("Speed reading");
comboBox.addItem("Pool");
comboBox.addItem("None of the above");
sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
// Set up tool tips for the sport cells.
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
renderer.setToolTipText("Click for combo box");
sportColumn.setCellRenderer(renderer);
}
class MyTableModel extends AbstractTableModel {
private static final long serialVersionUID = 1L;
private String[] columnNames = { "First Name", "Last Name", "Sport",
"# of Years", "Vegetarian" };
private Object[][] data = {
{ "Kathy", "Smith", "Snowboarding", new Integer(5),
new Boolean(false) },
{ "John", "Doe", "Rowing", new Integer(3), new Boolean(true) },
{ "Sue", "Black", "Knitting", new Integer(2),
new Boolean(false) },
{ "Jane", "White", "Speed reading", new Integer(20),
new Boolean(true) },
{ "Joe", "Brown", "Pool", new Integer(10), new Boolean(false) } };
public final Object[] longValues = { "Jane", "Kathy",
"None of the above", new Integer(20), Boolean.TRUE };
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
/*
* JTable uses this method to determine the default renderer/ editor for
* each cell. If we didn't implement this method, then the last column
* would contain text ("true"/"false"), rather than a check box.
*/
public Class<? extends Object> getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
/*
* Don't need to implement this method unless your table's editable.
*/
public boolean isCellEditable(int row, int col) {
// Note that the data/cell address is constant,
// no matter where the cell appears onscreen.
if (col < 2) {
return false;
}
return true;
}
/*
* Don't need to implement this method unless your table's data can
* change.
*/
public void setValueAt(Object value, int row, int col) {
if (DEBUG) {
System.out.println("Setting value at " + row + "," + col
+ " to " + value + " (an instance of "
+ value.getClass() + ")");
}
data[row][col] = value;
fireTableCellUpdated(row, col);
if (DEBUG) {
System.out.println("New value of data:");
printDebugData();
}
}
private void printDebugData() {
int numRows = getRowCount();
int numCols = getColumnCount();
for (int i = 0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j = 0; j < numCols; j++) {
System.out.print(" " + data[i][j]);
}
System.out.println();
}
System.out.println("--------------------------");
}
}
/**
* Create the GUI and show it. For thread safety, this method should be
* invoked from the event-dispatching thread.
*/
private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("TableRenderDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create and set up the content pane.
TableRenderDemo newContentPane = new TableRenderDemo();
newContentPane.setOpaque(true); // content panes must be opaque
frame.setContentPane(newContentPane);
// Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
谢谢
Gary好的,首先,代码是由一位同事提供给我的(我原以为它就是Oracle网站上的代码),但不是,他一直在玩它 无论如何 我不明白的是,第一次跑步时,为什么整排都是绿色的 对于John和Jane,但当我点击素食者点击框时 其中只有组合框单元格改变颜色,而不是整行 我想我已经找到了发生这种情况的原因-对行中的每个单元格执行以下代码:
if ((Boolean) getModel().getValueAt(modelRow, 4)) {
c.setBackground(Color.GREEN);
} else {
c.setBackground(getBackground());
所以,当第一次绘制表格时。。。对于第一列,它检查第4列中的值,如果为“True”,则将第0列(和第1-3列)中的单元格设置为绿色。但是,当第4列中的值更改为“False”时,渲染器没有在第0列(等)中的单元格上运行,因此它不知道再次执行此检查,因此其他列的颜色无法更改
不管怎样,我已经通过
if ((Boolean) getModel().getValueAt(modelRow, 4)) {
c.setBackground(Color.GREEN);
repaint();
} else {
c.setBackground(getBackground());
快到了 尝试不同的实现以查看效果。渲染用于渲染多个单元格(通常用于同一列),如果不完全重新配置它们,它们会将状态从一个单元格传递到另一个单元格。发布一个可运行的示例Odd。。。这正是我所希望的,但我不明白为什么当我将“素食者”设置为false时,它会正常工作-代码不会重新绘制,但它会将所有单元格背景设置为白色。实际上,这提出了另一个问题,如果bacground是绿色的,为什么c.setBackground(getBackground())将其设置为白色?当然getBackground()返回绿色,而c.setBackground只是再次将其设置为绿色?
if ((Boolean) getModel().getValueAt(modelRow, 4)) {
c.setBackground(Color.GREEN);
} else {
c.setBackground(getBackground());
if ((Boolean) getModel().getValueAt(modelRow, 4)) {
c.setBackground(Color.GREEN);
repaint();
} else {
c.setBackground(getBackground());