-
Bug
-
Resolution: Not an Issue
-
P4
-
None
-
1.2.2
-
generic
-
generic
Name: skT88420 Date: 07/19/99
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.JScrollPane;
import javax.swing.JFrame;
import javax.swing.JEditorPane;
import java.awt.*;
import java.awt.event.*;
import java.lang.Object;
public class TableDemo extends JFrame {
private boolean DEBUG = true;
public TableDemo() {
super("TableDemo");
MyTableModel myModel = new MyTableModel();
JTable table = new JTable(myModel);
table.setPreferredScrollableViewportSize(new Dimension(500, 50));
// if the col is a Dummy class, then lookup the renderer JEdRenderer
table.setDefaultRenderer(Dummy.class, new JEdRenderer());
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Add the scroll pane to this window.
getContentPane().add(scrollPane, BorderLayout.CENTER);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
//----------------------------------------------------------
class JEdRenderer extends JEditorPane implements TableCellRenderer{
public JEdRenderer(){
super();
setContentType("text/html");
}
public Component getTableCellRendererComponent(JTable table,
Object dum,
boolean isSelected,
boolean hasFocus,
int row, int col){
System.out.println("Content Type: "+this.getEditorKit().getContentType());
System.out.println("Kit: "+this.getEditorKit().toString());
// ??? The following text cannot show up in the table ???
this.setText("<html><b>testing</b> bi li ba la<font color=blue> yeah </font></html>");
System.out.println("! "+row+" , "+col+" ! "+this.getText());
return this;
}
}
//----------------------------------------------------------
class Dummy extends Object{
private String info;
public Dummy(String st){
info = st;
}
public String getInfo(){ return info; }
}
//----------------------------------------------------------
class MyTableModel extends AbstractTableModel {
final String[] columnNames = {"Col 1", "Col 2"};
final Object[][] data = {
{new Boolean(false), new Dummy("<html><b>peter</b></html>")},
{new Boolean(true), new Dummy("<html>hello</html>")}
};
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]; }
public Class getColumnClass(int c) { return getValueAt(0, c).getClass(); }
public boolean isCellEditable(int row, int col) { return (col==0)?true:false; }
public void setValueAt(Object value, int row, int col){
if (DEBUG){
System.out.println("Setting value at "+row+", "+col+
" to " + value);
}
if (col==0){
data[row][col] = value;
fireTableCellUpdated(row,col);
}
}
} // class MyTableModel
public static void main(String[] args) {
TableDemo frame = new TableDemo();
frame.pack();
frame.setVisible(true);
}
}
(Review ID: 85753)
======================================================================