这是一个范例import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Vector;import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;public class Test extends JFrame
{
String[] columnNames =
{
"Name","Check-In Date","Check-Out Date","Smoking",
"Fax","Laptop","Room Rate","Photo",
}; Date dayOne = (new GregorianCalendar(2000,10,5)).getTime();
Date dayTwo = (new GregorianCalendar(2000,10,7)).getTime(); Object[][] data =
{
{"Andrews",dayOne,dayTwo,
new Boolean(true),new Boolean(true),
new Boolean(true),new Double(79.99),
new ImageIcon("tenchi.jpg")}
, {"Anthony",dayOne,dayTwo,
new Boolean(false),new Boolean(false),
new Boolean(false),new Double(69.99),
new ImageIcon("washu.jpg")}
, {"Woodard",dayOne,dayTwo,
new Boolean(true),new Boolean(false),
new Boolean(false),new Double(99.99),
new ImageIcon("sasami.jpg")}
, {"Thomas",dayOne,dayTwo,
new Boolean(false),new Boolean(true),
new Boolean(true),new Double(79.99),
new ImageIcon("aeka.jpg")}
, {"Reed",dayOne,dayTwo,
new Boolean(true),new Boolean(true),
new Boolean(true),new Double(79.99),
new ImageIcon("tenchi.jpg")}
, {"Crenshaw",dayOne,dayTwo,
new Boolean(false),new Boolean(false),
new Boolean(false),new Double(69.99),
new ImageIcon("washu.jpg")}
, {"Royal",dayOne,dayTwo,
new Boolean(true),new Boolean(false),
new Boolean(false),new Double(99.99),
new ImageIcon("sasami.jpg")}
, {"Moore",dayOne,dayTwo,
new Boolean(false),new Boolean(true),
new Boolean(true),new Double(79.99),
new ImageIcon("aeka.jpg")}
,
}; JTable table = new JTable(new CustomModel(data,columnNames));
public Test()
{
getContentPane().add(new JScrollPane(table),BorderLayout.CENTER);
} public static void main(String args[])
{
GJApp.launch(new Test(),"A Custom Table Model That Specifies Column Classes",300,300,650,182);
}
}class CustomModel extends DefaultTableModel
{
public CustomModel(Object[][] data,Object[] columnNames)
{
super(data,columnNames);
} public Class getColumnClass(int col)
{
// dataVector is a protected member of DefaultTableModel Vector v = (Vector)dataVector.elementAt(0);
return v.elementAt(col).getClass();
} public boolean isCellEditable(int row,int col)
{
Class columnClass = getColumnClass(col);
return columnClass != ImageIcon.class && columnClass != Date.class;
}
}class GJApp extends WindowAdapter
{
static private JPanel statusArea = new JPanel();
static private JLabel status = new JLabel(" ");
static private ResourceBundle resources; public static void launch(final JFrame f,String title,final int x,final int y,final int w,int h)
{
launch(f,title,x,y,w,h,null);
} public static void launch(final JFrame f,String title,final int x,final int y,final int w,int h,String propertiesFilename)
{
f.setTitle(title);
f.setBounds(x,y,w,h);
f.setVisible(true); statusArea.setBorder(BorderFactory.createEtchedBorder());
statusArea.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
statusArea.add(status);
status.setHorizontalAlignment(JLabel.LEFT); f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); if(propertiesFilename != null)
{
resources = ResourceBundle.getBundle(propertiesFilename,Locale.getDefault());
} f.addWindowListener(new WindowAdapter()
{
public void windowClosed(WindowEvent e)
{
System.exit(0);
}
});
} static public JPanel getStatusArea()
{
return statusArea;
} static public void showStatus(String s)
{
status.setText(s);
} static Object getResource(String key)
{
if(resources != null)
{
return resources.getString(key);
}
return null;
}
}
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Vector;import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;public class Test extends JFrame
{
String[] columnNames =
{
"Name","Check-In Date","Check-Out Date","Smoking",
"Fax","Laptop","Room Rate","Photo",
}; Date dayOne = (new GregorianCalendar(2000,10,5)).getTime();
Date dayTwo = (new GregorianCalendar(2000,10,7)).getTime(); Object[][] data =
{
{"Andrews",dayOne,dayTwo,
new Boolean(true),new Boolean(true),
new Boolean(true),new Double(79.99),
new ImageIcon("tenchi.jpg")}
, {"Anthony",dayOne,dayTwo,
new Boolean(false),new Boolean(false),
new Boolean(false),new Double(69.99),
new ImageIcon("washu.jpg")}
, {"Woodard",dayOne,dayTwo,
new Boolean(true),new Boolean(false),
new Boolean(false),new Double(99.99),
new ImageIcon("sasami.jpg")}
, {"Thomas",dayOne,dayTwo,
new Boolean(false),new Boolean(true),
new Boolean(true),new Double(79.99),
new ImageIcon("aeka.jpg")}
, {"Reed",dayOne,dayTwo,
new Boolean(true),new Boolean(true),
new Boolean(true),new Double(79.99),
new ImageIcon("tenchi.jpg")}
, {"Crenshaw",dayOne,dayTwo,
new Boolean(false),new Boolean(false),
new Boolean(false),new Double(69.99),
new ImageIcon("washu.jpg")}
, {"Royal",dayOne,dayTwo,
new Boolean(true),new Boolean(false),
new Boolean(false),new Double(99.99),
new ImageIcon("sasami.jpg")}
, {"Moore",dayOne,dayTwo,
new Boolean(false),new Boolean(true),
new Boolean(true),new Double(79.99),
new ImageIcon("aeka.jpg")}
,
}; JTable table = new JTable(new CustomModel(data,columnNames));
public Test()
{
getContentPane().add(new JScrollPane(table),BorderLayout.CENTER);
} public static void main(String args[])
{
GJApp.launch(new Test(),"A Custom Table Model That Specifies Column Classes",300,300,650,182);
}
}class CustomModel extends DefaultTableModel
{
public CustomModel(Object[][] data,Object[] columnNames)
{
super(data,columnNames);
} public Class getColumnClass(int col)
{
// dataVector is a protected member of DefaultTableModel Vector v = (Vector)dataVector.elementAt(0);
return v.elementAt(col).getClass();
} public boolean isCellEditable(int row,int col)
{
Class columnClass = getColumnClass(col);
return columnClass != ImageIcon.class && columnClass != Date.class;
}
}class GJApp extends WindowAdapter
{
static private JPanel statusArea = new JPanel();
static private JLabel status = new JLabel(" ");
static private ResourceBundle resources; public static void launch(final JFrame f,String title,final int x,final int y,final int w,int h)
{
launch(f,title,x,y,w,h,null);
} public static void launch(final JFrame f,String title,final int x,final int y,final int w,int h,String propertiesFilename)
{
f.setTitle(title);
f.setBounds(x,y,w,h);
f.setVisible(true); statusArea.setBorder(BorderFactory.createEtchedBorder());
statusArea.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
statusArea.add(status);
status.setHorizontalAlignment(JLabel.LEFT); f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); if(propertiesFilename != null)
{
resources = ResourceBundle.getBundle(propertiesFilename,Locale.getDefault());
} f.addWindowListener(new WindowAdapter()
{
public void windowClosed(WindowEvent e)
{
System.exit(0);
}
});
} static public JPanel getStatusArea()
{
return statusArea;
} static public void showStatus(String s)
{
status.setText(s);
} static Object getResource(String key)
{
if(resources != null)
{
return resources.getString(key);
}
return null;
}
}
解决方案 »
- 希望ecplise能尽快入门,如何建立简单工程
- 有熟悉润乾报表开发软件的吗?有的话留个QQ好吗?谢谢了
- [新手提问]关于空引用异常 (昨天那个"io流复制文件贴"续)
- 窗口重复打开问题如何解决啊?
- 请问有没有读取.CAB文件的java程序包啊
- 改FileChooser急求
- 请问eclipse如何显示java api的帮助
- 大家帮我看一下这个程序,为什么执行后会提示有错误!!急,在线等!
- 写一个tail程序,能够打印出命令行指定的文件的最后十行,
- JTextField 这个类有些什么方法啊?我想获取其输入值。
- 一个菜到不能在菜的问题了!@#$%
- 我改变了TableCell的BackGround之后,为什么没有马上生效?
首先,你要将想要显示图片的列进行如下类型设置:
ImageIcon ii=new ImageIcon("xxx.jpg");
添加这个object到JTable相应的列里最重要的是:
扩展一个TableModel类,如:
class CustomModel extends DefaultTableModel {
public Class getColumnClass(int col) {
Vector v=(Vector)dataVector.elementAt(0);
return v.element(col).getClass();
}
}
最后
JTable jtable=new JTable();
jtable.setModel(new CustomModel());
class ResultSetTableModel extends DefaultTableModel
{
public ResultSetTableModel(Object[][] data,Object[] columnNames)
{
super(data,columnNames);
}
public Class getColumnClass(int col) {
Vector v=(Vector)dataVector.elementAt(0);
return v.elementAt(col).getClass();
}
}
class ResultSetFrame extends JFrame
{ public ResultSetFrame()
{
Vector userInfo = new Vector();
Vector columnNames = new Vector();
Vector data = new Vector();
columnNames.add("编号");
while(rs.next())
{
String id = rs.getString(1);//code
String mypicture = rs.getString(8);//picture
userInfo.add(code);
ImageIcon rpicture=new ImageIcon("aaa.gif");//mypicture存放图片路径
userInfo.add(rpicture);
data.add(userInfo);
}
JTable table=new JTable();
table.setModel(new ResultSetTableModel());//此处编译出错
.....
}
错误如下:
cannot resolve symbol
constructor ResultSetTableModel()
location:class ResultSetTableModel
table.setMode(new ResultSetTableModel());
实在找不到是怎么回事
这是我对java不熟的缘故,我把
public ResultSetTableModel(Object[][] data,Object[] columnNames)
改成:
public ResultSetTableModel(Vector data,Vector columnNames)
然后:
ResultSetTableModel model = new ResultSetTableModel(data,columnNames);
JTable table=new JTable(model);
成功显示图片!