Hi
I've got a JComponent which I use as a TableCellRenderer.
The problem is that the Image is not drawn to the Graphics object of the paint method. Only when the form is resized is the Image shown. If the image change it is not shown at all. (remove //loadPicture((String)value); in the getTableCellRendererComponent method of the TestCellRenderer class)
I've used other methods on the JComponent just to see if it is working... all the other methods works fine... like the drawLine, drawString ...... but not the drawImage.
Any ideas ?
Here is the code
Toyman
VB / Java Programmer
carel_dutoit@yahoo.co.uk
I've got a JComponent which I use as a TableCellRenderer.
The problem is that the Image is not drawn to the Graphics object of the paint method. Only when the form is resized is the Image shown. If the image change it is not shown at all. (remove //loadPicture((String)value); in the getTableCellRendererComponent method of the TestCellRenderer class)
I've used other methods on the JComponent just to see if it is working... all the other methods works fine... like the drawLine, drawString ...... but not the drawImage.
Any ideas ?
Here is the code
Code:
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.table.*;
public class TestCellRenderer extends JComponent implements TableCellRenderer{
Image img;
ChangeListener cl;
public TestCellRenderer(){
try{
loadPicture("ac1.gif");
setSize(new Dimension(100,100));
this.setPreferredSize(new Dimension(100,100));
this.setBorder(new LineBorder(Color.RED));
}
catch(Exception e){
e.printStackTrace();
}
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column){
//loadPicture((String)value);
this.updateUI();
return this;
}
public Image getImage(){
return img;
}
public void setImage(Image img){
this.img = img;
}
public void addChangeListener(ChangeListener l){
cl = l;
}
private void loadPicture(String path){
try{
//if (img != null){
// img.flush();
// img = null;
//}
img = Toolkit.getDefaultToolkit().createImage(path);
if (cl != null){
cl.stateChanged(new ChangeEvent(this));
}
}
catch(Exception e){
e.printStackTrace();
}
}
public void paint(Graphics graphics){
Graphics2D g = (Graphics2D)graphics;
try{
g.drawLine(0,0,getWidth(),getHeight());
g.drawImage(img,0,0,getWidth(),getHeight(),null);
g.setColor(Color.RED);
g.drawLine(getWidth(),0,0,getHeight());
super.paint(g);
}
catch(Exception e){
e.printStackTrace();
}
}
}
Code:
import javax.swing.table.*;
public class TestModel extends DefaultTableModel{
public TestModel(){
this.addColumn("Image Column");
this.addColumn("Column Two");
}
}
Code:
import java.awt.*;
import java.util.*;
import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.*;
class TestPaintInTable extends JFrame{
private static TestCellRenderer viewer = new TestCellRenderer();
public static void main (String args[]){
TestPaintInTable frame = new TestPaintInTable();
frame.setSize(300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TestModel model = new TestModel();
//Data
Vector vecData = new Vector();
vecData.add("ac.gif");
vecData.add("Aircraft One");
model.addRow(vecData);
vecData.removeAllElements();
vecData.add("ac1.gif");
vecData.add("Aircraft Two");
model.addRow(vecData);
TestTable testTable = new TestTable(model);
TableColumn col = testTable.getColumnModel().getColumn(0);
TestCellRenderer tCR = new TestCellRenderer();
tCR.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e){
System.out.println("State has changed. Image = " + ((TestCellRenderer)e.getSource()).getImage() );
viewer.setImage(((TestCellRenderer)e.getSource()).getImage());
viewer.update(viewer.getGraphics());
}
});
col.setCellRenderer(tCR);
JScrollPane scr = new JScrollPane(testTable);
frame.setLayout(new BorderLayout());
frame.getContentPane().add(viewer,BorderLayout.NORTH);
frame.getContentPane().add(scr,BorderLayout.CENTER);
frame.setVisible(true);
}
}
Code:
import javax.swing.*;
import javax.swing.table.*;
public class TestTable extends JTable{
public TestTable(DefaultTableModel model){
super(model);
}
}
Toyman
VB / Java Programmer
carel_dutoit@yahoo.co.uk