/** The planet table model specifies the values, rendering and editing properties for the planet data. */ class PlanetTableModel extends AbstractTableModel { public String getColumnName(int c) { return columnNames[c]; } public Class getColumnClass(int c) { return cells[0][c].getClass(); } public int getColumnCount() { return cells[0].length; } public int getRowCount() { return cells.length; } public Object getValueAt(int r, int c) { return cells[r][c]; } public void setValueAt(Object obj, int r, int c) { cells[r][c] = obj; } public boolean isCellEditable(int r, int c) { return c == NAME_COLUMN || c == MOON_COLUMN || c == GASEOUS_COLUMN || c == COLOR_COLUMN; } public static final int NAME_COLUMN = 0; public static final int MOON_COLUMN = 2; public static final int GASEOUS_COLUMN = 3; public static final int COLOR_COLUMN = 4; private Object[][] cells = { { "Mercury ", new Double(2440), new Integer(0), Boolean.FALSE, Color.yellow, new ImageIcon( "temp1.jpg ") }, { "Venus ", new Double(6052), new Integer(0), Boolean.FALSE, Color.yellow, new ImageIcon( "temp1.jpg ") }, { "Earth ", new Double(6378), new Integer(1), Boolean.FALSE, Color.blue, new ImageIcon( "temp1.jpg ") }, { "Mars ", new Double(3397), new Integer(2), Boolean.FALSE, Color.red, new ImageIcon( "temp1.jpg ") }, { "Jupiter ", new Double(71492), new Integer(16), Boolean.TRUE, Color.orange, new ImageIcon( "temp1.jpg ") }, { "Saturn ", new Double(60268), new Integer(18), Boolean.TRUE, Color.orange, new ImageIcon( "temp1.jpg ") }, { "Uranus ", new Double(25559), new Integer(17), Boolean.TRUE, Color.blue, new ImageIcon( "temp1.jpg ") }, { "Neptune ", new Double(24766), new Integer(8), Boolean.TRUE, Color.blue, new ImageIcon( "temp1.jpg ") }, { "Pluto ", new Double(1137), new Integer(1), Boolean.FALSE, Color.black, new ImageIcon( "temp1.jpg ") } }; private String[] columnNames = { "Planet ", "Radius ", "Moons ", "Gaseous ", "Color ", "Image " }; } /** This renderer renders a color value as a panel with the given color. */ class ColorTableCellRenderer implements TableCellRenderer { public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { panel.setBackground((Color)value); return panel; } // the following panel is returned for all cells, with // the background color set to the Color value of the cell private JPanel panel = new JPanel(); } /** This editor pops up a color dialog to edit a cell value */ class ColorTableCellEditor extends AbstractCellEditor implements TableCellEditor { ColorTableCellEditor() { panel = new JPanel(); // prepare color dialog colorChooser = new JColorChooser(); colorDialog = JColorChooser.createDialog(null, "Planet Color ", false, colorChooser, new ActionListener() // OK button listener { public void actionPerformed(ActionEvent event) { stopCellEditing(); } }, new ActionListener() // Cancel button listener { public void actionPerformed(ActionEvent event) { cancelCellEditing(); } }); colorDialog.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent event) { cancelCellEditing(); } }); } public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { // this is where we get the current Color value. We // store it in the dialog in case the user starts editing colorChooser.setColor((Color)value); return panel; } public boolean shouldSelectCell(EventObject anEvent) { // start editing colorDialog.setVisible(true); // tell caller it is ok to select this cell return true; } public void cancelCellEditing() { // editing is canceled--hide dialog colorDialog.setVisible(false); super.cancelCellEditing(); } public boolean stopCellEditing() { // editing is complete--hide dialog colorDialog.setVisible(false); super.stopCellEditing(); // tell caller is is ok to use color value return true; } public Object getCellEditorValue() { return colorChooser.getColor(); } private Color color; private JColorChooser colorChooser; private JDialog colorDialog; private JPanel panel; } class ImageTableCellRenderer extends DefaultTableCellRenderer{ JLabel label = null; public ImageTableCellRenderer(){ super(); } @Override public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { label = (JLabel)super.getTableCellRendererComponent( table, value, isSelected, hasFocus, row, column ); System.out.println(row+"\t"+column+"\t"+value); ImageIcon icon = (ImageIcon)value; if(icon.getIconWidth()>table.getColumnModel().getColumn(column).getWidth()*1.1){ System.out.println("截取縮略圖"); double scale = 1.0*icon.getIconHeight()/icon.getIconWidth(); System.out.println("縮略比:"+scale); Image img = icon.getImage().getScaledInstance(table.getColumnModel().getColumn(column).getWidth(), (int)(table.getColumnModel().getColumn(column).getWidth()*scale),Image.SCALE_SMOOTH); icon.setImage(img); } System.out.println(icon.getIconWidth()+"\t"+icon.getIconHeight()); if(value!=null){ label.setText(null); label.setIcon(icon); return label; } JLabel label = new JLabel(); label.setText("圖片"); return label; }
} when run the application, don't drag the verticalScrollBar to the bottom,then I only see some pictures of all,then I resize the size of JFrame,the table automatically resize itself.then I drag the verticalscrollbar to the bottom,then you'll see a strange phenomenon, the "some pictures" we fist see is smaller then the "hidden pictures" that not shown at first. If,at first,you drag the verticalScrollBar to the bomttom,then all pictures are of same size,and never become big even if you resize JFrame. I think this phenomenon is caused by tableCellRenderer,how to make the pictures' size to fit the table cell when I resize the JFrame.Thank you very much. [/code]
/**
The planet table model specifies the values, rendering
and editing properties for the planet data.
*/
class PlanetTableModel extends AbstractTableModel
{
public String getColumnName(int c)
{
return columnNames[c];
} public Class getColumnClass(int c)
{
return cells[0][c].getClass();
} public int getColumnCount()
{
return cells[0].length;
} public int getRowCount()
{
return cells.length;
} public Object getValueAt(int r, int c)
{
return cells[r][c];
} public void setValueAt(Object obj, int r, int c)
{
cells[r][c] = obj;
} public boolean isCellEditable(int r, int c)
{
return c == NAME_COLUMN
|| c == MOON_COLUMN
|| c == GASEOUS_COLUMN
|| c == COLOR_COLUMN;
} public static final int NAME_COLUMN = 0;
public static final int MOON_COLUMN = 2;
public static final int GASEOUS_COLUMN = 3;
public static final int COLOR_COLUMN = 4; private Object[][] cells =
{
{
"Mercury ", new Double(2440), new Integer(0),
Boolean.FALSE, Color.yellow,
new ImageIcon( "temp1.jpg ")
},
{
"Venus ", new Double(6052), new Integer(0),
Boolean.FALSE, Color.yellow,
new ImageIcon( "temp1.jpg ")
},
{
"Earth ", new Double(6378), new Integer(1),
Boolean.FALSE, Color.blue,
new ImageIcon( "temp1.jpg ")
},
{
"Mars ", new Double(3397), new Integer(2),
Boolean.FALSE, Color.red,
new ImageIcon( "temp1.jpg ")
},
{
"Jupiter ", new Double(71492), new Integer(16),
Boolean.TRUE, Color.orange,
new ImageIcon( "temp1.jpg ")
},
{
"Saturn ", new Double(60268), new Integer(18),
Boolean.TRUE, Color.orange,
new ImageIcon( "temp1.jpg ")
},
{
"Uranus ", new Double(25559), new Integer(17),
Boolean.TRUE, Color.blue,
new ImageIcon( "temp1.jpg ")
},
{
"Neptune ", new Double(24766), new Integer(8),
Boolean.TRUE, Color.blue,
new ImageIcon( "temp1.jpg ")
},
{
"Pluto ", new Double(1137), new Integer(1),
Boolean.FALSE, Color.black,
new ImageIcon( "temp1.jpg ")
}
}; private String[] columnNames =
{
"Planet ", "Radius ", "Moons ", "Gaseous ", "Color ", "Image "
};
} /**
This renderer renders a color value as a panel with the
given color.
*/
class ColorTableCellRenderer implements TableCellRenderer
{
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column)
{
panel.setBackground((Color)value);
return panel;
} // the following panel is returned for all cells, with
// the background color set to the Color value of the cell private JPanel panel = new JPanel();
} /**
This editor pops up a color dialog to edit a cell value
*/
class ColorTableCellEditor extends AbstractCellEditor
implements TableCellEditor
{
ColorTableCellEditor()
{
panel = new JPanel();
// prepare color dialog colorChooser = new JColorChooser();
colorDialog = JColorChooser.createDialog(null,
"Planet Color ", false, colorChooser,
new
ActionListener() // OK button listener
{
public void actionPerformed(ActionEvent event)
{
stopCellEditing();
}
},
new
ActionListener() // Cancel button listener
{
public void actionPerformed(ActionEvent event)
{
cancelCellEditing();
}
});
colorDialog.addWindowListener(new
WindowAdapter()
{
public void windowClosing(WindowEvent event)
{
cancelCellEditing();
}
});
} public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected, int row, int column)
{
// this is where we get the current Color value. We
// store it in the dialog in case the user starts editing
colorChooser.setColor((Color)value);
return panel;
} public boolean shouldSelectCell(EventObject anEvent)
{
// start editing
colorDialog.setVisible(true); // tell caller it is ok to select this cell
return true;
} public void cancelCellEditing()
{
// editing is canceled--hide dialog
colorDialog.setVisible(false);
super.cancelCellEditing();
} public boolean stopCellEditing()
{
// editing is complete--hide dialog
colorDialog.setVisible(false);
super.stopCellEditing(); // tell caller is is ok to use color value
return true;
} public Object getCellEditorValue()
{
return colorChooser.getColor();
} private Color color;
private JColorChooser colorChooser;
private JDialog colorDialog;
private JPanel panel;
} class ImageTableCellRenderer extends DefaultTableCellRenderer{
JLabel label = null;
public ImageTableCellRenderer(){
super();
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
label = (JLabel)super.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, column
);
System.out.println(row+"\t"+column+"\t"+value);
ImageIcon icon = (ImageIcon)value;
if(icon.getIconWidth()>table.getColumnModel().getColumn(column).getWidth()*1.1){
System.out.println("截取縮略圖");
double scale = 1.0*icon.getIconHeight()/icon.getIconWidth();
System.out.println("縮略比:"+scale);
Image img = icon.getImage().getScaledInstance(table.getColumnModel().getColumn(column).getWidth(), (int)(table.getColumnModel().getColumn(column).getWidth()*scale),Image.SCALE_SMOOTH);
icon.setImage(img);
}
System.out.println(icon.getIconWidth()+"\t"+icon.getIconHeight());
if(value!=null){
label.setText(null);
label.setIcon(icon);
return label;
}
JLabel label = new JLabel();
label.setText("圖片");
return label;
}
}
when run the application, don't drag the verticalScrollBar to the bottom,then I only see some pictures of all,then I resize the size of JFrame,the table automatically resize itself.then I drag the verticalscrollbar to the bottom,then you'll see a strange phenomenon, the "some pictures" we fist see is smaller then the "hidden pictures" that not shown at first. If,at first,you drag the verticalScrollBar to the bomttom,then all pictures are of same size,and never become big even if you resize JFrame.
I think this phenomenon is caused by tableCellRenderer,how to make the pictures' size to fit the table cell when I resize the JFrame.Thank you very much.
[/code]
ImageIcon icon = (ImageIcon)value;
if(icon.getIconWidth()>table.getColumnModel().getColumn(column).getWidth()*1.1){
System.out.println("截取縮略圖");
double scale = 1.0*icon.getIconHeight()/icon.getIconWidth();
System.out.println("縮略比:"+scale);
Image img = icon.getImage().getScaledInstance(table.getColumnModel().getColumn(column).getWidth(), (int)(table.getColumnModel().getColumn(column).getWidth()*scale),Image.SCALE_SMOOTH);
icon.setImage(img);
}
这样写的话会影响到model中的图片值的不过如果按JTable自己来重绘的话太卡了
,请问各位大神有没有好办法呢?