package filechooser;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumnModel;
public class FrameTableDemo extends JFrame
{
private static final long serialVersionUID = 1L;
private boolean debug = true;
public FrameTableDemo()
{
super("职位表");
MyTableModel myModel = new MyTableModel();
JTable table = new JTable(myModel);
table.setPreferredScrollableViewportSize(new Dimension(400, 100));
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
TableColumnModel md7 = (TableColumnModel)table.getColumnModel();
md7.getColumn(0).setMinWidth(30);
md7.getColumn(1).setMaxWidth(50);
md7.getColumn(2).setMaxWidth(60);
JScrollPane scrollPane = new JScrollPane(table);
table.setRowMargin(2);
getContentPane().add(scrollPane, BorderLayout.CENTER);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
class MyTableModel extends AbstractTableModel
{
private static final long serialVersionUID = 1L;
final String[] columnNames = {"相片", "姓名", "职位", "电话", "工资", "婚否 "};
ImageIcon apple = createImageIcon("apple.jpg", "天峰相片");
ImageIcon anther = createImageIcon("anther.jpg", "其它相片");
ImageIcon apple1 = createImageIcon("apple1.jpg", "天峰相片");
ImageIcon anther2 = createImageIcon("anther2.jpg", "天峰相片");
final Object[][] data = { {apple, "天峰", "自由作家 ", "02988225560", Integer.valueOf(2000), Boolean.valueOf(false)},
{anther, "娜娜", "总经理秘书 ", "01068939397", Integer.valueOf(5000), Boolean.valueOf(true)},
{anther2, "杨深刚", "科长 ", "0296887678", Integer.valueOf(6000), Boolean.valueOf(true)},
{apple1, "李少恒", "财务科长 ", "02933541290", Integer.valueOf(4000), Boolean.valueOf(false)}};
/** 创建图片对象 */
public ImageIcon createImageIcon(String filename, String description)
{
String path = "/image/" + filename;
System.out.println(path);
return new ImageIcon(getClass().getResource(path), description);
}
/** 获取列长度 */
public int getColumnCount()
{
return columnNames.length;
}
/** 获取行长度 */
public int getRowCount()
{
return data.length;
}
/** 获取单元格值 */
public Object getValueAt(int rowIndex, int columnIndex)
{
return data[rowIndex][columnIndex];
}
/** 获取列属性 */
public Class<?> getColumnClass(int c)
{
return getValueAt(0, c).getClass();
}
FrameTableDemo1
最新推荐文章于 2022-02-25 23:47:59 发布